Submission #935808

# Submission time Handle Problem Language Result Execution time Memory
935808 2024-02-29T14:34:24 Z BF001 Hard route (IZhO17_road) C++17
100 / 100
643 ms 104948 KB
#include <bits/stdc++.h>
using namespace std;

#define N 500005
#define int long long
#define fi first
#define se second
typedef pair<int, int> ii;

int n, dp[N], cnt[N], res = 0, way = 1;
vector<int> adj[N];

void init(int u, int p){
	cnt[u] = 1;
	for (auto x : adj[u]){
		if (x == p) continue;
		init(x, u);
		if (dp[x] + 1 == dp[u]){
			cnt[u] += cnt[x];
		}
		if (dp[x] + 1 > dp[u]) cnt[u] = cnt[x];
		dp[u] = max(dp[u], dp[x] + 1);
	}
}

bool cmp (int& i, int& j){
	return dp[i] > dp[j];
}

void rr(int u, int p){
	sort(adj[u].begin(), adj[u].end(), cmp);
	if (adj[u].size() >= 3){
		int cur = 0, cn = 0;
		int a = dp[adj[u][0]] + 1, b = dp[adj[u][1]] + 1, c = dp[adj[u][2]] + 1;
		cur = a * (b + c);
		int ca = 0;
		for (auto x : adj[u]){
			if (dp[x] + 1 == c) cn += ca * cnt[x];
			if (dp[x] + 1 == b) ca += cnt[x];
		}
		if (cur > res) res = cur, way = cn;
		else if (cur == res) way += cn;
		//cout << u << ' ' << " " << cur << " " << way << "\n";
	}
	ii f = {0, 1}, s = {0, 0};
	for (auto x : adj[u]){
		if (dp[x] + 1 > f.fi) s = f, f = {dp[x] + 1, cnt[x]};
		else if (dp[x] + 1 == f.fi) f.se += cnt[x];
		else if (dp[x] + 1 > s.fi) s = {dp[x] + 1, cnt[x]};
		else if (dp[x] + 1 == s.fi) s.se += cnt[x];
	}
	int tmpdp = dp[u], tmpcnt = cnt[u];
	for (auto x : adj[u]){
		if (x == p) continue;
		dp[u] = f.fi, cnt[u] = f.se;
		if (dp[x] + 1 == f.fi && cnt[x] == f.se) dp[u] = s.fi, cnt[u] = s.se;
		else if (dp[x] + 1 == f.fi && cnt[x] != f.se) dp[u] = f.fi, cnt[u] = f.se - cnt[x];
		rr(x, u);
	}
	dp[u] = tmpdp, cnt[u] = tmpcnt;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
      
    cin >> n;
    for (int i = 1; i <= n - 1; i++){
    	int u, v;
    	cin >> u >> v;
    	adj[u].push_back(v);
    	adj[v].push_back(u);
    }

    init(1, 0);
    rr(1, 0);

    cout << res << " " << way;

    return 0;
}     
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 5 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12956 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12892 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12968 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 4 ms 13148 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12916 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12900 KB Output is correct
21 Correct 4 ms 12908 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12956 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 5 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12956 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12892 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12968 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 4 ms 13148 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12916 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12900 KB Output is correct
21 Correct 4 ms 12908 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12956 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 4 ms 13144 KB Output is correct
26 Correct 5 ms 13404 KB Output is correct
27 Correct 4 ms 13404 KB Output is correct
28 Correct 5 ms 13404 KB Output is correct
29 Correct 4 ms 13404 KB Output is correct
30 Correct 4 ms 13424 KB Output is correct
31 Correct 5 ms 13404 KB Output is correct
32 Correct 4 ms 13400 KB Output is correct
33 Correct 4 ms 13400 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 5 ms 13404 KB Output is correct
36 Correct 4 ms 13404 KB Output is correct
37 Correct 5 ms 13404 KB Output is correct
38 Correct 5 ms 13732 KB Output is correct
39 Correct 4 ms 13404 KB Output is correct
40 Correct 5 ms 13220 KB Output is correct
41 Correct 4 ms 13224 KB Output is correct
42 Correct 4 ms 12984 KB Output is correct
43 Correct 6 ms 13212 KB Output is correct
44 Correct 5 ms 13148 KB Output is correct
45 Correct 4 ms 13148 KB Output is correct
46 Correct 4 ms 13152 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 4 ms 13068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 5 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12956 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12892 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 3 ms 12968 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 4 ms 13148 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12916 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12900 KB Output is correct
21 Correct 4 ms 12908 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12956 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 4 ms 13144 KB Output is correct
26 Correct 5 ms 13404 KB Output is correct
27 Correct 4 ms 13404 KB Output is correct
28 Correct 5 ms 13404 KB Output is correct
29 Correct 4 ms 13404 KB Output is correct
30 Correct 4 ms 13424 KB Output is correct
31 Correct 5 ms 13404 KB Output is correct
32 Correct 4 ms 13400 KB Output is correct
33 Correct 4 ms 13400 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 5 ms 13404 KB Output is correct
36 Correct 4 ms 13404 KB Output is correct
37 Correct 5 ms 13404 KB Output is correct
38 Correct 5 ms 13732 KB Output is correct
39 Correct 4 ms 13404 KB Output is correct
40 Correct 5 ms 13220 KB Output is correct
41 Correct 4 ms 13224 KB Output is correct
42 Correct 4 ms 12984 KB Output is correct
43 Correct 6 ms 13212 KB Output is correct
44 Correct 5 ms 13148 KB Output is correct
45 Correct 4 ms 13148 KB Output is correct
46 Correct 4 ms 13152 KB Output is correct
47 Correct 4 ms 13148 KB Output is correct
48 Correct 4 ms 13068 KB Output is correct
49 Correct 394 ms 67324 KB Output is correct
50 Correct 374 ms 71860 KB Output is correct
51 Correct 411 ms 75596 KB Output is correct
52 Correct 373 ms 62432 KB Output is correct
53 Correct 277 ms 72016 KB Output is correct
54 Correct 369 ms 76140 KB Output is correct
55 Correct 310 ms 66132 KB Output is correct
56 Correct 327 ms 70492 KB Output is correct
57 Correct 357 ms 76852 KB Output is correct
58 Correct 343 ms 72016 KB Output is correct
59 Correct 326 ms 71764 KB Output is correct
60 Correct 351 ms 69868 KB Output is correct
61 Correct 558 ms 104948 KB Output is correct
62 Correct 643 ms 93568 KB Output is correct
63 Correct 581 ms 60812 KB Output is correct
64 Correct 601 ms 52908 KB Output is correct
65 Correct 592 ms 47808 KB Output is correct
66 Correct 566 ms 45176 KB Output is correct
67 Correct 575 ms 43624 KB Output is correct
68 Correct 546 ms 43088 KB Output is correct
69 Correct 483 ms 42832 KB Output is correct
70 Correct 491 ms 42948 KB Output is correct
71 Correct 542 ms 42720 KB Output is correct
72 Correct 547 ms 42636 KB Output is correct
73 Correct 515 ms 42864 KB Output is correct
74 Correct 539 ms 42804 KB Output is correct
75 Correct 455 ms 43128 KB Output is correct
76 Correct 449 ms 43464 KB Output is correct
77 Correct 316 ms 44480 KB Output is correct
78 Correct 224 ms 46772 KB Output is correct