Submission #569507

#TimeUsernameProblemLanguageResultExecution timeMemory
569507Waratpp123Torrent (COI16_torrent)C++14
31 / 100
2073 ms21772 KiB
#include<bits/stdc++.h> using namespace std; vector<int> g[300010],tr; int a,b,par[300010],mark[300010],dp[300010]; int dfs1(int i,int p){ if(i==b) return 1; for(auto x : g[i]){ if(x==p) continue; int opr=dfs1(x,i); if(opr==1){ tr.push_back(x); return 1; } } return 0; } void setpar(int i,int p){ par[i]=p; for(auto x : g[i]){ if(x==p) continue; setpar(x,i); } } int dfs(int i,int p){ vector<int> t; t.clear(); for(auto x : g[i]){ if(x==p||mark[x]==1) continue; dfs(x,i); t.push_back(dp[x]); } int mx=0,n=t.size(); sort(t.begin(),t.end()); for(int j=0;j<n;j++){ mx=max(mx,t[j]+(n-j)); } return dp[i]=mx; } int main(){ int i,j,n,u,v; scanf("%d %d %d",&n,&a,&b); for(i=1;i<=n-1;i++){ scanf("%d %d",&u,&v); g[u].push_back(v); g[v].push_back(u); } dfs1(a,-1); setpar(a,-1); int mid; int ans=n; for(mid=0;mid<tr.size();mid++){ mark[tr[mid]]=1; int suma=dfs(a,-1); mark[tr[mid]]=0; mark[par[tr[mid]]]=1; int sumb=dfs(b,-1); mark[par[tr[mid]]]=0; ans=min(ans,max(suma,sumb)); } printf("%d\n",ans); return 0; }

Compilation message (stderr)

torrent.cpp: In function 'int main()':
torrent.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(mid=0;mid<tr.size();mid++){
      |               ~~~^~~~~~~~~~
torrent.cpp:40:11: warning: unused variable 'j' [-Wunused-variable]
   40 |     int i,j,n,u,v;
      |           ^
torrent.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     scanf("%d %d %d",&n,&a,&b);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
torrent.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         scanf("%d %d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...