# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
462051 |
2021-08-10T07:25:33 Z |
8e7 |
Hard route (IZhO17_road) |
C++17 |
|
7 ms |
11980 KB |
//Challenge: Accepted
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <utility>
#include <assert.h>
using namespace std;
void debug() {cout << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cout << a << " "; debug(b...);}
template <class T> void pary(T l, T r) {
while (l != r) {cout << *l << " ";l++;}
cout << endl;
}
#define ll long long
#define ld long double
#define maxn 500005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn];
int f[maxn][3], cnt[maxn][2], deg[maxn], up[maxn], down[maxn], g[maxn][3];
void upd(int x, int n, int y) {
if (x > f[n][0]) {
f[n][2] = f[n][1], f[n][1] = f[n][0], f[n][0] = x;
g[n][2] = g[n][1], g[n][1] = g[n][0], g[n][0] = y;
} else if (x > f[n][1]){
f[n][2] = f[n][1], f[n][1] = x;
g[n][2] = g[n][1], g[n][1] = y;
} else if (x > f[n][2]){
f[n][2] = x, g[n][2] = y;
}
}
void dfs(int n, int par) {
for (int v:adj[n]) {
if (v != par) {
dfs(v, n);
int x = f[v][0] + 1;
if (x > f[n][0]) cnt[n][0] = cnt[v][0];
else if (x == f[n][0]) cnt[n][0] += cnt[v][0];
upd(x, n, cnt[v][0]);
}
}
down[n] = f[n][0];
if (deg[n] == 1) cnt[n][0]++;
}
void dfs2(int n, int par, int d) {
upd(d, n, cnt[n][1]);
up[n] = d;
int best = d, bcnt = cnt[n][1], sec = 0, scnt = 0;
for (int v:adj[n]) {
if (v != par) {
int x = f[v][0] + 1;
if (x > best) sec = best, scnt = bcnt, best = x, bcnt = cnt[v][0];
else if (x == best) bcnt += cnt[v][0];
else if (x > sec) sec = x, scnt = cnt[v][0];
else if (x == sec) scnt += cnt[v][0];
}
}
for (int v:adj[n]) {
if (v != par) {
if (f[v][0] + 1 == best) {
if (bcnt > cnt[v][0]) cnt[v][1] = bcnt - cnt[v][0], dfs2(v, n, best + 1);
else cnt[v][1] = scnt, dfs2(v, n, sec + 1);
} else {
cnt[v][1] = bcnt;
dfs2(v, n, best + 1);
}
}
}
}
ll ans = 0, num = 0;
void solve(int n, int par) {
vector<int> arr;
for (int v:adj[n]) {
if (v != par) {
if (down[v] + 1 == f[n][2]) arr.push_back(cnt[v][0]);
}
}
if (par && up[n] == f[n][2]) arr.push_back(cnt[n][1]);
ll tot = 0, nums = 0;
for (int i:arr) tot += i;
//debug(n);
//pary(arr.begin(), arr.end());
if (f[n][0] == f[n][1] && f[n][1] == f[n][2]) {
ll p1 = 0, p2 = 0;
for (int i:arr) {
nums += p2 * i * 3;
p2 += p1 * i;
p1 += i;
}
} else if (f[n][0] == f[n][1]) {
nums += tot * g[n][0] * g[n][1] * 2;
} else if (f[n][1] == f[n][2]) {
ll p1 = 0;
for (int i:arr) nums += p1 * i * g[n][0], p1 += i;
} else {
nums += tot * g[n][0] * g[n][1];
}
tot = (ll)f[n][0] * (f[n][1] + f[n][2]);
//debug(n, tot);
if (tot > ans) ans = tot, num = nums;
else if (ans == tot) num += nums;
for (int v:adj[n]) {
if (v != par) solve(v, n);
}
}
int main() {
io
int n;
cin >> n;
for (int i = 0;i < n - 1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
deg[u]++, deg[v]++;
}
int root = 0;
for (int i = 1;i <= n;i++) {
if (deg[i] > 2) {
root = i;
break;
}
}
if (!root) {
cout << 0 << " " << 1 << endl;
return 0;
}
dfs(root, 0);
dfs2(root, 0, 0);
//debug(root);
//for (int i = 1;i <= n;i++) debug(f[i][0], f[i][1], f[i][2]);
solve(root, 0);
cout << ans << " " << num << endl;
}
/*
7
1 2
1 3
2 4
2 5
3 6
3 7
13
1 2
1 3
1 4
2 5
4 6
5 7
5 8
7 9
7 10
8 11
10 12
11 13
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |