Num[i] Represents the value of I to the root node
This problem at first thought is a line tree = =! Later, it was found that the segment tree was unable to maintain sub-intervals
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 222222; int fa[maxn],num[maxn];int find_father (int u) {if (fa[u] = = u) return fa[u]; else{int temp = Fa[u]; Fa[u] = Find_father (Fa[u]); Num[u] + = num[temp]; return Fa[u]; }}int Main () {int n,m; while (scanf ("%d%d", &n,&m)! = EOF) {memset (num,0,sizeof (num)); int cnt = 0; for (int i = 0; I <= N; i++) fa[i] = i; for (int i = 0; i < m; i++) {int x, y, Z; scanf ("%d%d%d", &x,&y,&z); X--; int L = find_father (x), R = Find_father (y); if (L = = r) {int e = Num[y]-num[x]; if (E! = z) {cnt + +; }} else{Fa[r] = l; Num[r] = z + num[x]-num[y]; }} printf ("%d\n", CNT); } return 0;}
"HDU" How many Answers is wrong (with right and check set)