#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<pair<int, int>>> res(n);
{
vector<bool> vis(n);
function<pair<int, int>(int)> dfs = [&](int u) -> pair<int, int>
{
res[u].resize(g[u].size());
vis[u] = 1;
pair<int, int> ret(0, 1);
for (int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (vis[v])
{
continue;
}
res[u][i] = dfs(v);
if (res[u][i].first > ret.first)
{
ret = res[u][i];
}
else if (res[u][i].first == ret.first)
{
ret.second += res[u][i].second;
}
}
ret.first++;
return ret;
};
dfs(0);
}
long long ans = 0, cnt = 1;
auto update = [&](long long x, long long y)
{
if (y == 0)
{
return;
}
if (ans < x)
{
ans = x;
cnt = y;
}
else if (ans == x)
{
cnt += y;
}
};
{
vector<bool> vis(n);
function<void(int, pair<int, int>)> dfs = [&](int u, pair<int, int> cur)
{
vis[u] = 1;
multiset<pair<int, int>> st;
for (int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (vis[v])
{
continue;
}
st.insert(res[u][i]);
}
if (u != 0)
{
st.insert(cur);
}
if (st.size() >= 3)
{
auto it = --st.end();
auto it3 = it--;
auto it2 = it--;
if (it->first == it3->first)
{
update((it->first + it2->first) * it3->first, 1ll * it->second * it2->second + 1ll * it->second * it3->second + 1ll * it2->second * it3->second);
}
else if (it2->first == it3->first)
{
update((it->first + it2->first) * it3->first, 1ll * it->second * it2->second + 1ll * it->second * it3->second);
}
else
{
update((it->first + it2->first) * it3->first, 1ll * it->second * it2->second);
}
}
map<int, int> mp;
for (int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (vis[v])
{
continue;
}
mp[res[u][i].first] += res[u][i].second;
}
mp[cur.first] += cur.second;
for (int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (vis[v])
{
continue;
}
mp[res[u][i].first] -= res[u][i].second;
if ((--mp.end())->second == 0)
{
mp.erase(--mp.end());
}
pair<int, int> x = *(--mp.end());
x.first++;
dfs(v, x);
mp[res[u][i].first] += res[u][i].second;
}
};
dfs(0, make_pair(0, 1));
}
cout << ans << ' ' << cnt << '\n';
return 0;
}
Compilation message
road.cpp: In lambda function:
road.cpp:27:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
road.cpp: In lambda function:
road.cpp:72:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
road.cpp:104:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
road.cpp:114:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |