权值并查集裸题的感觉。。。
哎就是自己太傻了,写错一个关键性的地方
权值累加的过程中一点要先get(F[x])再去加!
这个问题处理好就可以了。。哎
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <ctime>
#include <vector>
#define inf 1e9
#define ll long long
#define For(i,j,k) for(int i=j;i<=k;i++)
#define Dow(i,j,k) for(int i=k;i>=j;i--)
using namespace std;
inline void read(int &tx){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} tx=x*f; }
inline void write(ll x){ if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10); putchar(x%10+'0'); }
inline void writeln(ll x){write(x);puts("");}
using namespace std;
int v[10001],F[10001],n,m,x,y,z,T;
inline int get(int x)
{
if(x==F[x]) return x;
int tmp=get(F[x]);
v[x]+=v[F[x]];
return F[x]=tmp;
}
inline bool merge(int x,int y,int z)
{
x--;
int tx=get(x),ty=get(y);
if(tx==ty)
if(v[y]-v[x]-z==0)return 1;else return 0;
F[tx]=ty;
v[tx]=v[y]-v[x]-z;
return 1;
}
inline void doit()
{
bool flag=1;
read(n);read(m);
For(i,0,n) F[i]=i,v[i]=0;
For(i,1,m)
{
read(x);read(y);read(z);
if(!merge(x,y,z)) {flag=0;}
}
if(!flag) puts("false");else
puts("true");
}
int main()
{
read(T);
while(T--)
doit();
}