#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);
}
int root = n - rand() % n;
SetDown(root, -1);
/*for (int v = 1; v <= n; ++v)
cerr << v << ": " << ray[v][0] << ' ' << ray[v][1] << ' ' << ray[v][2] << endl;*/
SetUp(root, -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<pair<llong, int>> x;
for (int i = 0; i < 3; ++i)
if (ray[v][i].first)
{
for (int j = 0; j < min(3, ray[v][i].second); ++j)
x.push_back({ray[v][i].first, min(3, ray[v][i].second)});
}
if (x.size() < 3) continue;
int cnt = 0;
if (x[1].first == x[2].first)
cnt = (x[1].second * (x[1].second - 1LL)) / 2LL;
else
cnt = x[1].second * (llong)x[2].second;
ans[x[0].first * (x[1].first + x[2].first)] += cnt;
}
/*for (auto it: ans)
cerr << it.first << ' ' << it.second << endl;*/
if (ans.size())
cout << ans.rbegin()->first << ' ' << ans.rbegin()->second;
else
cout << "0 1";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12124 KB |
Output is correct |
3 |
Correct |
12 ms |
12152 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12024 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Incorrect |
12 ms |
12024 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12124 KB |
Output is correct |
3 |
Correct |
12 ms |
12152 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12024 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Incorrect |
12 ms |
12024 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12124 KB |
Output is correct |
3 |
Correct |
12 ms |
12152 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12024 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Incorrect |
12 ms |
12024 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |