我竟然会忍痛写了这道题,边写边想骂人,mdzz
dfs一遍就可以,取对数后乘除化加减。
考场上能让直接long double乘的人过去了,出题人我也是服了。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 20010
#define eps 1e-8
using namespace std;
long double dep[maxn],lena[maxn],lenb[maxn];
bool d[maxn],len[maxn],vis[maxn];
int head[maxn],to[maxn],next[maxn];
int n,m,num,T;
bool flag;
void addedge(int x,int y,int a,int b,bool w)
{
num++;to[num]=y;lena[num]=a;lenb[num]=b;len[num]=w;next[num]=head[x];head[x]=num;
}
void dfs(int x,int fa)
{
if (flag) return;
vis[x]=1;
for (int p=head[x];p;p=next[p])
if (to[p]!=fa)
{
if (!vis[to[p]]) d[to[p]]=(d[x]^len[p]),dep[to[p]]=dep[x]+log(lenb[p])-log(lena[p]),dfs(to[p],x);
else if (d[to[p]]!=(d[x]^len[p]) || abs(dep[to[p]]-dep[x]-log(lenb[p])+log(lena[p]))>eps) {flag=1;return;}
}
}
int main()
{
scanf("%d",&T);
for (int j=1;j<=T;j++)
{
scanf("%d%d",&n,&m);
num=0;
memset(head,0,sizeof(head));
for (int i=1;i<=m;i++)
{
int x,y,a,b;
scanf("%d%d%d%d",&x,&y,&a,&b);
if (b<0) addedge(x,y,a,-b,1),addedge(y,x,-b,a,1);
else addedge(x,y,a,b,0),addedge(y,x,b,a,0);
}
flag=0;
memset(vis,0,sizeof(vis));
for (int i=1;i<=n;i++) if (!vis[i]) dep[i]=0,dfs(i,0);
printf("Case #%d: ",j);
if (flag) printf("No\n"); else printf("Yes\n");
}
return 0;
}