题目
带权并查集
#include<iostream>
#include<cstdio>
#include<cmath>
#define eps 1e-9;
using namespace std;
const int maxn=1100;
int t,n,m,bo;
int f[maxn];
double g[maxn];
int find(int x)
{
if(f[x]==x) return x;
int tmp=find(f[x]);
g[x]*=g[f[x]];
return f[x]=tmp;
}
bool judge(double a,double b){return fabs(a-b)<=eps;}
int main()
{
cin>>t;
for(int j=1;j<=t;j++)
{
cin>>n>>m;bo=0;
for(int i=1;i<=n;i++)f[i]=i,g[i]=1;
for(int i=1;i<=m;i++)
{
int a,b;double x,y;
cin>>a>>b>>x>>y;
int l1=find(a);
int l2=find(b);
if(l1==l2)
{
if(!judge(x/y,g[a]/g[b])) { bo=1;}
}
else f[l2]=l1,g[l2]*=g[a]/g[b]*y/x;
}
if(bo) printf("Case #%d: No\n",j);
else printf("Case #%d: Yes\n",j);
}
return 0;
}