Submission #1030591

#TimeUsernameProblemLanguageResultExecution timeMemory
1030591sleepntsheepGraph (BOI20_graph)C++17
58 / 100
1022 ms18892 KiB
#include<stdio.h> #include<stdlib.h> #include<vector> #define N 100001 #define M 400001 double a(double x){return x<0?-x:x;} int n,m,head[N],fr[M],to[M],nxt[M],col[M],ii,vis[N],aa[N],bb[N],grp[N],got[N]; double xx[N]; std::vector<int> A[N]; void link(int u,int v,int c){ int i=++ii; to[i]=v; fr[i]=u; col[i]=c; nxt[i]=head[u]; head[u]=i; } void dfs(int u,int a,int b,int cp){ if(vis[u]){ if(a==aa[u]&&b!=bb[u]) exit(puts("NO")*0); return; } grp[u]=cp; A[cp].push_back(u); vis[u]=1,aa[u]=a,bb[u]=b; for(int j=head[u];j;j=nxt[j]) dfs(to[j],-a,col[j]-b,cp); } int main(){ scanf("%d%d",&n,&m); for(int u,v,c,i=0;i<m;++i)scanf("%d%d%d",&u,&v,&c),link(u,v,c),link(v,u,c); for(int i=1;i<=n;++i) if(!vis[i]) dfs(i,1,0,i); for(int j=1;j<ii;++j){ double a1=aa[fr[j]]+aa[to[j]],b1=bb[fr[j]]+bb[to[j]]; int gp=grp[fr[j]]; if(got[gp])continue; if(a1==0&&b1!=col[j])exit(puts("NO")*0); if(b1==col[j]&&a1==0)continue; int ok=1; double x = (col[j]-b1)/a1; for(int k=1;k<ii&&ok;++k){ if(grp[fr[k]]!=gp)continue; if(aa[fr[k]]*x+bb[fr[k]]+aa[to[k]]*x+bb[to[k]] != col[k])ok=0; } if(ok) xx[gp]=x,got[gp]=1; } for(int i=1;i<=n;++i){ if(got[grp[i]])continue; double min=1e18,argmin=424242; for(double f=-10000;f<=10000;f+=0.5){ double sum=0; for(int j:A[grp[i]])sum+=a(aa[j]*f+bb[j]); if(min>sum) min=sum,argmin=f; } got[i]=1;xx[grp[i]]=argmin; } puts("YES"); for(int i=1;i<=n;++i) printf("%.3f ",aa[i]*xx[grp[i]]+bb[i]); }

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     scanf("%d%d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
Graph.cpp:35:36: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |     for(int u,v,c,i=0;i<m;++i)scanf("%d%d%d",&u,&v,&c),link(u,v,c),link(v,u,c);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...