题意:输入 A B X,表示 B 的权值比 A 多 X,以300为周期,求描述错误的数目
思路:标准带权并查集
#include <cstdio> #include <cstring> #include <iostream> using namespace std; #define maxn 200000 + 10 int father[maxn]; int val[maxn]; int Find(int x) { if (x == father[x]) return x; int temp = father[x]; father[x] = Find(father[x]); val[x] += val[temp]; return father[x]; } int n, m; int main() { while (~scanf("%d %d", &n, &m)) { for (int i = 1; i <= n; i++) { father[i] = i; val[i] = 0; } int ans = 0; for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); // u -= 1; int ru = Find(u), rv = Find(v); if (ru == rv) { if (val[u] + w != val[v]) ans++; } else { father[rv] = ru; val[rv] = val[u] - val[v] + w; } } printf("%d ", ans); } return 0; }