Submission #1116242

# Submission time Handle Problem Language Result Execution time Memory
1116242 2024-11-21T11:28:21 Z hamzabc Hard route (IZhO17_road) C++14
100 / 100
643 ms 117364 KB
#include <bits/stdc++.h>
 
 
using namespace std;
 
 
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define sp << " " <<
#define endl << '\n'


vector<vector<int>> graph;
vector<long long int> dist;
vector<long long int> distind;
vector<long long int> distmul;
long long int hard = 0;
long long int cnt = 0;
int n;



void dp(int node = 0, int parent = -1){
	for (auto go : graph[node]){
		if (go == parent)
			continue;
		dp(go, node);
		if (dist[node] < dist[go] + 1){
			dist[node] = dist[go] + 1;
			distmul[node] = distmul[go];
		}else if (dist[node] == dist[go] + 1){
			distmul[node] += distmul[go];
		}
	}
}

void reroot(int node = 0, int parent = -1, long long int parentdist = -1, long long int parentmul = 1){
	long long int big1 = parentdist + 1, big2 = 0, big3 = 0;
	long long int mul1 = parentmul, mul2 = 0, mul3 = 0;
	for (int go : graph[node]){
		if (go == parent)
			continue;
		if (dist[go] + 1 > big1){
			big3 = big2;
			mul3 = mul2;
			big2 = big1;
			mul2 = mul1;
			big1 = dist[go] + 1;
			mul1 = distmul[go];
		}else if (dist[go] + 1 > big2){
			big3 = big2;
			mul3 = mul2;
			big2 = dist[go] + 1;
			mul2 = distmul[go];
		}else if (dist[go] + 1 > big3){
			big3 = dist[go] + 1;
			mul3 = distmul[go];
		}
	}
	long long int deg1 = big1 * (big2 + big3);
	if (graph[node].size() > 2 && deg1 > hard){
		hard = deg1;
	}
	for (int go : graph[node]){
		if (go == parent)
			continue;
		if (big1 == dist[go] + 1){
			reroot(go, node, big2, mul2);
		}else{
			reroot(go, node, big1, mul1);
		}
	}
}

void reroot2(int node = 0, int parent = -1, long long int parentdist = -1, long long int parentmul = 0, bool parentused = true){
	if (graph[node].size() == 0)
		return;
	long long int big1 = parentdist + 1, big2 = 0, big3 = 0;
	long long int mul1 = max(parentmul, 1LL), mul2 = 0, mul3 = 0;
	long long int smul1 = parentmul, smul2 = 0;
	long long int num1 = !parentused, num2 = 0, num3 = 0;
	if (parentused){
		big1 = 0;
		mul1 = 0;
	}
	for (int go : graph[node]){
		if (go == parent)
			continue;
		if (dist[go] + 1 == big1){
			mul1 += distmul[go];
			smul1 += distmul[go] * distmul[go];
			num1++;
		}else if (dist[go] + 1 == big2){
			mul2 += distmul[go];
			smul2 += distmul[go] * distmul[go];
			num2++;
		}else if (dist[go] + 1 == big3){
			mul3 += distmul[go];
			num3++;
		}else if (dist[go] + 1 > big1){
			big3 = big2;
			mul3 = mul2;
			num3 = num2;
			big2 = big1;
			mul2 = mul1;
			num2 = num1;
			smul2 = smul1;
			big1 = dist[go] + 1;
			mul1 = distmul[go];
			num1 = 1;
			smul1 = mul1 * mul1;
		}else if (dist[go] + 1 > big2){
			big3 = big2;
			mul3 = mul2;
			num3 = num2;
			big2 = dist[go] + 1;
			mul2 = distmul[go];
			smul2 = mul2 * mul2;
			num2 = 1;
		}else if (dist[go] + 1 > big3){
			big3 = dist[go] + 1;
			mul3 = distmul[go];
			num3 = 1;
		}
	}
	// cerr << node sp "|" sp big1 sp mul1 sp num1 sp "|" sp big2 sp mul2 sp num2 sp "|" sp big3 sp mul3 sp num3 sp "|" sp parentused sp "pd:"sp parentdist + 1 sp parentmul endl;
	bool flag1 = false, flag2 = false;
	if (parentused && (big1 + big2) * (parentdist + 1) == hard){
		cnt += mul2 * mul1;
		if (((parentdist + 1) + big2) * big1 == hard)
			cnt += parentmul * mul2;
		flag1 = true;
		flag2 = true;
	}else if (parentused && (num1 > 1 && (2 * big1 * (parentdist + 1)) == hard)){
		cnt += (mul1 * mul1 - smul1) / 2;
		if (((parentdist + 1) + big2) * big1 == hard)
			cnt += parentmul * mul2;
		flag1 = true;
		flag2 = true;
	}else if (big1 * (big2 + big3) == hard || (num2 > 1 && big1 * big2 * 2 == hard) || (num1 > 1 && big1 * (big1 + big2) == hard) || (num1 > 2 && big1 * big1 * 2 == hard)){
		if (num1 > 2){
			cnt += (mul1 * mul1 - smul1) / 2;
			flag2 = true;
			flag1 = true;
		}else if (num1 > 1){
			cnt += mul2 * mul1;
			flag2 = true;
			flag1 = true;
		}else if (num2 > 1){
			cnt += (mul2 * mul2 - smul2) / 2;
			flag1 = true;
		}else{
			cnt += mul2 * mul3;
			flag1 = true;
		}
	}
	for (int go : graph[node]){
		if (go == parent)
			continue;
		if (parentused){
			if (big1 == dist[go] + 1 && num1 > 1 && big1 >= parentdist + 1)
				reroot2(go, node, big1, mul1 - distmul[go], flag2);
			else if (big1 != dist[go] + 1 && big1 >= parentdist + 1){
				reroot2(go, node, big1, mul1, flag2);
			}else if (big2 >= parentdist + 1){
				reroot2(go, node, big2, mul2, flag1);
			}else{
				reroot2(go, node, parentdist + 1, parentmul, true);
			}
		}else{
			if (big1 == dist[go] + 1 && num1 == 1){
				reroot2(go, node, big2, mul2, flag1);
			}else if (big1 == dist[go] + 1 && num1 > 1)
				reroot2(go, node, big1, mul1 - distmul[go], flag2);
			else
				reroot2(go, node, big1, mul1, flag2);
		}
	}
}


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	graph.resize(n);
	dist.resize(n, 0);
	distind.resize(n);
	distmul.resize(n, 1);
	for (int i = 0; i < n - 1; i++){
		int a, b;
		cin >> a >> b;
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dp();
	reroot();
	reroot2();
	cout << hard sp max(1LL, cnt);
}

