bzoj4602 [Sdoi2016] Gear edge right and check set

Draw the tree structure of the union search and try to compress the path, which is to directly multiply the edge weights, which means that the current node makes one turn and the parent node makes a few turns. .

If the card accuracy can be used to take the log or decompose the prime factor.


code:

#include<iostream>
#include<cstdio>
using namespace std;
int mb;
double la,lb;
long double v[10005],a,b;
int x,y,fu[10005],i,j,n,m,T,sta[10005],top,k;
bool ky;
void zhao(int o)
{
	int i;
	top=0;
while(fu[fu[o]]!=fu[o])
{	
mb=fu[fu[o]];
sta[++top]=o;
o=fu[o];
}
for(i=top;i>=1;i--)
v[sta[i]]=v[sta[i]]*v[fu[sta[i]]],fu[sta[i]]=mb;
}
int main()
{
	scanf("%d",&T);
	for(j=1;j<=T;j++)
	{ky=1;
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)fu[i]=i,v[i]=1;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%lf%lf",&x,&y,&la,&lb);a=la,b=lb;
			zhao(x);zhao(y);
			int f1=fu[x],f2=fu[y];
			if(f1!=f2)
			{
			v[f1]=(v[y]*b)/(v[x]*a);fu[f1]=f2;
		    }
			else
			{
				if(v[x]*a+0.00000000001>v[y]*b&&v[x]*a-0.00000000001<v[y]*b);
				else
				{
					ky=0;
				}
			}
		}
		if(ky==0)printf("Case #%d: No\n",j);
		else printf("Case #%d: Yes\n",j);
	}
}

Guess you like

Origin blog.csdn.net/haobang866/article/details/79369531