답안 #120617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120617 2019-06-25T05:41:57 Z 김세빈(#2958) Mergers (JOI19_mergers) C++14
100 / 100
1734 ms 132236 KB
#include <bits/stdc++.h>

using namespace std;

vector <int> T[505050];
int V[1010101], E[1010101];
int P[19][505050], D[505050], N[505050];
int S[1010101], A[505050], B[1010101], C[1010101];
int GS[505050], GL[505050], GR[505050], X[505050];
int n, k, cnt;

void dfs1(int p, int r)
{
	N[p] = ++ cnt;
	P[0][p] = r; D[p] = D[r] + 1;
	
	for(int &t: T[p]){
		if(V[t ^ 1] != r){
			dfs1(V[t ^ 1], p);
		}
	}
}

int dfs2(int p, int r)
{
	int s, x;
	
	s = 1;
	
	for(int &t: T[p]){
		if(V[t ^ 1] != r){
			s += dfs2(V[t ^ 1], p);
			X[p] += X[V[t ^ 1]];
		}
		else x = t;
	}
	
	if(r){
		if(!X[p]) C[x] = C[x ^ 1] = 1;
		S[x] = s; S[x ^ 1] = n - s;
	}
	
	return s;
}

int lca(int p, int q)
{
	int i;
	
	if(D[p] < D[q]) swap(p, q);
	
	for(i=0; i<=18; i++){
		if(D[p] - D[q] & (1 << i)){
			p = P[i][p];
		}
	}
	
	if(p == q) return p;
	
	for(i=18; i>=0; i--){
		if(P[i][p] != P[i][q]){
			p = P[i][p];
			q = P[i][q];
		}
	}
	
	return P[0][p];
}

int main()
{
	int i, j, m, s;
	
	scanf("%d%d", &n, &k);
	
	for(i=0; i<n+n-2; i++){
		scanf("%d", V + i);
		T[V[i]].push_back(i);
		E[i] = i;
	}
	
	dfs1(1, 0);
	
	for(i=1; i<=18; i++){
		for(j=1; j<=n; j++){
			P[i][j] = P[i - 1][P[i - 1][j]];
		}
	}
	
	N[n + 1] = n + 1;
	
	for(i=1; i<=k; i++){
		GL[i] = n + 1; GR[i] = 0;
	}
	
	for(i=1; i<=n; i++){
		scanf("%d", &s); GS[s] ++; X[i] ++;
		if(N[GL[s]] > N[i]) GL[s] = i;
		if(N[GR[s]] < N[i]) GR[s] = i;
	}
	
	for(i=1; i<=k; i++){
		X[lca(GL[i], GR[i])] -= GS[i];
	}
	
	dfs2(1, 0);
	
	sort(E, E + n + n - 2, [&](int &ea, int &eb){
		return S[ea] < S[eb];
	});
	
	for(i=0; i<n+n-2; i++){
		A[V[E[i] ^ 1]] += (B[E[i]] = max(A[V[E[i]]] - B[E[i] ^ 1], C[E[i]]));
	}
	
	for(i=1; i<=n; i++){
		m = s = 0;
		for(int &t: T[i]){
			m = max(m, B[t ^ 1]);
			s += B[t ^ 1];
		}
		
		if(m + m <= s + 1){
			printf("%d\n", s + 1 >> 1);
			return 0;
		}
	}
	
	return 0;
}

Compilation message

mergers.cpp: In function 'int lca(int, int)':
mergers.cpp:53:11: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   if(D[p] - D[q] & (1 << i)){
      ~~~~~^~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:124:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    printf("%d\n", s + 1 >> 1);
                   ~~^~~
mergers.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", V + i);
   ~~~~~^~~~~~~~~~~~~
mergers.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &s); GS[s] ++; X[i] ++;
   ~~~~~^~~~~~~~~~
mergers.cpp: In function 'int dfs2(int, int)':
mergers.cpp:26:9: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int s, x;
         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12408 KB Output is correct
