1202: [HNOI2005]狡猾的商人
带权并差集,后续将补上查分约束版本
BZOJ 1202
/**************************************************************
Problem: 1202
User: Dream_Tonight
Language: C++
Result: Accepted
Time:228 ms
Memory:1296 kb
****************************************************************/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int fa[1005], d[1005];
int find(int x) {
if (x == fa[x]) return x;
int t = find(fa[x]);
d[x] += d[fa[x]];
return fa[x] = t;
}
int main() {
int n, m, x, y, z, w;
scanf("%d", &w);
while (w--) {
bool fake = true;
scanf("%d%d", &n, &m);
memset(d, 0, sizeof(d));
for (int i = 0; i <= n; i++) fa[i] = i;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &z);x--;
int fx = find(x);
int fy = find(y);
if (fx != fy) {
fa[fx] = fy;
d[fx] = z - d[x] + d[y];
} else if (d[x] - d[y] != z) {
fake = false;
}
}
puts(fake ? "true" : "false");
}
return 0;
}