Submission #463578

#TimeUsernameProblemLanguageResultExecution timeMemory
463578vanicMousetrap (CEOI17_mousetrap)C++14
25 / 100
1022 ms65092 KiB
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> using namespace std; const int maxn=1e6+5; vector < int > ms[maxn]; int gore[maxn]; bool bio[maxn]; int dp[maxn]; void dfs(int x, int prosl){ int br=0; for(int i=0; i<(int)ms[x].size(); i++){ if(bio[ms[x][i]]){ br++; } } dp[x]=ms[x].size()-1-br; int maksi=0, drugi=0; for(int i=0; i<(int)ms[x].size(); i++){ if(ms[x][i]!=prosl && !bio[ms[x][i]]){ dfs(ms[x][i], x); if(dp[ms[x][i]]>maksi){ drugi=maksi; maksi=dp[ms[x][i]]; } else if(dp[ms[x][i]]>drugi){ drugi=dp[ms[x][i]]; } } } bio[x]=1; dp[x]+=drugi; } void rokaj(int x, int prosl){ gore[x]=prosl; for(int i=0; i<(int)ms[x].size(); i++){ if(ms[x][i]!=prosl){ rokaj(ms[x][i], x); } } } int val[maxn]; int pref[maxn]; int main(){ int n, t, m; scanf("%d%d%d", &n, &t, &m); t--; m--; int a, b; for(int i=0; i<n-1; i++){ scanf("%d%d", &a, &b); a--; b--; ms[a].push_back(b); ms[b].push_back(a); } if(n==1){ printf("0\n"); return 0; } rokaj(t, t); int sol=0; int x=m; int br=0; while(gore[x]!=x){ br++; x=gore[x]; } x=m; int uk=br; while(gore[x]!=x){ br--; val[br]=(int)ms[x].size()-2; x=gore[x]; } for(int i=0; i<uk; i++){ pref[i+1]=pref[i]+val[i]; } x=m; while(gore[x]!=x){ dfs(x, gore[x]); uk--; sol=max(dp[x]+pref[uk], sol); x=gore[x]; } printf("%d\n", sol); return 0; }

Compilation message (stderr)

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d%d%d", &n, &t, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...