답안 #120701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120701 2019-06-25T09:12:55 Z onjo0127 Mergers (JOI19_mergers) C++11
100 / 100
1985 ms 118748 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]);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 22 ms 23936 KB Output is correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 22 ms 23928 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 22 ms 23908 KB Output is correct
11 Correct 21 ms 23936 KB Output is correct
12 Correct 21 ms 23848 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23844 KB Output is correct
17 Correct 22 ms 23936 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23904 KB Output is correct
21 Correct 23 ms 23908 KB Output is correct
22 Correct 22 ms 23852 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 22 ms 23936 KB Output is correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 22 ms 23928 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 22 ms 23908 KB Output is correct
11 Correct 21 ms 23936 KB Output is correct
12 Correct 21 ms 23848 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23844 KB Output is correct
17 Correct 22 ms 23936 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23904 KB Output is correct
21 Correct 23 ms 23908 KB Output is correct
22 Correct 22 ms 23852 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 22 ms 23936 KB Output is correct
26 Correct 24 ms 24312 KB Output is correct
27 Correct 26 ms 24316 KB Output is correct
28 Correct 26 ms 24440 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 25 ms 24320 KB Output is correct
31 Correct 22 ms 23928 KB Output is correct
32 Correct 24 ms 24508 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 25 ms 24312 KB Output is correct
35 Correct 25 ms 24312 KB Output is correct
36 Correct 24 ms 24312 KB Output is correct
37 Correct 25 ms 24320 KB Output is correct
38 Correct 21 ms 23936 KB Output is correct
39 Correct 25 ms 24320 KB Output is correct
40 Correct 24 ms 24320 KB Output is correct
41 Correct 24 ms 24312 KB Output is correct
42 Correct 25 ms 24448 KB Output is correct
43 Correct 28 ms 24544 KB Output is correct
44 Correct 21 ms 23928 KB Output is correct
45 Correct 26 ms 24448 KB Output is correct
46 Correct 24 ms 24320 KB Output is correct
47 Correct 22 ms 23936 KB Output is correct
48 Correct 24 ms 24312 KB Output is correct
49 Correct 33 ms 24356 KB Output is correct
50 Correct 25 ms 24320 KB Output is correct
51 Correct 25 ms 24320 KB Output is correct
52 Correct 25 ms 24448 KB Output is correct
53 Correct 24 ms 24448 KB Output is correct
54 Correct 25 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 22 ms 23936 KB Output is correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 22 ms 23928 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 22 ms 23908 KB Output is correct
11 Correct 21 ms 23936 KB Output is correct
12 Correct 21 ms 23848 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23844 KB Output is correct
17 Correct 22 ms 23936 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23904 KB Output is correct
21 Correct 23 ms 23908 KB Output is correct
22 Correct 22 ms 23852 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 22 ms 23932 KB Output is correct
26 Correct 213 ms 38864 KB Output is correct
27 Correct 253 ms 38544 KB Output is correct
28 Correct 25 ms 24320 KB Output is correct
29 Correct 22 ms 23932 KB Output is correct
30 Correct 23 ms 23936 KB Output is correct
31 Correct 228 ms 38388 KB Output is correct
32 Correct 26 ms 24448 KB Output is correct
33 Correct 166 ms 42776 KB Output is correct
34 Correct 246 ms 38596 KB Output is correct
35 Correct 24 ms 24320 KB Output is correct
36 Correct 143 ms 38776 KB Output is correct
37 Correct 25 ms 24320 KB Output is correct
38 Correct 25 ms 24420 KB Output is correct
39 Correct 218 ms 38912 KB Output is correct
40 Correct 24 ms 24448 KB Output is correct
41 Correct 192 ms 38620 KB Output is correct
42 Correct 157 ms 39788 KB Output is correct
43 Correct 21 ms 23900 KB Output is correct
44 Correct 141 ms 42736 KB Output is correct
45 Correct 207 ms 40660 KB Output is correct
46 Correct 24 ms 24440 KB Output is correct
47 Correct 25 ms 24420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 38592 KB Output is correct
2 Correct 123 ms 39832 KB Output is correct
3 Correct 27 ms 24440 KB Output is correct
4 Correct 25 ms 24404 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 22 ms 23872 KB Output is correct
7 Correct 24 ms 24316 KB Output is correct
8 Correct 241 ms 38836 KB Output is correct
9 Correct 35 ms 24440 KB Output is correct
10 Correct 211 ms 38588 KB Output is correct
11 Correct 21 ms 23936 KB Output is correct
12 Correct 140 ms 38572 KB Output is correct
13 Correct 203 ms 38644 KB Output is correct
14 Correct 127 ms 39644 KB Output is correct
15 Correct 234 ms 38728 KB Output is correct
16 Correct 25 ms 24320 KB Output is correct
17 Correct 23 ms 23936 KB Output is correct
18 Correct 132 ms 39920 KB Output is correct
19 Correct 147 ms 42004 KB Output is correct
20 Correct 24 ms 24320 KB Output is correct
21 Correct 22 ms 23960 KB Output is correct
22 Correct 184 ms 39020 KB Output is correct
23 Correct 26 ms 24320 KB Output is correct
24 Correct 159 ms 38812 KB Output is correct
25 Correct 164 ms 41116 KB Output is correct
26 Correct 25 ms 24320 KB Output is correct
27 Correct 25 ms 24320 KB Output is correct
28 Correct 26 ms 24428 KB Output is correct
29 Correct 25 ms 24448 KB Output is correct
30 Correct 25 ms 24380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 22 ms 23936 KB Output is correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 22 ms 23928 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 22 ms 23908 KB Output is correct
11 Correct 21 ms 23936 KB Output is correct
12 Correct 21 ms 23848 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23936 KB Output is correct
15 Correct 23 ms 23936 KB Output is correct
16 Correct 23 ms 23844 KB Output is correct
17 Correct 22 ms 23936 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23904 KB Output is correct
21 Correct 23 ms 23908 KB Output is correct
22 Correct 22 ms 23852 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 22 ms 23936 KB Output is correct
26 Correct 24 ms 24312 KB Output is correct
27 Correct 26 ms 24316 KB Output is correct
28 Correct 26 ms 24440 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 25 ms 24320 KB Output is correct
31 Correct 22 ms 23928 KB Output is correct
32 Correct 24 ms 24508 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 25 ms 24312 KB Output is correct
35 Correct 25 ms 24312 KB Output is correct
36 Correct 24 ms 24312 KB Output is correct
37 Correct 25 ms 24320 KB Output is correct
38 Correct 21 ms 23936 KB Output is correct
39 Correct 25 ms 24320 KB Output is correct
40 Correct 24 ms 24320 KB Output is correct
41 Correct 24 ms 24312 KB Output is correct
42 Correct 25 ms 24448 KB Output is correct
43 Correct 28 ms 24544 KB Output is correct
44 Correct 21 ms 23928 KB Output is correct
45 Correct 26 ms 24448 KB Output is correct
46 Correct 24 ms 24320 KB Output is correct
47 Correct 22 ms 23936 KB Output is correct
48 Correct 24 ms 24312 KB Output is correct
49 Correct 33 ms 24356 KB Output is correct
50 Correct 25 ms 24320 KB Output is correct
51 Correct 25 ms 24320 KB Output is correct
52 Correct 25 ms 24448 KB Output is correct
53 Correct 24 ms 24448 KB Output is correct
54 Correct 25 ms 24320 KB Output is correct
55 Correct 22 ms 23932 KB Output is correct
56 Correct 213 ms 38864 KB Output is correct
57 Correct 253 ms 38544 KB Output is correct
58 Correct 25 ms 24320 KB Output is correct
59 Correct 22 ms 23932 KB Output is correct
60 Correct 23 ms 23936 KB Output is correct
61 Correct 228 ms 38388 KB Output is correct
62 Correct 26 ms 24448 KB Output is correct
63 Correct 166 ms 42776 KB Output is correct
64 Correct 246 ms 38596 KB Output is correct
65 Correct 24 ms 24320 KB Output is correct
66 Correct 143 ms 38776 KB Output is correct
67 Correct 25 ms 24320 KB Output is correct
68 Correct 25 ms 24420 KB Output is correct
69 Correct 218 ms 38912 KB Output is correct
70 Correct 24 ms 24448 KB Output is correct
71 Correct 192 ms 38620 KB Output is correct
72 Correct 157 ms 39788 KB Output is correct
73 Correct 21 ms 23900 KB Output is correct
74 Correct 141 ms 42736 KB Output is correct
75 Correct 207 ms 40660 KB Output is correct
76 Correct 24 ms 24440 KB Output is correct
77 Correct 25 ms 24420 KB Output is correct
78 Correct 199 ms 38592 KB Output is correct
79 Correct 123 ms 39832 KB Output is correct
80 Correct 27 ms 24440 KB Output is correct
81 Correct 25 ms 24404 KB Output is correct
82 Correct 22 ms 23928 KB Output is correct
83 Correct 22 ms 23872 KB Output is correct
84 Correct 24 ms 24316 KB Output is correct
85 Correct 241 ms 38836 KB Output is correct
86 Correct 35 ms 24440 KB Output is correct
87 Correct 211 ms 38588 KB Output is correct
88 Correct 21 ms 23936 KB Output is correct
89 Correct 140 ms 38572 KB Output is correct
90 Correct 203 ms 38644 KB Output is correct
91 Correct 127 ms 39644 KB Output is correct
92 Correct 234 ms 38728 KB Output is correct
93 Correct 25 ms 24320 KB Output is correct
94 Correct 23 ms 23936 KB Output is correct
95 Correct 132 ms 39920 KB Output is correct
96 Correct 147 ms 42004 KB Output is correct
97 Correct 24 ms 24320 KB Output is correct
98 Correct 22 ms 23960 KB Output is correct
99 Correct 184 ms 39020 KB Output is correct
100 Correct 26 ms 24320 KB Output is correct
101 Correct 159 ms 38812 KB Output is correct
102 Correct 164 ms 41116 KB Output is correct
103 Correct 25 ms 24320 KB Output is correct
104 Correct 25 ms 24320 KB Output is correct
105 Correct 26 ms 24428 KB Output is correct
106 Correct 25 ms 24448 KB Output is correct
107 Correct 25 ms 24380 KB Output is correct
108 Correct 1463 ms 102404 KB Output is correct
109 Correct 866 ms 106280 KB Output is correct
110 Correct 863 ms 113032 KB Output is correct
111 Correct 1114 ms 118748 KB Output is correct
112 Correct 1061 ms 112664 KB Output is correct
113 Correct 918 ms 105336 KB Output is correct
114 Correct 1401 ms 95688 KB Output is correct
115 Correct 1455 ms 95692 KB Output is correct
116 Correct 1985 ms 99188 KB Output is correct
117 Correct 1069 ms 105208 KB Output is correct
118 Correct 1521 ms 95816 KB Output is correct
119 Correct 1022 ms 105044 KB Output is correct
120 Correct 1092 ms 113256 KB Output is correct
121 Correct 1034 ms 105080 KB Output is correct
122 Correct 1562 ms 100612 KB Output is correct
123 Correct 711 ms 106596 KB Output is correct
124 Correct 1063 ms 103124 KB Output is correct