Submission #467572

#TimeUsernameProblemLanguageResultExecution timeMemory
467572idk321Mousetrap (CEOI17_mousetrap)C++17
0 / 100
337 ms58936 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1000005; vector<int> adj[N]; int cost[N]; void dfs(int node, int par, int height) { array<int, 2> most={0, 0}; for (int next : adj[node]) { if (next == par) continue; dfs(next, node, height + 1); if (cost[next] > most[1]) most[1] = cost[next]; if (most[1] > most[0]) swap(most[1], most[0]); } if (par != -1 && adj[node].size() == 1) { cost[node] = height; } else if (par != -1 && adj[node].size() == 2) { cost[node] = height + 1; } else { cost[node] = adj[node].size() - 2 + most[1]; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, t, m; cin >> n >> t >> m; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(t, -1, 0); cout << cost[m] << "\n"; } /* 10 1 2 1 2 2 3 2 4 3 9 3 5 4 7 4 6 6 8 7 10 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...