BZOJ4602 SDOI2016齒輪(搜尋)
阿新 • • 發佈:2018-11-03
dfs一遍給每個齒輪隨便標個值看是否矛盾就行了。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 1010 #define M 10010 int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);}int T,n,m,p[N],t; struct data{int to,nxt;double len; }edge[M<<1]; const double eps=1E-8; bool flag[N],ans; double f[N]; void addedge(int x,int y,double z){t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].len=z;p[x]=t;} void dfs(int k) { flag[k]=1; for (int i=p[k];i;i=edge[i].nxt) if (!flag[edge[i].to]) { f[edge[i].to]=f[k]*edge[i].len; dfs(edge[i].to); } else if (fabs(f[k]*edge[i].len-f[edge[i].to])>eps) ans=0; } int main() { #ifndef ONLINE_JUDGE freopen("bzoj4602.in","r",stdin); freopen("bzoj4602.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif T=read(); for (int k=1;k<=T;k++) { n=read(),m=read();ans=1; memset(p,0,sizeof(p));t=0; for (int i=1;i<=m;i++) { int x=read(),y=read(),a=read(),b=read(); addedge(x,y,(double)b/a),addedge(y,x,(double)a/b); } memset(flag,0,sizeof(flag)); for (int i=1;i<=n;i++) if (!flag[i]) {f[i]=1;dfs(i);} printf("Case #%d: ",k); if (ans) printf("Yes\n");else printf("No\n"); } return 0; }