Submission #303060

#TimeUsernameProblemLanguageResultExecution timeMemory
303060Kenzo_1114Mousetrap (CEOI17_mousetrap)C++17
45 / 100
911 ms64148 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN = 1000010; int n, t, m, dp[MAXN], par[MAXN]; vector<int> graph[MAXN]; void dfs(int v, int p) { int mx[2] = {-1, -1}; par[v] = p; for(int i = 0; i < graph[v].size(); i++) { int u = graph[v][i]; if(u == p) continue; dfs(u, v); if(dp[u] >= mx[0]) mx[1] = mx[0], mx[0] = dp[u]; else if(dp[u] > mx[1]) mx[1] = dp[u]; } dp[v] = ((int) graph[v].size() == 2) ? 1 : mx[1] + graph[v].size() - 1; if((int) graph[v].size() == 1) dp[v] = 0; } int main () { scanf("%d %d %d", &n, &t, &m); for(int i = 0, u, v; i < n - 1; i++) { scanf("%d %d", &u, &v); graph[u].push_back(v); graph[v].push_back(u); } if(t == m || (int) graph[m].size() == 0) { printf("0\n"); return 0; } dfs(t, t); int cur = m, ans = dp[m]; while(true) { cur = par[cur]; if(cur == t) break; ans += (int) graph[cur].size() - 2; } // for(int i = 1; i <= n; i++) printf("dp[%d] = %d\n", i, dp[i]); printf("%d\n", ans); } /* 10 1 4 1 2 2 3 2 4 3 9 3 5 4 7 4 6 6 8 7 10 */

Compilation message (stderr)

mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:13:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |  for(int i = 0; i < graph[v].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |  scanf("%d %d %d", &n, &t, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |   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...