Submission #137040

#TimeUsernameProblemLanguageResultExecution timeMemory
137040thebesMousetrap (CEOI17_mousetrap)C++14
100 / 100
1678 ms185976 KiB
#include <bits/stdc++.h> using namespace std; const int MN = 1e6+6; int N, T, S, p[MN], dis[MN], dp[MN], u[MN], d[MN], m[MN], i, x, y; vector<int> adj[MN], w[MN], path; void dfs(int n,int pp,int dd){ p[n]=pp; d[n]=dd; for(auto v : adj[n]){ if(v==pp) continue; dfs(v, n, dd+1); } } void dfs2(int n,int pp){ if(n != T){ for(auto v : adj[n]) if(!u[v]&&v!=pp) dis[n]++; } for(auto v : adj[n]){ if(v != pp){ dis[v] = dis[n]; dfs2(v, n); } } } void dfs3(int n,int pp){ int a=0, b=0; for(auto v : adj[n]){ if(v==pp) continue; dfs3(v, n); if(dp[v]>a) b=a, a=dp[v]; else if(dp[v]>b) b=dp[v]; } dp[n]=max(dis[n],b); } int main(){ for(scanf("%d%d%d",&N,&T,&S),i=1;i<N;i++){ scanf("%d%d",&x,&y); adj[x].push_back(y); adj[y].push_back(x); } dfs(S, 0, 1); for(i=T;i;i=p[i]){ path.push_back(i); u[i] = 1; } dfs2(T, 0); for(auto v : path){ m[d[v]] = dis[v]; if(v == T) continue; for(auto a : adj[v]){ if(u[a]) continue; dis[a] = 0; dfs2(a, v); dfs3(a, v); w[d[v]].push_back(dp[a]); } } for(i=1;i<=N;i++){ sort(w[i].begin(),w[i].end(),[](int i,int j){return i>j;}); } int lo=dis[S], hi=N; while(lo<hi){ int mm=(lo+hi)/2; int ac=0, f=0, j, b=mm; for(i=1;i<=N;i++){ for(j=-1;j+1<w[i].size();j++){ if(w[i][j+1]+m[i]<=b) break; } ac-=j; b-=j+1; if(ac<0||b<0){ f = 1; break; } } if(f) lo=mm+1; else hi=mm; } printf("%d\n",lo); return 0; }

Compilation message (stderr)

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:71:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(j=-1;j+1<w[i].size();j++){
                      ~~~^~~~~~~~~~~~
mousetrap.cpp:41:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d%d%d",&N,&T,&S),i=1;i<N;i++){
         ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
mousetrap.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...