Submission #284304

#TimeUsernameProblemLanguageResultExecution timeMemory
284304BeanZMousetrap (CEOI17_mousetrap)C++14
45 / 100
1143 ms70184 KiB
#include <bits/stdc++.h> using namespace std; #define ll int #define endl '\n' const int N = 3e6 + 5; const int mod = 998244353; ll n, t, m, ban = 0; vector<ll> node[1000006]; ll dp[1000006], par[1000006], f[1000006]; void dfsFirst(ll u, ll v){ par[u] = v; for (auto j : node[u]){ if (j == v) continue; dfsFirst(j, u); f[u] |= f[j]; } if (u == m) f[u] = 1; } void dfs(ll u, ll p, ll cnt){ dp[u] = 0; vector<ll> val; for (auto j : node[u]){ if (j == p || j == ban) continue; dfs(j, u, 1); val.push_back(dp[j] + 1); } if (val.size()){ sort(val.begin(), val.end()); dp[u] += min((ll)val.size(), cnt); if (val.size() > cnt) dp[u] += val[val.size() - cnt - 1]; dp[u] += max(0, (ll)val.size() - cnt - 1); //cout << u << " " << dp[u] << endl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); if (fopen("MOUSETRAP.INP", "r")){ freopen("MOUSETRAP.INP", "r", stdin); freopen("MOUSETRAP.OUT", "w", stdout); } cin >> n >> t >> m; for (int i = 1; i < n; i++){ ll u, v; cin >> u >> v; node[u].push_back(v); node[v].push_back(u); } dfsFirst(t, t); ll ans = 0; ll cnt = 0; while (true){ if (m == t) break; ll preans = 0; dfs(m, par[m], cnt + 1); ll cur = par[m]; preans = dp[m]; while (cur != t){ preans = preans + node[cur].size() - 2; cur = par[cur]; } //cout << preans << " " << dp[m] << endl; ans = max(ans, preans - (cnt > 0)); cnt++; ban = m; m = par[m]; } cout << ans; } /* 13 13 1 1 2 2 3 2 4 1 5 5 6 5 7 1 8 8 9 8 10 1 11 11 12 11 13 10 1 4 1 2 2 3 2 4 3 9 3 5 4 7 4 6 6 8 7 10 14 4 1 1 2 2 5 2 6 5 7 5 8 8 10 6 9 1 3 3 14 3 11 11 12 11 13 1 4 12 12 1 1 2 1 3 3 4 4 5 4 6 3 7 7 8 7 9 3 10 10 11 10 12 */

Compilation message (stderr)

mousetrap.cpp: In function 'void dfs(int, int, int)':
mousetrap.cpp:32:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   32 |                 if (val.size() > cnt) dp[u] += val[val.size() - cnt - 1];
      |                     ~~~~~~~~~~~^~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:41:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   41 |                 freopen("MOUSETRAP.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:42:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   42 |                 freopen("MOUSETRAP.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...