# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
172234 | 2019-12-31T18:27:02 Z | emil_physmath | Hard route (IZhO17_road) | C++17 | 0 ms | 0 KB |
#include <algorithm> #include <vector> #include <iostream> #include <map> using namespace std; typedef long long llong; const int maxN = 500005; vector<int> nei[maxN]; pair<int, int> ray[maxN][3]; inline ostream& operator<<(ostream& ostr, const pair<int, int>& a) { return ostr << "{" << a.first << ", " << a.second << "}"; } inline const pair<int, int>& Second(const pair<int, int>* a) { return a[0].second >= 2 ? a[0] : a[1]; } void Maximize(pair<int, int>* a, pair<int, int> x) { if (a[0].first == x.first) a[0].second += x.second; else if (a[1].first == x.first) a[1].second += x.second; else if (a[2].first == x.first) a[2].second += x.second; else if (x.first > a[2].first) { a[2] = x; sort(a, a + 3, greater<pair<int, int>>()); } } void SetDown(int v, int par) { fill(ray[v], ray[v] + 3, make_pair(0, 0)); ray[v][0] = {0, 1}; for (int to: nei[v]) if (to != par) { SetDown(to, v); Maximize(ray[v], {ray[to][0].first + 1, 1}); } } void SetUp(int v, int par) { for (int to: nei[v]) if (to != par) { // Set up[to]. if (ray[v][0].first == ray[to][0].first + 1) Maximize(ray[to], {Second(ray[v]).first + 1, 1}); else Maximize(ray[to], {ray[v][0].first + 1, 1}); SetUp(to, v); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; nei[u].push_back(v); nei[v].push_back(u); } SetDown(1, -1); /*for (int v = 1; v <= n; ++v) cerr << v << ": " << ray[v][0] << ' ' << ray[v][1] << ' ' << ray[v][2] << endl;*/ SetUp(1, -1); map<llong, llong> ans; for (int v = 1; v <= n; ++v) { // cerr << v << ": " << ray[v][0] << ' ' << ray[v][1] << ' ' << ray[v][2] << endl; vector<llong> x; vector<llong> y; for (int i = 0; i < 3; ++i) if (ray[v][i].first) { y.push_back(ray[v][i].second); for (int j = 0; j < min(3, y.back()); ++j) x.push_back(ray[v][i].first); } if (x.size() < 3) continue; int cnt = 0; if (y[0] == 1) { if (y[1] == 1) cnt = y[1] * y[2]; else cnt = (y[1] * (y[1] - 1)) / 2; } else { if (y[0] >= 3) cnt = (y[0] * (y[0] - 1)) / 2; else cnt = y[1]; } ans[x[0] * (x[1] + x[2])] += cnt; } /*for (auto it: ans) cerr << it.first << ' ' << it.second << endl;*/ if (ans.size()) cout << ans.rbegin()->first << ' ' << ans.rbegin()->second << endl; else cout << "0 1"; }