Submission #120651

# Submission time Handle Problem Language Result Execution time Memory
120651 2019-06-25T07:08:33 Z 이온조(#2959) Mergers (JOI19_mergers) C++14
100 / 100
2100 ms 118960 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

vector<int> A[500009], adj[500009];
int S[500009], in[500009], ou[500009], t, p[22][500009], np[500009], l;
int uf[500009], deg[500009];

inline bool isup(int u, int v) {return in[u] <= in[v] && ou[u] >= ou[v];}

int LCA(int u, int v) {
	if(isup(u, v)) return u;
	if(isup(v, u)) return v;
	for(int i=l; i>=0; i--) if(!isup(p[i][u], v)) u = p[i][u];
	return p[0][u];
}

void dfs(int x, int par) {
	in[x] = ++t;
	p[0][x] = np[x] = par;
	for(auto& it: adj[x]) if(it != par) dfs(it, x);
	ou[x] = ++t;
}

int root(int x) {
	if(uf[x] == x) return x;
	return uf[x] = root(uf[x]);
}

void merg(int u, int v) {
	u = root(u); v = root(v);
	uf[u] = v;
}

void mrg(int now, int anc) {
	if(isup(now, anc)) return;
	if(S[now] != S[np[now]]) merg(S[now], S[np[now]]);
	mrg(np[now], anc);
	np[now] = anc;
}

int main() {
	int N, K; scanf("%d%d",&N,&K);
	for(int i=1; i<=K; i++) uf[i] = i;
	for(int i=0; i<N-1; i++) {
		int u, v; scanf("%d%d",&u,&v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 1);
	for(int i=1; (1<<i)<=N; i++) {
		for(int j=1; j<=N; j++) p[i][j] = p[i-1][p[i-1][j]];
		l = i;
	}
	for(int i=1; i<=N; i++) {
		scanf("%d",&S[i]);
		A[S[i]].push_back(i);
	}
	vector<pii> Q;
	for(int i=1; i<=K; i++) {
		sort(A[i].begin(), A[i].end(), [&](int P, int Q) {return in[P] < in[Q];});
		for(int j=1; j<A[i].size(); j++) {
			int u = A[i][j-1], v = A[i][j];
			int lca = LCA(u, v);
			Q.push_back({u, lca});
			Q.push_back({v, lca});
		}
	}
	for(auto& it: Q) {
		int now, anc; tie(now, anc) = it;
		mrg(now, anc);
	}
	for(int i=1; i<=N; i++) {
		for(auto& it: adj[i]) {
			if(i < it) {
				int u = root(S[i]), v = root(S[it]);
				if(u != v) {
					++deg[u];
					++deg[v];
				}
			}
		}
	}
	int cnt = 0;
	for(int i=1; i<=K; i++) if(deg[i] == 1) ++cnt;
	printf("%d", (cnt+1) / 2);
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:62:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=1; j<A[i].size(); j++) {
                ~^~~~~~~~~~~~
mergers.cpp:43:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int N, K; scanf("%d%d",&N,&K);
            ~~~~~^~~~~~~~~~~~~~
mergers.cpp:46:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d%d",&u,&v);
             ~~~~~^~~~~~~~~~~~~~
mergers.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&S[i]);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 23 ms 23936 KB Output is correct
3 Correct 23 ms 23936 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 21 ms 23804 KB Output is correct
10 Correct 23 ms 23936 KB Output is correct
11 Correct 22 ms 23936 KB Output is correct
12 Correct 24 ms 23936 KB Output is correct
13 Correct 23 ms 23920 KB Output is correct
14 Correct 23 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23808 KB Output is correct
17 Correct 23 ms 23936 KB Output is correct
18 Correct 23 ms 23936 KB Output is correct
19 Correct 22 ms 23808 KB Output is correct
20 Correct 21 ms 23928 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 21 ms 23928 KB Output is correct
23 Correct 22 ms 23960 KB Output is correct
24 Correct 22 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 23 ms 23936 KB Output is correct
3 Correct 23 ms 23936 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 21 ms 23804 KB Output is correct
10 Correct 23 ms 23936 KB Output is correct
11 Correct 22 ms 23936 KB Output is correct
12 Correct 24 ms 23936 KB Output is correct
13 Correct 23 ms 23920 KB Output is correct
14 Correct 23 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23808 KB Output is correct
17 Correct 23 ms 23936 KB Output is correct
18 Correct 23 ms 23936 KB Output is correct
19 Correct 22 ms 23808 KB Output is correct
20 Correct 21 ms 23928 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 21 ms 23928 KB Output is correct
23 Correct 22 ms 23960 KB Output is correct
24 Correct 22 ms 23808 KB Output is correct
25 Correct 22 ms 23936 KB Output is correct
26 Correct 24 ms 24284 KB Output is correct
27 Correct 24 ms 24320 KB Output is correct
28 Correct 26 ms 24516 KB Output is correct
29 Correct 24 ms 24448 KB Output is correct
30 Correct 24 ms 24320 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 25 ms 24472 KB Output is correct
33 Correct 22 ms 23936 KB Output is correct
34 Correct 25 ms 24320 KB Output is correct
35 Correct 24 ms 24420 KB Output is correct
36 Correct 24 ms 24440 KB Output is correct
37 Correct 25 ms 24432 KB Output is correct
38 Correct 22 ms 23936 KB Output is correct
39 Correct 25 ms 24448 KB Output is correct
40 Correct 24 ms 24448 KB Output is correct
41 Correct 23 ms 24420 KB Output is correct
42 Correct 25 ms 24320 KB Output is correct
43 Correct 26 ms 24576 KB Output is correct
44 Correct 21 ms 23928 KB Output is correct
45 Correct 24 ms 24440 KB Output is correct
46 Correct 25 ms 24320 KB Output is correct
47 Correct 22 ms 23936 KB Output is correct
48 Correct 24 ms 24440 KB Output is correct
49 Correct 24 ms 24440 KB Output is correct
50 Correct 25 ms 24448 KB Output is correct
51 Correct 24 ms 24312 KB Output is correct
52 Correct 24 ms 24320 KB Output is correct
53 Correct 26 ms 24416 KB Output is correct
54 Correct 24 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 23 ms 23936 KB Output is correct
3 Correct 23 ms 23936 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 21 ms 23804 KB Output is correct
10 Correct 23 ms 23936 KB Output is correct
11 Correct 22 ms 23936 KB Output is correct
12 Correct 24 ms 23936 KB Output is correct
13 Correct 23 ms 23920 KB Output is correct
14 Correct 23 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23808 KB Output is correct
17 Correct 23 ms 23936 KB Output is correct
18 Correct 23 ms 23936 KB Output is correct
19 Correct 22 ms 23808 KB Output is correct
20 Correct 21 ms 23928 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 21 ms 23928 KB Output is correct
23 Correct 22 ms 23960 KB Output is correct
24 Correct 22 ms 23808 KB Output is correct
25 Correct 21 ms 23936 KB Output is correct
26 Correct 212 ms 38984 KB Output is correct
27 Correct 219 ms 38636 KB Output is correct
28 Correct 25 ms 24440 KB Output is correct
29 Correct 21 ms 23936 KB Output is correct
30 Correct 22 ms 23936 KB Output is correct
31 Correct 179 ms 38648 KB Output is correct
32 Correct 25 ms 24448 KB Output is correct
33 Correct 152 ms 42988 KB Output is correct
34 Correct 192 ms 38800 KB Output is correct
35 Correct 25 ms 24440 KB Output is correct
36 Correct 134 ms 39032 KB Output is correct
37 Correct 24 ms 24448 KB Output is correct
38 Correct 25 ms 24440 KB Output is correct
39 Correct 205 ms 38872 KB Output is correct
40 Correct 24 ms 24440 KB Output is correct
41 Correct 186 ms 38728 KB Output is correct
42 Correct 147 ms 40092 KB Output is correct
43 Correct 24 ms 23936 KB Output is correct
44 Correct 131 ms 43248 KB Output is correct
45 Correct 223 ms 41264 KB Output is correct
46 Correct 25 ms 24448 KB Output is correct
47 Correct 24 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 38916 KB Output is correct
2 Correct 111 ms 40068 KB Output is correct
3 Correct 25 ms 24312 KB Output is correct
4 Correct 24 ms 24448 KB Output is correct
5 Correct 22 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 24 ms 24320 KB Output is correct
8 Correct 208 ms 38768 KB Output is correct
9 Correct 24 ms 24312 KB Output is correct
10 Correct 199 ms 39104 KB Output is correct
11 Correct 26 ms 23928 KB Output is correct
12 Correct 159 ms 38892 KB Output is correct
13 Correct 206 ms 39028 KB Output is correct
14 Correct 146 ms 40164 KB Output is correct
15 Correct 240 ms 39224 KB Output is correct
16 Correct 26 ms 24312 KB Output is correct
17 Correct 23 ms 23996 KB Output is correct
18 Correct 139 ms 40420 KB Output is correct
19 Correct 153 ms 42568 KB Output is correct
20 Correct 25 ms 24320 KB Output is correct
21 Correct 40 ms 23936 KB Output is correct
22 Correct 180 ms 39408 KB Output is correct
23 Correct 24 ms 24320 KB Output is correct
24 Correct 160 ms 39228 KB Output is correct
25 Correct 166 ms 41720 KB Output is correct
26 Correct 25 ms 24320 KB Output is correct
27 Correct 24 ms 24448 KB Output is correct
28 Correct 24 ms 24316 KB Output is correct
29 Correct 25 ms 24448 KB Output is correct
30 Correct 25 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 23 ms 23936 KB Output is correct
3 Correct 23 ms 23936 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 21 ms 23804 KB Output is correct
10 Correct 23 ms 23936 KB Output is correct
11 Correct 22 ms 23936 KB Output is correct
12 Correct 24 ms 23936 KB Output is correct
13 Correct 23 ms 23920 KB Output is correct
14 Correct 23 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23808 KB Output is correct
17 Correct 23 ms 23936 KB Output is correct
18 Correct 23 ms 23936 KB Output is correct
19 Correct 22 ms 23808 KB Output is correct
20 Correct 21 ms 23928 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 21 ms 23928 KB Output is correct
23 Correct 22 ms 23960 KB Output is correct
24 Correct 22 ms 23808 KB Output is correct
25 Correct 22 ms 23936 KB Output is correct
26 Correct 24 ms 24284 KB Output is correct
27 Correct 24 ms 24320 KB Output is correct
28 Correct 26 ms 24516 KB Output is correct
29 Correct 24 ms 24448 KB Output is correct
30 Correct 24 ms 24320 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 25 ms 24472 KB Output is correct
33 Correct 22 ms 23936 KB Output is correct
34 Correct 25 ms 24320 KB Output is correct
35 Correct 24 ms 24420 KB Output is correct
36 Correct 24 ms 24440 KB Output is correct
37 Correct 25 ms 24432 KB Output is correct
38 Correct 22 ms 23936 KB Output is correct
39 Correct 25 ms 24448 KB Output is correct
40 Correct 24 ms 24448 KB Output is correct
41 Correct 23 ms 24420 KB Output is correct
42 Correct 25 ms 24320 KB Output is correct
43 Correct 26 ms 24576 KB Output is correct
44 Correct 21 ms 23928 KB Output is correct
45 Correct 24 ms 24440 KB Output is correct
46 Correct 25 ms 24320 KB Output is correct
47 Correct 22 ms 23936 KB Output is correct
48 Correct 24 ms 24440 KB Output is correct
49 Correct 24 ms 24440 KB Output is correct
50 Correct 25 ms 24448 KB Output is correct
51 Correct 24 ms 24312 KB Output is correct
52 Correct 24 ms 24320 KB Output is correct
53 Correct 26 ms 24416 KB Output is correct
54 Correct 24 ms 24320 KB Output is correct
55 Correct 21 ms 23936 KB Output is correct
56 Correct 212 ms 38984 KB Output is correct
57 Correct 219 ms 38636 KB Output is correct
58 Correct 25 ms 24440 KB Output is correct
59 Correct 21 ms 23936 KB Output is correct
60 Correct 22 ms 23936 KB Output is correct
61 Correct 179 ms 38648 KB Output is correct
62 Correct 25 ms 24448 KB Output is correct
63 Correct 152 ms 42988 KB Output is correct
64 Correct 192 ms 38800 KB Output is correct
65 Correct 25 ms 24440 KB Output is correct
66 Correct 134 ms 39032 KB Output is correct
67 Correct 24 ms 24448 KB Output is correct
68 Correct 25 ms 24440 KB Output is correct
69 Correct 205 ms 38872 KB Output is correct
70 Correct 24 ms 24440 KB Output is correct
71 Correct 186 ms 38728 KB Output is correct
72 Correct 147 ms 40092 KB Output is correct
73 Correct 24 ms 23936 KB Output is correct
74 Correct 131 ms 43248 KB Output is correct
75 Correct 223 ms 41264 KB Output is correct
76 Correct 25 ms 24448 KB Output is correct
77 Correct 24 ms 24320 KB Output is correct
78 Correct 222 ms 38916 KB Output is correct
79 Correct 111 ms 40068 KB Output is correct
80 Correct 25 ms 24312 KB Output is correct
81 Correct 24 ms 24448 KB Output is correct
82 Correct 22 ms 23936 KB Output is correct
83 Correct 21 ms 23936 KB Output is correct
84 Correct 24 ms 24320 KB Output is correct
85 Correct 208 ms 38768 KB Output is correct
86 Correct 24 ms 24312 KB Output is correct
87 Correct 199 ms 39104 KB Output is correct
88 Correct 26 ms 23928 KB Output is correct
89 Correct 159 ms 38892 KB Output is correct
90 Correct 206 ms 39028 KB Output is correct
91 Correct 146 ms 40164 KB Output is correct
92 Correct 240 ms 39224 KB Output is correct
93 Correct 26 ms 24312 KB Output is correct
94 Correct 23 ms 23996 KB Output is correct
95 Correct 139 ms 40420 KB Output is correct
96 Correct 153 ms 42568 KB Output is correct
97 Correct 25 ms 24320 KB Output is correct
98 Correct 40 ms 23936 KB Output is correct
99 Correct 180 ms 39408 KB Output is correct
100 Correct 24 ms 24320 KB Output is correct
101 Correct 160 ms 39228 KB Output is correct
102 Correct 166 ms 41720 KB Output is correct
103 Correct 25 ms 24320 KB Output is correct
104 Correct 24 ms 24448 KB Output is correct
105 Correct 24 ms 24316 KB Output is correct
106 Correct 25 ms 24448 KB Output is correct
107 Correct 25 ms 24320 KB Output is correct
108 Correct 1417 ms 102748 KB Output is correct
109 Correct 913 ms 106736 KB Output is correct
110 Correct 955 ms 113440 KB Output is correct
111 Correct 1084 ms 118960 KB Output is correct
112 Correct 1050 ms 113116 KB Output is correct
113 Correct 871 ms 105604 KB Output is correct
114 Correct 1384 ms 96232 KB Output is correct
115 Correct 1496 ms 96232 KB Output is correct
116 Correct 2100 ms 99500 KB Output is correct
117 Correct 1174 ms 105220 KB Output is correct
118 Correct 1840 ms 96248 KB Output is correct
119 Correct 1150 ms 105284 KB Output is correct
120 Correct 1086 ms 113308 KB Output is correct
121 Correct 1081 ms 105316 KB Output is correct
122 Correct 1740 ms 100616 KB Output is correct
123 Correct 774 ms 106692 KB Output is correct
124 Correct 1392 ms 103544 KB Output is correct