Submission #93816

#TimeUsernameProblemLanguageResultExecution timeMemory
93816Bodo171Friend (IOI14_friend)C++14
27 / 100
75 ms12024 KiB
#include "friend.h" #include <vector> #include <iostream> // Find out best sample using namespace std; const int nmax=100005; vector<int> v[nmax],ad[nmax],inloc[nmax]; int sum[nmax][2],best[nmax][2]; int a[nmax],val[nmax]; int nr,p,tip; void dfs(int x) { for(int i=0;i<v[x].size();i++) dfs(v[x][i]); for(int i=0;i<inloc[x].size();i++) dfs(inloc[x][i]); for(int i=0;i<ad[x].size();i++) dfs(ad[x][i]); nr=0; for(int i=0;i<v[x].size();i++) { a[++nr]=v[x][i]; sum[nr][0]=sum[nr-1][0]+best[a[nr]][0]; sum[nr][1]=sum[nr-1][1]+max(best[a[nr]][0],best[a[nr]][1]); } int totad=0,totinloc=0; for(int i=0;i<ad[x].size();i++) totad+=best[ad[x][i]][0]; for(int i=0;i<inloc[x].size();i++) totinloc+=best[inloc[x][i]][0]; best[x][1]=sum[nr][0]+val[x]+totad+totinloc; for(int i=0;i<ad[x].size();i++) { p=0; while(a[p]<=ad[x][i]&&p<=nr) p++; p--; best[x][1]=max(best[x][1],sum[p][0]+sum[nr][1]-sum[p][1]+best[ad[x][i]][1]+totad+totinloc-best[ad[x][i]][0]); best[x][1]=max(best[x][1],sum[nr][0]+val[x]+best[ad[x][i]][1]+totad+totinloc-best[ad[x][i]][0]); totad+=best[ad[x][i]][1]-best[ad[x][i]][0]; } totad=0; for(int i=0;i<ad[x].size();i++) totad+=best[ad[x][i]][0]; for(int i=0;i<inloc[x].size();i++) { p=0; while(a[p]<=inloc[x][i]&&p<=nr) p++; p--; best[x][1]=max(best[x][1],best[inloc[x][i]][1]+sum[nr][1]-sum[p][1]+sum[p][0]+totad+totinloc-best[inloc[x][i]][0]); } for(int i=0;i<v[x].size();i++) best[x][0]+=max(best[v[x][i]][0],best[v[x][i]][1]); for(int i=0;i<ad[x].size();i++) best[x][0]+=best[ad[x][i]][0]; for(int i=0;i<inloc[x].size();i++) best[x][0]+=best[inloc[x][i]][0]; } void add(int x,int y) { if(x==y) return; ad[x][y]=ad[y][x]=1; } int tt[nmax],rg[nmax],part[nmax]; int finds(int x) { while(x!=tt[x]) x=tt[x]; } void unite(int A,int B) { if(rg[A]>rg[B]) swap(A,B); } int findSample(int n,int confidence[],int host[],int protocol[]){ for(int i=0;i<n;i++) val[i]=confidence[i]; if(n<=10) { int j,k,ok,mx=0; for(int i=0;i<n;i++) for(j=0;j<=n;j++) ad[i].push_back(0); for(int i=1; i<n; i++) { tip=protocol[i]; if(tip==0||tip==2) { add(i,host[i]); } if(tip==1||tip==2) { for(j=0;j<n;j++) if(ad[host[i]][j]) add(i,j); } } for(int i=0;i<(1<<n);i++) { ok=1; for(int j=0;j<n;j++) for(int k=0;k<n;k++) if(((1<<j)&i)&&((1<<k)&i)&&ad[j][k]&&j!=k) ok=0; if(ok) { int act=0; for(int j=0;j<n;j++) if(((1<<j)&i)) act+=confidence[j]; mx=max(mx,act); } } return mx; } int cf=0; for(int i=1;i<n;i++) if(confidence[i]>cf) cf=confidence[i]; for(int i=1;i<n;i++) { tip=protocol[i]; if(tip==0) { v[host[i]].push_back(i); } if(tip==1) { ad[host[i]].push_back(i); } if(tip==2) { inloc[host[i]].push_back(i); } } best[0][0]=1; if(cf==1) { for(int i=1;i<n;i++) tt[i]=i,rg[i]=1; for(int i=1; i<n; i++) { tip=protocol[i]; if(tip==0||tip==2||rg[finds(host[i])]>1) unite(finds(i),finds(host[i])); if(tip==0) { part[i]=(part[host[i]]^1); best[finds(i)][part[i]]+=1; } if(tip==1) { if(rg[finds(host[i])]!=1) { part[i]=part[host[i]]; } best[finds(i)][part[i]]+=1; } if(tip==2) { part[i]=part[host[i]]; } } int wk=0; for(int i=0;i<n;i++) if(finds(i)==i) wk+=max(best[i][0],best[i][1]); return wk; } dfs(0); return max(best[0][0],best[0][1]); }

Compilation message (stderr)

friend.cpp: In function 'void dfs(int)':
friend.cpp:13:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v[x].size();i++)
                 ~^~~~~~~~~~~~
friend.cpp:15:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<inloc[x].size();i++)
                 ~^~~~~~~~~~~~~~~~
friend.cpp:17:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
friend.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v[x].size();i++)
                 ~^~~~~~~~~~~~
friend.cpp:27:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
friend.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<inloc[x].size();i++)
                 ~^~~~~~~~~~~~~~~~
friend.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
friend.cpp:43:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
friend.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<inloc[x].size();i++)
                 ~^~~~~~~~~~~~~~~~
friend.cpp:53:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v[x].size();i++)
                 ~^~~~~~~~~~~~
friend.cpp:55:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
friend.cpp:57:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<inloc[x].size();i++)
                 ~^~~~~~~~~~~~~~~~
friend.cpp: In function 'int finds(int)':
friend.cpp:71:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:81:15: warning: unused variable 'k' [-Wunused-variable]
         int j,k,ok,mx=0;
               ^
friend.cpp:166:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
         for(int i=0;i<n;i++)
         ^~~
friend.cpp:169:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
             return wk;
             ^~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...