Submission #96368

#TimeUsernameProblemLanguageResultExecution timeMemory
96368BruteforcemanMousetrap (CEOI17_mousetrap)C++11
25 / 100
1206 ms72192 KiB
#include "bits/stdc++.h" using namespace std; vector <int> g[1000010]; int dp[1000010], cn[1000010]; int dep[1000010]; int m, t; void dfs(int x, int par) { if(x == t) { cn[x] = 1; dp[x] = 0; dep[x] = 0; return ; } int deg = 0; int sib = 0; for(auto i : g[x]) { if(i - par) { dfs(i, x); cn[x] |= cn[i]; ++deg; if(cn[i]) { dep[x] = dep[i]; sib = i; } } } dep[x] += deg - 1; vector <int> can; for(auto i : g[x]) { if(i - par) { if(cn[i]) { can.emplace_back(dp[i]); } else { if(cn[x]) { can.emplace_back(dp[i] + dep[x]); } else { can.emplace_back(dp[i] + deg); } } } } sort(can.begin(), can.end()); reverse(can.begin(), can.end()); if(cn[x]) { dp[x] = dp[sib]; } else { dp[x] = 0; } if(can.size() > 1) { dp[x] = max(dp[x], can[1]); } else if (!cn[x] && can.size() == 1) { dp[x] = 1; } } bool good(int x) { int cur = m; int prv = -1; int flux = 0; while(cur != t) { ++flux; for(auto i : g[cur]) { if(!cn[i]) { if(dep[cur] + dp[i] >= x) { --flux; } } } if(flux < 0) return true; for(auto i : g[cur]) { if(cn[i] && i != prv) { prv = cur; cur = i; break; } } } return false; } int search(int b, int e) { if(b == e) { return b; } int m = (b + e + 1) >> 1; if(good(m)) { return search(m, e); } else { return search(b, m-1); } } int main(int argc, char const *argv[]) { int n; scanf("%d %d %d", &n, &t, &m); for(int i = 1; i < n; i++) { int p, q; scanf("%d %d", &p, &q); g[p].emplace_back(q); g[q].emplace_back(p); } dfs(m, 0); printf("%d\n", search(0, n)); return 0; }

Compilation message (stderr)

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