Submission #393115

# Submission time Handle Problem Language Result Execution time Memory
393115 2021-04-22T18:29:52 Z patrikpavic2 Hard route (IZhO17_road) C++17
100 / 100
1066 ms 115584 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;

const int N = 1e6 + 500;

int n;
vector < int > v[N];
pll dis[N], gor[N];
int dep[N];
ll cnt[N][3];

inline pll add(const pll &A, const pll &B){
	return {(A.X > B.X ? A.X : B.X), A.Y * (A.X >= B.X) + B.Y * (B.X >= A.X)};
}

pll dolje(int x, int lst){
	dep[x] = dep[lst] + 1;
	dis[x] = {0, 1};
	for(int y : v[x]){
		if(y == lst) continue;
		pll nxt = dolje(y, x);
		nxt.X++; dis[x] = add(dis[x], nxt);
	}
	pll cur = {1, 1};
	for(int y : v[x]){
		if(y == lst) continue;
		gor[y] = add(gor[y], cur);
		pll tmp = dis[y]; tmp.X += 2;
		cur = add(cur, tmp);
	}
	reverse(v[x].begin(), v[x].end());
	cur = {0, 0};
	for(int y : v[x]){
		if(y == lst) continue;	
		gor[y] = add(gor[y], cur);
		pll tmp = dis[y]; tmp.X += 2;
		cur = add(cur, tmp);
	}
	reverse(v[x].begin(), v[x].end());
	return dis[x];
}

void gore(int x, int lst){
	for(int y : v[x]){
		if(y == lst) continue;
		gor[y] = add(gor[y], {gor[x].X + 1, gor[x].Y});
		gore(y, x);
	}
}

pll calc_dis(int x, int y){
	if(dep[x] > dep[y])
		return {dis[x].X + 1, dis[x].Y};
	return {gor[y].X, gor[y].Y};
}

ll ch(int n, int k){
	int ret = 1;
	for(int i = 0;i < k;i++)
		ret *= (n - i), ret /= i + 1;
	return ret;
}

int main(){
	for(int i = 0;i < N;i++) dis[i].X = -1;
	scanf("%d", &n);
	for(int i = 1;i < n;i++){
		int x, y; scanf("%d%d", &x, &y);
		v[x].PB(y), v[y].PB(x);
	}
	dolje(1, 1); gore(1, 1);
	pll sol = {0, 0};
	for(int i = 1;i <= n;i++){
		vector < int > sus;
		
		for(int nxt : v[i]){
			pll nw = calc_dis(nxt, i);
			sus.PB(nw.X);
			cnt[nw.X][2] += nw.Y * cnt[nw.X][1];
			cnt[nw.X][1] += nw.Y;
		}
		for(int j = 0;j < 3;j++)
			for(int i = (int)sus.size() - 1; i ;i--)
				if(sus[i - 1] < sus[i]) swap(sus[i], sus[i - 1]);
		if((int)sus.size() > 2){
			if(sus[0] > sus[1] && sus[1] > sus[2])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][1] * cnt[sus[2]][1]});
			else if(sus[0] == sus[1] && sus[1] == sus[2])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[0]][2]});
			else if(sus[0] > sus[1])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2]});				
			else
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2] & cnt[sus[2]][1]});						
		}
		for(int x : sus){
			cnt[x][1] = 0, cnt[x][2] = 0;
		}
	}
	if(!sol.X) sol.Y++;
	printf("%lld %lld\n", sol.X, sol.Y);
	return 0;
}






Compilation message

