对于30%的数据,保证n<=100,m<=1000;
对于100%的数据,保证w<=100,n<=1000,m<=30000,|A|<=30000.
题解
#include<iostream>
#include<cstdio>
#define N 1010
using namespace std;
struct node
{
int fa,val;
}cow[N];
int T,n,m;
int root(int x)
{
if (cow[x].fa == x) return x;
int y=cow[x].fa; // fa的值会改变,先记录一下
cow[x].fa = root(cow[x].fa);
cow[x].val = cow[x].val+cow[y].val;
return cow[x].fa;
}
int main()
{
int s,t,v,ans;
scanf("%d",&T);
for (int I=1;I<=T;I++)
{
ans=1;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
cow[i].fa = i;
cow[i].val = 0;
}
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&v);
if (!ans) continue;
if (s == t && v!=0)
{
ans = 0;
continue;
}
int fx=root(s),fy=root(t);
if (fx != fy)
{
cow[fx].fa=fy;
cow[fx].val=cow[t].val-cow[s].val+v; //算一下
}
else
{
if (cow[t].val+v != cow[s].val)
ans=0;
}
}
if (ans) printf("Bessie‘s eyes are good\n");
else printf("Bessie is blind.\n");
}
}