2 Correct 13 ms 12416 KB Output is correct
3 Correct 13 ms 12416 KB Output is correct
4 Correct 13 ms 12416 KB Output is correct
5 Correct 12 ms 12416 KB Output is correct
6 Correct 12 ms 12416 KB Output is correct
7 Correct 13 ms 12416 KB Output is correct
8 Correct 29 ms 12408 KB Output is correct
9 Correct 12 ms 12416 KB Output is correct
10 Correct 11 ms 12416 KB Output is correct
11 Correct 12 ms 12416 KB Output is correct
12 Correct 13 ms 12416 KB Output is correct
13 Correct 13 ms 12416 KB Output is correct
14 Correct 22 ms 12416 KB Output is correct
15 Correct 12 ms 12416 KB Output is correct
16 Correct 13 ms 12416 KB Output is correct
17 Correct 13 ms 12416 KB Output is correct
18 Correct 13 ms 12416 KB Output is correct
19 Correct 13 ms 12416 KB Output is correct
20 Correct 12 ms 12416 KB Output is correct
21 Correct 12 ms 12416 KB Output is correct
22 Correct 12 ms 12416 KB Output is correct
23 Correct 11 ms 12416 KB Output is correct
24 Correct 12 ms 12416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12408 KB Output is correct
2 Correct 13 ms 12416 KB Output is correct
3 Correct 13 ms 12416 KB Output is correct
4 Correct 13 ms 12416 KB Output is correct
5 Correct 12 ms 12416 KB Output is correct
6 Correct 12 ms 12416 KB Output is correct
7 Correct 13 ms 12416 KB Output is correct
8 Correct 29 ms 12408 KB Output is correct
9 Correct 12 ms 12416 KB Output is correct
10 Correct 11 ms 12416 KB Output is correct
11 Correct 12 ms 12416 KB Output is correct
12 Correct 13 ms 12416 KB Output is correct
13 Correct 13 ms 12416 KB Output is correct
14 Correct 22 ms 12416 KB Output is correct
15 Correct 12 ms 12416 KB Output is correct
16 Correct 13 ms 12416 KB Output is correct
17 Correct 13 ms 12416 KB Output is correct
18 Correct 13 ms 12416 KB Output is correct
19 Correct 13 ms 12416 KB Output is correct
20 Correct 12 ms 12416 KB Output is correct
21 Correct 12 ms 12416 KB Output is correct
22 Correct 12 ms 12416 KB Output is correct
23 Correct 11 ms 12416 KB Output is correct
24 Correct 12 ms 12416 KB Output is correct
25 Correct 12 ms 12416 KB Output is correct
26 Correct 14 ms 13056 KB Output is correct
27 Correct 15 ms 12884 KB Output is correct
28 Correct 15 ms 13056 KB Output is correct
29 Correct 15 ms 12928 KB Output is correct
30 Correct 15 ms 12928 KB Output is correct
31 Correct 12 ms 12416 KB Output is correct
32 Correct 15 ms 13184 KB Output is correct
33 Correct 14 ms 12416 KB Output is correct
34 Correct 14 ms 12928 KB Output is correct
35 Correct 15 ms 13056 KB Output is correct
36 Correct 15 ms 12928 KB Output is correct
37 Correct 15 ms 12952 KB Output is correct
38 Correct 12 ms 12416 KB Output is correct
39 Correct 16 ms 12928 KB Output is correct
40 Correct 15 ms 12928 KB Output is correct
41 Correct 15 ms 12928 KB Output is correct
42 Correct 15 ms 12928 KB Output is correct
43 Correct 15 ms 13184 KB Output is correct
44 Correct 12 ms 12416 KB Output is correct
45 Correct 15 ms 13056 KB Output is correct
46 Correct 15 ms 12928 KB Output is correct
47 Correct 12 ms 12416 KB Output is correct
48 Correct 15 ms 12928 KB Output is correct
49 Correct 14 ms 12928 KB Output is correct
50 Correct 15 ms 13056 KB Output is correct
51 Correct 15 ms 12928 KB Output is correct
52 Correct 17 ms 12928 KB Output is correct
53 Correct 15 ms 13056 KB Output is correct
54 Correct 16 ms 12928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12408 KB Output is correct
2 Correct 13 ms 12416 KB Output is correct
3 Correct 13 ms 12416 KB Output is correct
4 Correct 13 ms 12416 KB Output is correct
5 Correct 12 ms 12416 KB Output is correct
6 Correct 12 ms 12416 KB Output is correct
7 Correct 13 ms 12416 KB Output is correct
8 Correct 29 ms 12408 KB Output is correct
9 Correct 12 ms 12416 KB Output is correct
10 Correct 11 ms 12416 KB Output is correct
11 Correct 12 ms 12416 KB Output is correct
12 Correct 13 ms 12416 KB Output is correct
13 Correct 13 ms 12416 KB Output is correct
14 Correct 22 ms 12416 KB Output is correct
15 Correct 12 ms 12416 KB Output is correct
16 Correct 13 ms 12416 KB Output is correct
17 Correct 13 ms 12416 KB Output is correct
18 Correct 13 ms 12416 KB Output is correct
19 Correct 13 ms 12416 KB Output is correct
20 Correct 12 ms 12416 KB Output is correct
21 Correct 12 ms 12416 KB Output is correct
22 Correct 12 ms 12416 KB Output is correct
23 Correct 11 ms 12416 KB Output is correct
24 Correct 12 ms 12416 KB Output is correct
25 Correct 13 ms 12416 KB Output is correct
26 Correct 94 ms 29552 KB Output is correct
27 Correct 147 ms 29264 KB Output is correct
28 Correct 15 ms 12928 KB Output is correct
29 Correct 12 ms 12416 KB Output is correct
30 Correct 12 ms 12416 KB Output is correct
31 Correct 137 ms 29432 KB Output is correct
32 Correct 14 ms 12928 KB Output is correct
33 Correct 270 ms 36324 KB Output is correct
34 Correct 123 ms 29304 KB Output is correct
35 Correct 15 ms 12928 KB Output is correct
36 Correct 183 ms 30072 KB Output is correct
37 Correct 14 ms 12928 KB Output is correct
38 Correct 14 ms 12928 KB Output is correct
39 Correct 94 ms 29520 KB Output is correct
40 Correct 16 ms 13172 KB Output is correct
41 Correct 131 ms 29176 KB Output is correct
42 Correct 183 ms 31608 KB Output is correct
43 Correct 13 ms 12416 KB Output is correct
44 Correct 198 ms 36548 KB Output is correct
45 Correct 163 ms 32888 KB Output is correct
46 Correct 15 ms 13048 KB Output is correct
47 Correct 15 ms 12928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 28016 KB Output is correct
2 Correct 113 ms 30064 KB Output is correct
3 Correct 15 ms 12900 KB Output is correct
4 Correct 15 ms 12928 KB Output is correct
5 Correct 12 ms 12416 KB Output is correct
6 Correct 12 ms 12416 KB Output is correct
7 Correct 14 ms 12848 KB Output is correct
8 Correct 154 ms 29176 KB Output is correct
9 Correct 15 ms 12928 KB Output is correct
10 Correct 151 ms 30128 KB Output is correct
11 Correct 12 ms 12416 KB Output is correct
12 Correct 174 ms 30288 KB Output is correct
13 Correct 167 ms 30932 KB Output is correct
14 Correct 151 ms 31480 KB Output is correct
15 Correct 97 ms 29560 KB Output is correct
16 Correct 14 ms 12928 KB Output is correct
17 Correct 15 ms 12464 KB Output is correct
18 Correct 114 ms 31612 KB Output is correct
19 Correct 189 ms 35440 KB Output is correct
20 Correct 15 ms 12928 KB Output is correct
21 Correct 12 ms 12416 KB Output is correct
22 Correct 104 ms 31088 KB Output is correct
23 Correct 15 ms 12928 KB Output is correct
24 Correct 161 ms 30404 KB Output is correct
25 Correct 156 ms 34040 KB Output is correct
26 Correct 14 ms 13056 KB Output is correct
27 Correct 15 ms 13056 KB Output is correct
28 Correct 15 ms 12928 KB Output is correct
29 Correct 15 ms 12928 KB Output is correct
30 Correct 14 ms 12988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12408 KB Output is correct
2 Correct 13 ms 12416 KB Output is correct
3 Correct 13 ms 12416 KB Output is correct
4 Correct 13 ms 12416 KB Output is correct
5 Correct 12 ms 12416 KB Output is correct
6 Correct 12 ms 12416 KB Output is correct
7 Correct 13 ms 12416 KB Output is correct
8 Correct 29 ms 12408 KB Output is correct
9 Correct 12 ms 12416 KB Output is correct
10 Correct 11 ms 12416 KB Output is correct
11 Correct 12 ms 12416 KB Output is correct
12 Correct 13 ms 12416 KB Output is correct
13 Correct 13 ms 12416 KB Output is correct
14 Correct 22 ms 12416 KB Output is correct
15 Correct 12 ms 12416 KB Output is correct
16 Correct 13 ms 12416 KB Output is correct
17 Correct 13 ms 12416 KB Output is correct
18 Correct 13 ms 12416 KB Output is correct
19 Correct 13 ms 12416 KB Output is correct
20 Correct 12 ms 12416 KB Output is correct
21 Correct 12 ms 12416 KB Output is correct
22 Correct 12 ms 12416 KB Output is correct
23 Correct 11 ms 12416 KB Output is correct
24 Correct 12 ms 12416 KB Output is correct
25 Correct 12 ms 12416 KB Output is correct
26 Correct 14 ms 13056 KB Output is correct
27 Correct 15 ms 12884 KB Output is correct
28 Correct 15 ms 13056 KB Output is correct
29 Correct 15 ms 12928 KB Output is correct
30 Correct 15 ms 12928 KB Output is correct
31 Correct 12 ms 12416 KB Output is correct
32 Correct 15 ms 13184 KB Output is correct
33 Correct 14 ms 12416 KB Output is correct
34 Correct 14 ms 12928 KB Output is correct
35 Correct 15 ms 13056 KB Output is correct
36 Correct 15 ms 12928 KB Output is correct
37 Correct 15 ms 12952 KB Output is correct
38 Correct 12 ms 12416 KB Output is correct
39 Correct 16 ms 12928 KB Output is correct
40 Correct 15 ms 12928 KB Output is correct
41 Correct 15 ms 12928 KB Output is correct
42 Correct 15 ms 12928 KB Output is correct
43 Correct 15 ms 13184 KB Output is correct
44 Correct 12 ms 12416 KB Output is correct
45 Correct 15 ms 13056 KB Output is correct
46 Correct 15 ms 12928 KB Output is correct
47 Correct 12 ms 12416 KB Output is correct
48 Correct 15 ms 12928 KB Output is correct
49 Correct 14 ms 12928 KB Output is correct
50 Correct 15 ms 13056 KB Output is correct
51 Correct 15 ms 12928 KB Output is correct
52 Correct 17 ms 12928 KB Output is correct
53 Correct 15 ms 13056 KB Output is correct
54 Correct 16 ms 12928 KB Output is correct
55 Correct 13 ms 12416 KB Output is correct
56 Correct 94 ms 29552 KB Output is correct
57 Correct 147 ms 29264 KB Output is correct
58 Correct 15 ms 12928 KB Output is correct
59 Correct 12 ms 12416 KB Output is correct
60 Correct 12 ms 12416 KB Output is correct
61 Correct 137 ms 29432 KB Output is correct
62 Correct 14 ms 12928 KB Output is correct
63 Correct 270 ms 36324 KB Output is correct
64 Correct 123 ms 29304 KB Output is correct
65 Correct 15 ms 12928 KB Output is correct
66 Correct 183 ms 30072 KB Output is correct
67 Correct 14 ms 12928 KB Output is correct
68 Correct 14 ms 12928 KB Output is correct
69 Correct 94 ms 29520 KB Output is correct
70 Correct 16 ms 13172 KB Output is correct
71 Correct 131 ms 29176 KB Output is correct
72 Correct 183 ms 31608 KB Output is correct
73 Correct 13 ms 12416 KB Output is correct
74 Correct 198 ms 36548 KB Output is correct
75 Correct 163 ms 32888 KB Output is correct
76 Correct 15 ms 13048 KB Output is correct
77 Correct 15 ms 12928 KB Output is correct
78 Correct 104 ms 28016 KB Output is correct
79 Correct 113 ms 30064 KB Output is correct
80 Correct 15 ms 12900 KB Output is correct
81 Correct 15 ms 12928 KB Output is correct
82 Correct 12 ms 12416 KB Output is correct
83 Correct 12 ms 12416 KB Output is correct
84 Correct 14 ms 12848 KB Output is correct
85 Correct 154 ms 29176 KB Output is correct
86 Correct 15 ms 12928 KB Output is correct
87 Correct 151 ms 30128 KB Output is correct
88 Correct 12 ms 12416 KB Output is correct
89 Correct 174 ms 30288 KB Output is correct
90 Correct 167 ms 30932 KB Output is correct
91 Correct 151 ms 31480 KB Output is correct
92 Correct 97 ms 29560 KB Output is correct
93 Correct 14 ms 12928 KB Output is correct
94 Correct 15 ms 12464 KB Output is correct
95 Correct 114 ms 31612 KB Output is correct
96 Correct 189 ms 35440 KB Output is correct
97 Correct 15 ms 12928 KB Output is correct
98 Correct 12 ms 12416 KB Output is correct
99 Correct 104 ms 31088 KB Output is correct
100 Correct 15 ms 12928 KB Output is correct
101 Correct 161 ms 30404 KB Output is correct
102 Correct 156 ms 34040 KB Output is correct
103 Correct 14 ms 13056 KB Output is correct
104 Correct 15 ms 13056 KB Output is correct
105 Correct 15 ms 12928 KB Output is correct
106 Correct 15 ms 12928 KB Output is correct
107 Correct 14 ms 12988 KB Output is correct
108 Correct 792 ms 106844 KB Output is correct
109 Correct 1461 ms 114816 KB Output is correct
110 Correct 1527 ms 126196 KB Output is correct
111 Correct 1734 ms 132236 KB Output is correct
112 Correct 1430 ms 122448 KB Output is correct
113 Correct 842 ms 109876 KB Output is correct
114 Correct 1039 ms 97096 KB Output is correct
115 Correct 1047 ms 96904 KB Output is correct
116 Correct 1239 ms 102432 KB Output is correct
117 Correct 1224 ms 109120 KB Output is correct
118 Correct 1061 ms 98256 KB Output is correct
119 Correct 1200 ms 109104 KB Output is correct
120 Correct 1575 ms 122896 KB Output is correct
121 Correct 1231 ms 109200 KB Output is correct
122 Correct 1322 ms 106600 KB Output is correct
123 Correct 810 ms 110480 KB Output is correct
124 Correct 1633 ms 106936 KB Output is correct