Submission #462064

# Submission time Handle Problem Language Result Execution time Memory
462064 2021-08-10T07:33:58 Z 8e7 Hard route (IZhO17_road) C++17
100 / 100
888 ms 91380 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][2]) {
		if (f[n][0] == f[n][1] && f[n][1] == f[n][2]) {
			ll p1 = 0;
			for (int i:arr) {
				nums += p1 * i;
				p1 += i;
			}
		} else if (f[n][0] == f[n][1]) {
			nums += tot * (g[n][1] + g[n][0]);
		} else if (f[n][1] == f[n][2]) {
			ll p1 = 0;
			for (int i:arr) nums += p1 * i, p1 += i;
		} else {
			nums += tot * g[n][1];
		}
		tot = (ll)f[n][0] * (f[n][1] + f[n][2]);
		//debug(n, tot, nums);
		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 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 8 ms 12048 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 7 ms 12012 KB Output is correct
10 Correct 7 ms 12060 KB Output is correct
11 Correct 7 ms 12004 KB Output is correct
12 Correct 7 ms 12108 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12028 KB Output is correct
19 Correct 7 ms 12064 KB Output is correct
20 Correct 7 ms 12108 KB Output is correct
21 Correct 8 ms 11992 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 12060 KB Output is correct
24 Correct 7 ms 12008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 8 ms 12048 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 7 ms 12012 KB Output is correct
10 Correct 7 ms 12060 KB Output is correct
11 Correct 7 ms 12004 KB Output is correct
12 Correct 7 ms 12108 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12028 KB Output is correct
19 Correct 7 ms 12064 KB Output is correct
20 Correct 7 ms 12108 KB Output is correct
21 Correct 8 ms 11992 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 12060 KB Output is correct
24 Correct 7 ms 12008 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 10 ms 12748 KB Output is correct
27 Correct 9 ms 12748 KB Output is correct
28 Correct 10 ms 12692 KB Output is correct
29 Correct 10 ms 12728 KB Output is correct
30 Correct 9 ms 12712 KB Output is correct
31 Correct 10 ms 12692 KB Output is correct
32 Correct 9 ms 12716 KB Output is correct
33 Correct 10 ms 12876 KB Output is correct
34 Correct 12 ms 12748 KB Output is correct
35 Correct 10 ms 12744 KB Output is correct
36 Correct 12 ms 12736 KB Output is correct
37 Correct 9 ms 12204 KB Output is correct
38 Correct 9 ms 12204 KB Output is correct
39 Correct 10 ms 12620 KB Output is correct
40 Correct 10 ms 12492 KB Output is correct
41 Correct 10 ms 12492 KB Output is correct
42 Correct 10 ms 12492 KB Output is correct
43 Correct 9 ms 12492 KB Output is correct
44 Correct 11 ms 12620 KB Output is correct
45 Correct 9 ms 12364 KB Output is correct
46 Correct 9 ms 12364 KB Output is correct
47 Correct 11 ms 12464 KB Output is correct
48 Correct 9 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 8 ms 12048 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 8 ms 11980 KB Output is correct
9 Correct 7 ms 12012 KB Output is correct
10 Correct 7 ms 12060 KB Output is correct
11 Correct 7 ms 12004 KB Output is correct
12 Correct 7 ms 12108 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12028 KB Output is correct
19 Correct 7 ms 12064 KB Output is correct
20 Correct 7 ms 12108 KB Output is correct
21 Correct 8 ms 11992 KB Output is correct
22 Correct 7 ms 12060 KB Output is correct
23 Correct 7 ms 12060 KB Output is correct
24 Correct 7 ms 12008 KB Output is correct
25 Correct 10 ms 12748 KB Output is correct
26 Correct 10 ms 12748 KB Output is correct
27 Correct 9 ms 12748 KB Output is correct
28 Correct 10 ms 12692 KB Output is correct
29 Correct 10 ms 12728 KB Output is correct
30 Correct 9 ms 12712 KB Output is correct
31 Correct 10 ms 12692 KB Output is correct
32 Correct 9 ms 12716 KB Output is correct
33 Correct 10 ms 12876 KB Output is correct
34 Correct 12 ms 12748 KB Output is correct
35 Correct 10 ms 12744 KB Output is correct
36 Correct 12 ms 12736 KB Output is correct
37 Correct 9 ms 12204 KB Output is correct
38 Correct 9 ms 12204 KB Output is correct
39 Correct 10 ms 12620 KB Output is correct
40 Correct 10 ms 12492 KB Output is correct
41 Correct 10 ms 12492 KB Output is correct
42 Correct 10 ms 12492 KB Output is correct
43 Correct 9 ms 12492 KB Output is correct
44 Correct 11 ms 12620 KB Output is correct
45 Correct 9 ms 12364 KB Output is correct
46 Correct 9 ms 12364 KB Output is correct
47 Correct 11 ms 12464 KB Output is correct
48 Correct 9 ms 12492 KB Output is correct
49 Correct 682 ms 84040 KB Output is correct
50 Correct 706 ms 84000 KB Output is correct
51 Correct 673 ms 83996 KB Output is correct
52 Correct 667 ms 84148 KB Output is correct
53 Correct 584 ms 91380 KB Output is correct
54 Correct 605 ms 88588 KB Output is correct
55 Correct 607 ms 80792 KB Output is correct
56 Correct 605 ms 74012 KB Output is correct
57 Correct 568 ms 83812 KB Output is correct
58 Correct 539 ms 83740 KB Output is correct
59 Correct 523 ms 83840 KB Output is correct
60 Correct 552 ms 83716 KB Output is correct
61 Correct 426 ms 36812 KB Output is correct
62 Correct 440 ms 36768 KB Output is correct
63 Correct 847 ms 67300 KB Output is correct
64 Correct 829 ms 61888 KB Output is correct
65 Correct 846 ms 59112 KB Output is correct
66 Correct 826 ms 57436 KB Output is correct
67 Correct 888 ms 56768 KB Output is correct
68 Correct 827 ms 56648 KB Output is correct
69 Correct 836 ms 56512 KB Output is correct
70 Correct 818 ms 56320 KB Output is correct
71 Correct 843 ms 56192 KB Output is correct
72 Correct 821 ms 56332 KB Output is correct
73 Correct 866 ms 56612 KB Output is correct
74 Correct 794 ms 56652 KB Output is correct
75 Correct 843 ms 56928 KB Output is correct
76 Correct 798 ms 57444 KB Output is correct
77 Correct 697 ms 58684 KB Output is correct
78 Correct 436 ms 61412 KB Output is correct