Files
python/TangDou/AcWing/HihoCoder1515.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

51 lines
1.0 KiB
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int p[N];
int score[N];
int find(int x) {
if (x != p[x]) {
int t = p[x];
p[x] = find(p[x]);
score[x] += score[t];
}
return p[x];
}
//https://blog.csdn.net/yjr3426619/article/details/82315133
int main() {
//输入+输出重定向
freopen("../AcWing/N5/HihoCoder1515.txt", "r", stdin);
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i++) {
p[i] = i;
}
while (m--) {
int x, y, s;
scanf("%d%d%d", &x, &y, &s);
int px = find(x);
int py = find(y);
if (px != py) {
p[px] = py;
score[px] = -score[x] + score[y] + s;
}
}
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
if (find(x) != find(y)) {
printf("-1\n");
} else {
printf("%d\n", score[x] - score[y]);
}
}
//关闭文件
fclose(stdin);
return 0;
}