Submission #167652

#TimeUsernameProblemLanguageResultExecution timeMemory
167652HungAnhGoldIBO2020Mag (COCI16_mag)C++14
120 / 120
607 ms173080 KiB
#include<bits/stdc++.h> using namespace std; const int N=1e6+2; const int inf=1e9+7; vector<int> adj[N]; int val[N],f[N],max1=1; pair<int,int> nei[N],nei1[N]; void dfs1(int x,int p,int mx){ for(int i:adj[x]){ if(i!=p){ if(val[x]==1){ int val=-1; if(i!=nei[x].second){ val=nei[x].first+1; } else{ val=nei1[x].first+1; } dfs1(i,x,max(mx+1,val)); } else{ dfs1(i,x,0); } } } // cout<<x<<' '<<nei[x].first<<' '<<nei[x].second<<' '<<mx<<' '<<nei1[x].first<<' '<<nei1[x].second<<endl; if(val[x]==2&&nei[x].first==max1&&(nei1[x].first==max1||mx==max1)){ cout<<2<<'/'<<2*max1+1; exit(0); } } void dfs(int x,int p){ int mx=0; if(val[x]==1){ f[x]=1; } for(int i:adj[x]){ if(i!=p){ dfs(i,x); if(nei[x].first<=f[i]){ nei1[x]=nei[x]; nei[x]={f[i],i}; } else{ if(nei1[x].first<f[i]){ nei1[x]={f[i],i}; } } if(val[i]>1||val[x]>1){ continue; } if(f[x]<=f[i]+1){ mx=f[x]; f[x]=f[i]+1; } else{ if(f[i]+1>mx){ mx=f[i]+1; } } } } max1=max(max1,mx+f[x]-1); } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n,i,j,k,l,min1=inf; cin>>n; for(i=1;i<n;i++){ cin>>j>>k; adj[j].push_back(k); adj[k].push_back(j); } for(i=1;i<=n;i++){ cin>>val[i]; min1=min(min1,val[i]); } if(min1>1){ cout<<min1<<'/'<<1; return 0; } dfs(1,1); // for(i=1;i<=n;i++){ // cout<<i<<' '<<f[i]<<endl; // } dfs1(1,1,0); cout<<1<<'/'<<max1; } /* 5 1 2 2 4 1 3 5 2 2 1 1 1 3 */ /* 5 1 2 2 3 3 4 4 5 1 1 2 1 1 */

Compilation message (stderr)

mag.cpp: In function 'int main()':
mag.cpp:68:14: warning: unused variable 'l' [-Wunused-variable]
  int n,i,j,k,l,min1=inf;
              ^
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...