记忆化搜索,强行double可过。。。数据简直..
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 #define ld double 7 using namespace std; 8 const int maxn=1023,maxm=10023; 9 const ld eps=1e-8; 10 struct zs{int too,pre;ld v;}e[maxm<<1];int tot,last[maxn]; 11 bool u[maxn]; 12 ld val[maxn]; 13 int i,j,k,n,m,ans; 14 bool flag; 15 16 int ra,fh;char rx; 17 inline int read(){ 18 rx=getchar(),ra=0,fh=1; 19 while((rx<‘0‘||rx>‘9‘)&&rx!=‘-‘)rx=getchar(); 20 if(rx==‘-‘)fh=-1,rx=getchar(); 21 while(rx>=‘0‘&&rx<=‘9‘)ra*=10,ra+=rx-48,rx=getchar();return ra*fh; 22 } 23 inline void insert(int a,int b,ld c){ 24 e[++tot].too=b,e[tot].v=c,e[tot].pre=last[a],last[a]=tot; 25 } 26 void dfs(int x,ld v){ 27 if(u[x])return;u[x]=1,val[x]=v; 28 for(int i=last[x];i&&flag;i=e[i].pre)if(!u[e[i].too])dfs(e[i].too,v*e[i].v); 29 else if(fabs(val[e[i].too]-v*e[i].v)>eps)flag=0; 30 } 31 int main(){int a,b,c,d,TT=0; 32 for(int T=read();T;T--){TT++,tot=0,memset(last,0,(n+1)<<2); 33 n=read(),m=read(); 34 for(i=1;i<=m;i++) 35 a=read(),b=read(),c=read(),d=read(), 36 insert(a,b,(ld)d/c),insert(b,a,(ld)c/d); 37 flag=1,memset(u,0,n+1); 38 for(i=1;i<=n&&flag;i++)if(!u[i])dfs(i,(ld)1.0); 39 printf("Case #%d: ",TT); 40 puts(flag?"Yes":"No"); 41 } 42 return 0; 43 } 44
时间: 2024-10-14 09:23:33