This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 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 (down[i] + 1 == m1 && adj[node].size() > 2)
{
ll x = (m2 + m3) * m1;
if (x > ans)
ans = x, freq = 1;
else if (x == ans)
++freq;
}
dfs1(i, node);
}
if (up[node] == m1 && adj[node].size() > 2 && node != 1)
{
ll x = (m2 + m3) * m1;
if (x > ans)
ans = x, freq = 1;
else if (x == ans)
++freq;
}
// cout << m1 << ' ' << m2 << ' ' << m3 << endl;
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |