Submission #354180

#TimeUsernameProblemLanguageResultExecution timeMemory
354180valerikkMousetrap (CEOI17_mousetrap)C++17
0 / 100
450 ms80288 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; int n, t, m; vector <int> g[N]; int par[N]; int sum[N]; int w[N]; void dfs(int v, int p = -1) { par[v] = p; vector <int> gg; for (int u : g[v]) { if (u != p) { gg.push_back(u); dfs(u, v); } } g[v].swap(gg); } void zhfs(int v) { for (int u : g[v]) { sum[u] = sum[v] + (int) g[v].size(); zhfs(u); } } void wfs(int v) { if (g[v].size() <= 1) w[v] = sum[v]; vector<int> have; for (int u : g[v]) { wfs(u); have.push_back(w[u]); } sort(have.rbegin(), have.rend()); if (have.size() >= 2) w[v] = have[1]; } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cin >> n >> t >> m; --t; --m; for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } dfs(t); zhfs(t); wfs(t); cout << w[m] << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...