Submission #340426

#TimeUsernameProblemLanguageResultExecution timeMemory
340426SprdaloHard route (IZhO17_road)C++17
52 / 100
552 ms1132 KiB
#include <bits/stdc++.h> using namespace std; #define int ll typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<double> vd; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<pi> vp; typedef vector<pl> vpl; const int maxn = 5005; vi e[maxn], d(maxn); void dfs(int x, int st){ d[x] = 0; for (int i : e[x]){ if (i != st){ dfs(i,x); d[x] = max(d[x], d[i]); } } ++d[x]; } int sol, cnt; void calc(int x, int st=0, int dij=0, int ds=0){ int len = e[x].size(); if (len == 1 && st){ int res =ds*dij; if (sol==res) ++cnt; if (sol < res){ sol =res; cnt=1; } } int m = 0, y = 0; for (int i : e[x]){ if (i == st) continue; if (d[i] >= m){ swap(m,y); m=d[i]; } else if (d[i] >=y){ y=d[i]; } } for (int i : e[x]){ if (i == st) continue; if (m == d[i]) calc(i,x,dij+1,max(ds,y)); else calc(i,x,dij+1,max(ds,m)); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 1; i < n; ++i){ int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } int tt = 0; for (int i = 1; i <= n; ++i){ int len = e[i].size(); tt += (len == 1); } if (tt == 2) return cout << "0 1\n", 0; for (int i = 1; i <= n; ++i){ int len = e[i].size(); if (len > 1) continue; dfs(i,i); calc(i); } cout << sol << ' ' << cnt/2 << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...