裸的带权并查集
如果输入的两者不知道关系,就新建关系
如果两者知道关系,看看两者的关系是否与输入一致就AC辣
坑点
Bessie’s eyes are good
Bessie is blind.
这才是输出,不要相信样例啊!
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int fat[9999],high[9999];
int find(int x)
{
if(fat[x]==x)
return x;
int tmp=fat[x];
fat[x]=find(fat[x]);
high[x]+=high[tmp];
return fat[x];
}
void work()
{
int n,m;
memset(high,0,sizeof(high));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fat[i]=i;
bool flag=1;
for(int i=1;i<=m;i++)
{
int a,b,t;
scanf("%d%d%d",&a,&b,&t);
int f1=find(a),f2=find(b);
if(f1!=f2)
{
fat[f2]=f1;
high[f2]=high[a]+t-high[b];
}
else
{
int sum=high[b]-high[a];
if(sum!=t) flag=0;
}
}
if(flag) printf("Bessie's eyes are good\n");
else printf("Bessie is blind.\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--) work();
}