제출 #167567

#제출 시각아이디문제언어결과실행 시간메모리
167567puppies_and_rainbowsMag (COCI16_mag)C++14
120 / 120
1716 ms173436 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int n; vector<int> adj[1000005]; int cac[1000005]; int cac2[10000005]; int a[1000005]; int ans=0, ans2=0; bool hv=false; void dfs(int node, int fa) { pair<int, int> ok={0, 0}; for(auto i:adj[node]) { if(i==fa) continue; dfs(i, node); if(cac[i]>=cac[ok.first]) { ok.second=ok.first; ok.first=i; } else if(cac[i]>cac[ok.second]) { ok.second=i; } } if(a[node]==1) { ans=max(ans, cac[ok.first]+cac[ok.second]+1); cac[node]=cac[ok.first]+1; for(auto i:adj[node]) { if(i==fa) continue; cac2[node]=max(cac2[node], cac2[i]+1); if(i==ok.first) { ans2=max(ans2, cac[ok.second]+cac2[i]+1); } else { ans2=max(ans2, cac[ok.first]+cac2[i]+1); } } } else if(a[node]==2) { ans2=max(ans2, cac[ok.first]+1); cac2[node]=cac[ok.first]+1; } ans=max(ans, cac[node]); ans2=max(ans2, cac2[node]); } signed main() { cin>>n; for(int i=1; i<n; i++) { int u, v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for(int i=1; i<=n; i++) { cin>>a[i]; if(a[i]==1) hv=true; } if(!hv) { cout<<*min_element(a+1, a+n+1)<<"/1"; return 0; } dfs(1, 1); if(ans2>2*ans) { if(ans2%2==0) { cout<<"1/"<<ans2/2; } else { cout<<"2/"<<ans2; } } else { cout<<"1/"<<ans; } }
#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...