[Bzoj1202] cunning businessman

Hutchison and si represents the prefix, since the books may be negative, si se is no limit, then a message corresponding to each day so that a difference is determined, even an edge, after each message, the first two points is determined whether communicating, communicating directly to judgment, do not communicate it to add this edge, this thing can be weighted disjoint-set to maintain

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t,n,m,x,y,z,f[105],v[105];
 4 int find(int k){
 5     if (k==f[k])return k;
 6     int t=find(f[k]);
 7     v[k]+=v[f[k]];
 8     return f[k]=t;
 9 }
10 bool add(int x,int y,int z){
11     int p=find(x),q=find(y);
12     if (p==q)return (v[y]-v[x]==z);
13     f[p]=q;
14     v[p]=v[y]-v[x]-z;
15     return 1;
16 }
17 int main(){
18     scanf("%d",&t);
19     while (t--){
20         scanf("%d%d",&n,&m);
21         bool flag=0;
22         memset(v,0,sizeof(v));
23         for(int i=0;i<=n;i++)f[i]=i;
24         for(int i=1;i<=m;i++){
25             scanf("%d%d%d",&x,&y,&z);
26             if (!add(x-1,y,z))flag=1;
27         }
28         if (flag)printf("false\n");
29         else printf("true\n");
30     }
31 }
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11825291.html