Submission #309580

#TimeUsernameProblemLanguageResultExecution timeMemory
309580biggMousetrap (CEOI17_mousetrap)C++14
45 / 100
924 ms60280 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; vector<int> grafo[MAXN]; int dp[MAXN]; pair<int, int > inserts(int x, pair<int, int> p){ if(x >= p.first) swap(p.first, p.second), p.first = x; else if(x > p.second) p.second = x; return p; } void dfs(int x, int p){ pair<int, int> par = make_pair(-1, -1); for(int i = 0; i < grafo[x].size(); i++){ int viz = grafo[x][i]; if(viz == p) continue; dfs(viz, x); par = inserts(dp[viz], par); } for(int i = 0 ; i < grafo[x].size(); i++){ int viz = grafo[x][i]; if(viz == p) swap(grafo[x][i], grafo[x].front()); } if(grafo[x].size() == 1) dp[x] = 0; else{ dp[x] = (grafo[x].size() == 2) ? 1 : par.second + grafo[x].size() - 1; } //printf("%d\n", dp[x]); } int n, t, m; int main(){ scanf("%d %d %d", &n, &t, &m); if(n == 1 || t == m){ printf("0\n"); return 0; } for(int i = 1; i < n; i++){ int u, v; scanf("%d %d", &u, &v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs(t, 0); // printf("oi\n"); int ans = dp[m]; m = grafo[m][0]; // printf("%d\n",m ); while(m != t){ //printf("%d\n", m); ans += grafo[m].size() - 2; m = grafo[m][0]; } printf("%d\n",ans); }

Compilation message (stderr)

mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:15:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |  for(int i = 0; i < grafo[x].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for(int i = 0 ; i < grafo[x].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |  scanf("%d %d %d", &n, &t, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...