제출 #1116782

#제출 시각아이디문제언어결과실행 시간메모리
1116782kfhjadHard route (IZhO17_road)C++17
0 / 100
4 ms13816 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<int> adj[500001]; int up[500001], down[500001]; ll ans = 0; int freq = 1; void update(ll x) { if (x > ans) ans = x, freq = 1; else if (x == ans) ++freq; } void dfs(int node, int p) { for (int i : adj[node]) { if (i == p) continue; dfs(i, node); down[node] = max(down[node], down[i] + 1); } } void dfs1(int node, int p) { ll m1 = 0, m2 = 0, m3 = 0; for (int i : adj[node]) { if (i == p) continue; if (down[i] + 1 > m3) m3 = down[i] + 1; if (m3 > m2) swap(m3, m2); if (m2 > m1) swap(m2, m1); } if (up[node] > m3) m3 = up[node]; if (m3 > m2) swap(m3, m2); if (m2 > m1) swap(m2, m1); for (int i : adj[node]) { if (i == p) continue; if (down[i] + 1 == m1) up[i] = max((ll)up[node], m2) + 1; else up[i] = max((ll)up[node], m1) + 1; if (adj[node].size() > 2) { ll path = down[i] + 1; if (path == m1) update((m2 + m3) * path); else if (path == m2) update((m1 + m3) * path); else update((m1 + m2) * path); } dfs1(i, node); } if (adj[node].size() > 2) { ll path = up[node]; if (path == m1) update((m2 + m3) * path); else if (path == m2) update((m1 + m3) * path); else update((m1 + m2) * path); } } int main() { cin.tie(NULL) -> sync_with_stdio(false); int N; cin >> N; while (--N) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1, 0); dfs1(1, 0); cout << ans << ' ' << freq << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...