Submission #308784

#TimeUsernameProblemLanguageResultExecution timeMemory
308784wmrmrMousetrap (CEOI17_mousetrap)C++17
25 / 100
947 ms64248 KiB
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6+10; int dp[MAX], pai[MAX]; int n,t,m; vector<int> g[MAX]; void Quick(int v, int p) { pai[v] = p; for(int i=0;i<g[v].size();i++) { int prox = g[v][i]; if(prox == p) continue; Quick(prox,v); } } void DFS1(int v, int p) { int grau = g[v].size(); pair<int,int> mx; mx = {0,0}; if(grau==2){ dp[v] = 1; return; } for(int i=0;i<grau;i++) { int prox = g[v][i]; if(prox == p) continue; DFS1(prox,v); if(dp[prox] >= mx.first) mx.second = mx.first, mx.first = dp[prox]; else { if(dp[prox] > mx.second) mx.second = dp[prox]; } } dp[v] = grau + mx.second - 1; } void DFS2(int v, int f, int ans) { if(v == t) { printf("%d",ans); return; } if(f == 0) { DFS2(pai[v],v,dp[v]); return; } int grau = g[v].size(); if(grau == 3) { DFS2(pai[v],v,ans+1); return; } pair<int,int> mx; mx = {0,0}; for(int i=0;i<g[v].size();i++) { int prox = g[v][i]; if(prox == f || prox == pai[v]) continue; if(dp[prox] >= mx.first) mx.second = mx.first, mx.first = dp[prox]; else { if(dp[prox] > mx.second) mx.second = dp[prox]; } } DFS2(pai[v],v,ans+grau+mx.second-2); } int main() { scanf("%d %d %d",&n,&t,&m); for(int i=1;i<n;i++) { int a,b; scanf("%d %d",&a,&b); g[a].push_back(b); g[b].push_back(a); } Quick(t,0); int cur = m; while(pai[cur] != t) cur = pai[cur]; DFS1(cur,t); DFS2(m,0,0); }

Compilation message (stderr)

mousetrap.cpp: In function 'void Quick(int, int)':
mousetrap.cpp:11:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |  for(int i=0;i<g[v].size();i++)
      |              ~^~~~~~~~~~~~
mousetrap.cpp: In function 'void DFS2(int, int, int)':
mousetrap.cpp:50:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |  for(int i=0;i<g[v].size();i++)
      |              ~^~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |  scanf("%d %d %d",&n,&t,&m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |   int a,b; 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...