road.cpp: In function 'int main()':
road.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   78 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
road.cpp:80:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |   int x, y; scanf("%d%d", &x, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 39372 KB Output is correct
2 Correct 22 ms 39372 KB Output is correct
3 Correct 23 ms 39372 KB Output is correct
4 Correct 23 ms 39468 KB Output is correct
5 Correct 23 ms 39372 KB Output is correct
6 Correct 23 ms 39396 KB Output is correct
7 Correct 22 ms 39372 KB Output is correct
8 Correct 22 ms 39372 KB Output is correct
9 Correct 23 ms 39392 KB Output is correct
10 Correct 24 ms 39392 KB Output is correct
11 Correct 23 ms 39472 KB Output is correct
12 Correct 23 ms 39400 KB Output is correct
13 Correct 22 ms 39352 KB Output is correct
14 Correct 23 ms 39372 KB Output is correct
15 Correct 23 ms 39364 KB Output is correct
16 Correct 23 ms 39360 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 23 ms 39372 KB Output is correct
19 Correct 23 ms 39432 KB Output is correct
20 Correct 23 ms 39352 KB Output is correct
21 Correct 22 ms 39372 KB Output is correct
22 Correct 23 ms 39472 KB Output is correct
23 Correct 23 ms 39372 KB Output is correct
24 Correct 23 ms 39404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 39372 KB Output is correct
2 Correct 22 ms 39372 KB Output is correct
3 Correct 23 ms 39372 KB Output is correct
4 Correct 23 ms 39468 KB Output is correct
5 Correct 23 ms 39372 KB Output is correct
6 Correct 23 ms 39396 KB Output is correct
7 Correct 22 ms 39372 KB Output is correct
8 Correct 22 ms 39372 KB Output is correct
9 Correct 23 ms 39392 KB Output is correct
10 Correct 24 ms 39392 KB Output is correct
11 Correct 23 ms 39472 KB Output is correct
12 Correct 23 ms 39400 KB Output is correct
13 Correct 22 ms 39352 KB Output is correct
14 Correct 23 ms 39372 KB Output is correct
15 Correct 23 ms 39364 KB Output is correct
16 Correct 23 ms 39360 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 23 ms 39372 KB Output is correct
19 Correct 23 ms 39432 KB Output is correct
20 Correct 23 ms 39352 KB Output is correct
21 Correct 22 ms 39372 KB Output is correct
22 Correct 23 ms 39472 KB Output is correct
23 Correct 23 ms 39372 KB Output is correct
24 Correct 23 ms 39404 KB Output is correct
25 Correct 26 ms 39932 KB Output is correct
26 Correct 27 ms 39900 KB Output is correct
27 Correct 26 ms 39976 KB Output is correct
28 Correct 26 ms 39860 KB Output is correct
29 Correct 25 ms 39924 KB Output is correct
30 Correct 26 ms 39884 KB Output is correct
31 Correct 26 ms 39900 KB Output is correct
32 Correct 26 ms 39840 KB Output is correct
33 Correct 26 ms 39948 KB Output is correct
34 Correct 26 ms 39972 KB Output is correct
35 Correct 26 ms 39884 KB Output is correct
36 Correct 26 ms 39948 KB Output is correct
37 Correct 26 ms 40012 KB Output is correct
38 Correct 27 ms 40228 KB Output is correct
39 Correct 26 ms 39844 KB Output is correct
40 Correct 26 ms 39784 KB Output is correct
41 Correct 27 ms 39756 KB Output is correct
42 Correct 26 ms 39628 KB Output is correct
43 Correct 26 ms 39676 KB Output is correct
44 Correct 25 ms 39596 KB Output is correct
45 Correct 26 ms 39624 KB Output is correct
46 Correct 25 ms 39628 KB Output is correct
47 Correct 25 ms 39628 KB Output is correct
48 Correct 25 ms 39756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 39372 KB Output is correct
2 Correct 22 ms 39372 KB Output is correct
3 Correct 23 ms 39372 KB Output is correct
4 Correct 23 ms 39468 KB Output is correct
5 Correct 23 ms 39372 KB Output is correct
6 Correct 23 ms 39396 KB Output is correct
7 Correct 22 ms 39372 KB Output is correct
8 Correct 22 ms 39372 KB Output is correct
9 Correct 23 ms 39392 KB Output is correct
10 Correct 24 ms 39392 KB Output is correct
11 Correct 23 ms 39472 KB Output is correct
12 Correct 23 ms 39400 KB Output is correct
13 Correct 22 ms 39352 KB Output is correct
14 Correct 23 ms 39372 KB Output is correct
15 Correct 23 ms 39364 KB Output is correct
16 Correct 23 ms 39360 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 23 ms 39372 KB Output is correct
19 Correct 23 ms 39432 KB Output is correct
20 Correct 23 ms 39352 KB Output is correct
21 Correct 22 ms 39372 KB Output is correct
22 Correct 23 ms 39472 KB Output is correct
23 Correct 23 ms 39372 KB Output is correct
24 Correct 23 ms 39404 KB Output is correct
25 Correct 26 ms 39932 KB Output is correct
26 Correct 27 ms 39900 KB Output is correct
27 Correct 26 ms 39976 KB Output is correct
28 Correct 26 ms 39860 KB Output is correct
29 Correct 25 ms 39924 KB Output is correct
30 Correct 26 ms 39884 KB Output is correct
31 Correct 26 ms 39900 KB Output is correct
32 Correct 26 ms 39840 KB Output is correct
33 Correct 26 ms 39948 KB Output is correct
34 Correct 26 ms 39972 KB Output is correct
35 Correct 26 ms 39884 KB Output is correct
36 Correct 26 ms 39948 KB Output is correct
37 Correct 26 ms 40012 KB Output is correct
38 Correct 27 ms 40228 KB Output is correct
39 Correct 26 ms 39844 KB Output is correct
40 Correct 26 ms 39784 KB Output is correct
41 Correct 27 ms 39756 KB Output is correct
42 Correct 26 ms 39628 KB Output is correct
43 Correct 26 ms 39676 KB Output is correct
44 Correct 25 ms 39596 KB Output is correct
45 Correct 26 ms 39624 KB Output is correct
46 Correct 25 ms 39628 KB Output is correct
47 Correct 25 ms 39628 KB Output is correct
48 Correct 25 ms 39756 KB Output is correct
49 Correct 773 ms 85416 KB Output is correct
50 Correct 789 ms 88224 KB Output is correct
51 Correct 763 ms 90516 KB Output is correct
52 Correct 769 ms 82304 KB Output is correct
53 Correct 670 ms 86652 KB Output is correct
54 Correct 700 ms 89284 KB Output is correct
55 Correct 680 ms 82936 KB Output is correct
56 Correct 684 ms 85588 KB Output is correct
57 Correct 748 ms 95368 KB Output is correct
58 Correct 741 ms 91972 KB Output is correct
59 Correct 754 ms 91848 KB Output is correct
60 Correct 756 ms 90432 KB Output is correct
61 Correct 1066 ms 115584 KB Output is correct
62 Correct 1054 ms 108560 KB Output is correct
63 Correct 1020 ms 80728 KB Output is correct
64 Correct 985 ms 73608 KB Output is correct
65 Correct 959 ms 69204 KB Output is correct
66 Correct 926 ms 66860 KB Output is correct
67 Correct 904 ms 65600 KB Output is correct
68 Correct 894 ms 65236 KB Output is correct
69 Correct 898 ms 65048 KB Output is correct
70 Correct 881 ms 64904 KB Output is correct
71 Correct 860 ms 64708 KB Output is correct
72 Correct 867 ms 64836 KB Output is correct
73 Correct 872 ms 64964 KB Output is correct
74 Correct 863 ms 72236 KB Output is correct
75 Correct 849 ms 72516 KB Output is correct
76 Correct 832 ms 73100 KB Output is correct
77 Correct 723 ms 74296 KB Output is correct
78 Correct 535 ms 77172 KB Output is correct