Compilation message

road.cpp: In function 'void reroot(int, int, long long int, long long int)':
road.cpp:39:44: warning: variable 'mul3' set but not used [-Wunused-but-set-variable]
   39 |  long long int mul1 = parentmul, mul2 = 0, mul3 = 0;
      |                                            ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 508 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 2 ms 1276 KB Output is correct
27 Correct 2 ms 1104 KB Output is correct
28 Correct 3 ms 1272 KB Output is correct
29 Correct 2 ms 1104 KB Output is correct
30 Correct 2 ms 1104 KB Output is correct
31 Correct 2 ms 1104 KB Output is correct
32 Correct 2 ms 848 KB Output is correct
33 Correct 3 ms 1104 KB Output is correct
34 Correct 2 ms 1104 KB Output is correct
35 Correct 2 ms 1104 KB Output is correct
36 Correct 2 ms 1104 KB Output is correct
37 Correct 2 ms 1104 KB Output is correct
38 Correct 3 ms 1616 KB Output is correct
39 Correct 2 ms 1104 KB Output is correct
40 Correct 2 ms 848 KB Output is correct
41 Correct 2 ms 848 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 2 ms 848 KB Output is correct
44 Correct 2 ms 592 KB Output is correct
45 Correct 2 ms 592 KB Output is correct
46 Correct 2 ms 592 KB Output is correct
47 Correct 3 ms 1028 KB Output is correct
48 Correct 2 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 508 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 2 ms 1276 KB Output is correct
27 Correct 2 ms 1104 KB Output is correct
28 Correct 3 ms 1272 KB Output is correct
29 Correct 2 ms 1104 KB Output is correct
30 Correct 2 ms 1104 KB Output is correct
31 Correct 2 ms 1104 KB Output is correct
32 Correct 2 ms 848 KB Output is correct
33 Correct 3 ms 1104 KB Output is correct
34 Correct 2 ms 1104 KB Output is correct
35 Correct 2 ms 1104 KB Output is correct
36 Correct 2 ms 1104 KB Output is correct
37 Correct 2 ms 1104 KB Output is correct
38 Correct 3 ms 1616 KB Output is correct
39 Correct 2 ms 1104 KB Output is correct
40 Correct 2 ms 848 KB Output is correct
41 Correct 2 ms 848 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 2 ms 848 KB Output is correct
44 Correct 2 ms 592 KB Output is correct
45 Correct 2 ms 592 KB Output is correct
46 Correct 2 ms 592 KB Output is correct
47 Correct 3 ms 1028 KB Output is correct
48 Correct 2 ms 848 KB Output is correct
49 Correct 408 ms 68680 KB Output is correct
50 Correct 384 ms 74312 KB Output is correct
51 Correct 376 ms 78920 KB Output is correct
52 Correct 395 ms 62412 KB Output is correct
53 Correct 310 ms 71240 KB Output is correct
54 Correct 317 ms 76364 KB Output is correct
55 Correct 319 ms 63808 KB Output is correct
56 Correct 295 ms 69192 KB Output is correct
57 Correct 381 ms 82248 KB Output is correct
58 Correct 404 ms 76104 KB Output is correct
59 Correct 382 ms 75848 KB Output is correct
60 Correct 286 ms 73004 KB Output is correct
61 Correct 634 ms 117364 KB Output is correct
62 Correct 643 ms 103220 KB Output is correct
63 Correct 618 ms 61788 KB Output is correct
64 Correct 613 ms 52040 KB Output is correct
65 Correct 578 ms 45872 KB Output is correct
66 Correct 525 ms 42568 KB Output is correct
67 Correct 514 ms 40776 KB Output is correct
68 Correct 462 ms 40008 KB Output is correct
69 Correct 469 ms 39752 KB Output is correct
70 Correct 473 ms 39560 KB Output is correct
71 Correct 552 ms 39320 KB Output is correct
72 Correct 551 ms 39568 KB Output is correct
73 Correct 516 ms 39520 KB Output is correct
74 Correct 489 ms 39480 KB Output is correct
75 Correct 475 ms 39796 KB Output is correct
76 Correct 432 ms 39872 KB Output is correct
77 Correct 352 ms 40380 KB Output is correct
78 Correct 222 ms 41400 KB Output is correct