# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
28745 | 2017-07-16T12:48:40 Z | samir_droubi | Torrent (COI16_torrent) | C++14 | 53 ms | 11244 KB |
#include <bits/stdc++.h> using namespace std; int n; const int mxn=(1e5)+5; int a,b; vector<int>tr[mxn]; int dep[mxn]; int pa[mxn]; bool path[mxn]; void dfs(int v,int p) { for(int i=0;i<tr[v].size();++i) { int u=tr[v][i]; if(u==p)continue; dep[u]=dep[v]+1; pa[u]=v; dfs(u,v); } } void lca() { int x=a; int y=b; path[x]=true; path[y]=true; if(dep[x]>dep[y])swap(y,x); while(dep[y]!=dep[x]) { y=pa[y]; path[y]=true; } if(x==y)return; while(pa[x]!=pa[y]) { x=pa[x]; y=pa[y]; path[x]=true; path[y]=true; } path[pa[x]]=true; } int dp[mxn]; bool cmp(int x,int y) { return dp[x]>dp[y]; } void dfs1(int v,int p) { dp[v]=0; for(int i=0;i<tr[v].size();++i) { int u=tr[v][i]; if(u==p||path[u])continue; dfs1(u,v); } sort(tr[v].begin(),tr[v].end(),cmp); int c=0; for(int i=0;i<tr[v].size();++i) { int u=tr[v][i]; if(u==p||path[u])continue; ++c; dp[u]+=c; dp[v]=max(dp[v],dp[u]); } } vector<int>vv[mxn]; bool explored[mxn]; bool check(int T) { memset(explored,0,sizeof explored); for(int i=0;i<mxn;++i)vv[i].clear(); vv[0].push_back(a); vv[0].push_back(b); for(int i=0;i<T;++i) { for(int j=0;j<vv[i].size();++j) { int v=vv[i][j]; if(explored[v])continue; explored[v]=true; if(dp[v]+i>T)return false; int del=1; int node=-1; for(int k=0;k<tr[v].size();++k) { int u=tr[v][k]; if(path[u]) { if(!explored[u])node=u; continue; } if(dp[u]+i==T)++del; } if(node==-1)continue; if(del+i>=T)continue; vv[del+i].push_back(node); } } bool ok=true; for(int i=1;i<=n;++i) if(path[i]&&!explored[i])ok=false; return ok; } void bs() { int l=1; int r=n; int ans=-1; while(l<=r) { int md=(l+r)/2; if(check(md)) { ans=md; r=md-1; } else l=md+1; } printf("%d\n",ans); } int main() { scanf("%d%d%d",&n,&a,&b); for(int i=0;i<n-1;++i) { int x,y; scanf("%d%d",&x,&y); tr[x].push_back(y); tr[y].push_back(x); } dfs(1,1); lca(); for(int i=1;i<=n;++i) { if(!path[i])continue; dfs1(i,i); } bs(); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 8076 KB | Output is correct |
2 | Correct | 3 ms | 8076 KB | Output is correct |
3 | Correct | 3 ms | 8076 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 53 ms | 11244 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |