Submission #997623

#TimeUsernameProblemLanguageResultExecution timeMemory
997623fuad27Hard route (IZhO17_road)C++17
0 / 100
3 ms13660 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 500100; vector<int> g[N]; int n; pair<int,int> dp[N]; pair<long long, long long> ans={0, 1}; void dfs1(int at, int p) { dp[at] = {1, 1}; for(int to:g[at]) { if(to==p)continue; dfs1(to, at); if(dp[to].first+1 > dp[at].first) { dp[at]=dp[to]; dp[at].first++; } else if(dp[to].first+1 == dp[at].first) { dp[at].second+=dp[to].second; } } } void dfs2(int at, int p) { sort(g[at].begin(), g[at].end(), [&](int u, int v) { return dp[u].first > dp[v].first; }); if(g[at].size()>=3) { long long a = dp[g[at][0]].first, b = dp[g[at][1]].first, c = dp[g[at][2]].first; long long res = a*(b+c); long long cntb = 0, cntc = 0, totcnt=0; for(int to:g[at]) { if(b == dp[to].first) cntb++; if(c == dp[to].first) cntc++; } if(b == c) { totcnt = cntb*(cntb-1)/2; } else { totcnt = cntb*cntc; } if(ans.first == res)ans.second+=totcnt; else if(ans.first < res) { ans.first = res; ans.second = totcnt; } } pair<long long, long long> fr = {1, 1}; pair<long long, long long> sc = {1, 1}; for(int to:g[at]) { if(fr.first < dp[to].first+1) { fr=dp[to]; fr.first++; } else if(fr.first == dp[to].first+1) { fr.second+=dp[to].second; } } for(int to:g[at]) { if(dp[to].first+1==fr.first)continue; if(sc.first < dp[to].first+1) { sc=dp[to]; sc.first++; } else if(sc.first == dp[to].first+1) { sc.second+=dp[to].second; } } for(int to:g[at]) { if(to==p)continue; if(fr.first == dp[to].first+1 and fr.second == dp[to].second) { dp[at] = sc; } else if(fr.first == dp[to].first+1) { dp[at] = fr; dp[at].second -= dp[to].second; } else { dp[at] = fr; } dfs2(to, at); } } signed main () { cin.tie(0)->sync_with_stdio(0); cin >> n; for(int i = 1;i<n;i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs1(1, 0); dfs2(1, 0); cout << ans.first << " " << ans.second << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...