답안 #106174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106174 2019-04-17T04:07:37 Z mzhao Mergers (JOI19_mergers) C++11
100 / 100
1811 ms 177340 KB
#include <bits/stdc++.h>
using namespace std;

#define MN 500100
#define x first
#define y second

int N, K, S[MN], tot[MN], ans, g[MN];
vector<int> adj[MN], adj2[MN];
bool bad[MN];
map<int, int>* m[MN];

int f(int x) { if (g[x] != x) g[x] = f(g[x]); return g[x]; }
void merge(int a, int b) { if (f(a) != f(b)) g[f(a)] = f(b); }

map<int, int>* dfs(int n, int p) {
	m[n] = new map<int, int>;
	if (tot[S[n]] > 1) (*m[n])[S[n]] = 1;
	for (int i : adj[n]) if (i != p) {
		map<int, int>* cur = dfs(i, n);
		if (cur->size() > m[n]->size()) swap(cur, m[n]);
		for (auto j : *cur) {
			(*m[n])[j.x] += j.y;
			if ((*m[n])[j.x] == tot[j.x]) m[n]->erase(j.x);
		}
	}
	if (p) {
		if (m[n]->size() == 0) bad[n] = 1;
		else merge(n, p);
	}
	return m[n];
}

void dfs2(int n, int p) {
	if (adj2[n].size() == 1) ans++;
	for (int i : adj2[n]) if (i != p) dfs2(i, n);
}

int main() {
	scanf("%d%d", &N, &K);
	for (int i = 1, A, B; i < N; i++) {
		scanf("%d%d", &A, &B);
		adj[A].push_back(B);
		adj[B].push_back(A);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d", &S[i]);
		tot[S[i]]++;
		g[i] = i;
	}
	dfs(1, 0);
	for (int i = 1; i <= N; i++) for (int j : adj[i]) {
		int a = f(i), b = f(j);
		if (a != b) adj2[a].push_back(b);
	}
	dfs2(f(1), 0);
	printf("%d\n", (ans+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:40: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:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &A, &B);
   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:47: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 28 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 23 ms 23808 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23804 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 25 ms 23936 KB Output is correct
12 Correct 28 ms 23936 KB Output is correct
13 Correct 25 ms 23856 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 29 ms 23908 KB Output is correct
16 Correct 27 ms 23808 KB Output is correct
17 Correct 25 ms 23876 KB Output is correct
18 Correct 25 ms 23800 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 26 ms 23808 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 23 ms 23936 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 23 ms 23808 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23804 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 25 ms 23936 KB Output is correct
12 Correct 28 ms 23936 KB Output is correct
13 Correct 25 ms 23856 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 29 ms 23908 KB Output is correct
16 Correct 27 ms 23808 KB Output is correct
17 Correct 25 ms 23876 KB Output is correct
18 Correct 25 ms 23800 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 26 ms 23808 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 23 ms 23936 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 26 ms 23908 KB Output is correct
26 Correct 28 ms 24292 KB Output is correct
27 Correct 32 ms 24316 KB Output is correct
28 Correct 32 ms 24440 KB Output is correct
29 Correct 28 ms 24208 KB Output is correct
30 Correct 35 ms 24568 KB Output is correct
31 Correct 25 ms 23808 KB Output is correct
32 Correct 26 ms 24356 KB Output is correct
33 Correct 22 ms 23808 KB Output is correct
34 Correct 26 ms 24320 KB Output is correct
35 Correct 32 ms 24312 KB Output is correct
36 Correct 31 ms 24284 KB Output is correct
37 Correct 29 ms 24568 KB Output is correct
38 Correct 23 ms 23800 KB Output is correct
39 Correct 30 ms 24576 KB Output is correct
40 Correct 31 ms 24312 KB Output is correct
41 Correct 26 ms 24320 KB Output is correct
42 Correct 25 ms 24448 KB Output is correct
43 Correct 25 ms 24576 KB Output is correct
44 Correct 29 ms 23936 KB Output is correct
45 Correct 26 ms 24348 KB Output is correct
46 Correct 27 ms 24448 KB Output is correct
47 Correct 25 ms 23936 KB Output is correct
48 Correct 27 ms 24320 KB Output is correct
49 Correct 25 ms 24320 KB Output is correct
50 Correct 23 ms 24320 KB Output is correct
51 Correct 28 ms 24440 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 25 ms 24320 KB Output is correct
54 Correct 30 ms 24540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 23 ms 23808 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23804 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 25 ms 23936 KB Output is correct
12 Correct 28 ms 23936 KB Output is correct
13 Correct 25 ms 23856 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 29 ms 23908 KB Output is correct
16 Correct 27 ms 23808 KB Output is correct
17 Correct 25 ms 23876 KB Output is correct
18 Correct 25 ms 23800 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 26 ms 23808 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 23 ms 23936 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 23 ms 23808 KB Output is correct
26 Correct 154 ms 41268 KB Output is correct
27 Correct 216 ms 47796 KB Output is correct
28 Correct 24 ms 24320 KB Output is correct
29 Correct 22 ms 23936 KB Output is correct
30 Correct 23 ms 23928 KB Output is correct
31 Correct 148 ms 41052 KB Output is correct
32 Correct 26 ms 24320 KB Output is correct
33 Correct 201 ms 51064 KB Output is correct
34 Correct 211 ms 46456 KB Output is correct
35 Correct 28 ms 24320 KB Output is correct
36 Correct 185 ms 42104 KB Output is correct
37 Correct 27 ms 24320 KB Output is correct
38 Correct 24 ms 24312 KB Output is correct
39 Correct 130 ms 41172 KB Output is correct
40 Correct 26 ms 24576 KB Output is correct
41 Correct 189 ms 40940 KB Output is correct
42 Correct 157 ms 44408 KB Output is correct
43 Correct 21 ms 23808 KB Output is correct
44 Correct 182 ms 51588 KB Output is correct
45 Correct 198 ms 47352 KB Output is correct
46 Correct 24 ms 24416 KB Output is correct
47 Correct 24 ms 24448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 41200 KB Output is correct
2 Correct 128 ms 40944 KB Output is correct
3 Correct 25 ms 24312 KB Output is correct
4 Correct 27 ms 24440 KB Output is correct
5 Correct 22 ms 23928 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 23 ms 24320 KB Output is correct
8 Correct 166 ms 44380 KB Output is correct
9 Correct 28 ms 24320 KB Output is correct
10 Correct 162 ms 41336 KB Output is correct
11 Correct 22 ms 23936 KB Output is correct
12 Correct 157 ms 41080 KB Output is correct
13 Correct 206 ms 41308 KB Output is correct
14 Correct 152 ms 40312 KB Output is correct
15 Correct 145 ms 41232 KB Output is correct
16 Correct 28 ms 24448 KB Output is correct
17 Correct 25 ms 23936 KB Output is correct
18 Correct 213 ms 41316 KB Output is correct
19 Correct 186 ms 45688 KB Output is correct
20 Correct 26 ms 24440 KB Output is correct
21 Correct 26 ms 23800 KB Output is correct
22 Correct 220 ms 41584 KB Output is correct
23 Correct 27 ms 24312 KB Output is correct
24 Correct 194 ms 41208 KB Output is correct
25 Correct 238 ms 43896 KB Output is correct
26 Correct 28 ms 24440 KB Output is correct
27 Correct 24 ms 24312 KB Output is correct
28 Correct 24 ms 24292 KB Output is correct
29 Correct 24 ms 24312 KB Output is correct
30 Correct 30 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 23 ms 23808 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23804 KB Output is correct
8 Correct 23 ms 23936 KB Output is correct
9 Correct 24 ms 23808 KB Output is correct
10 Correct 25 ms 23808 KB Output is correct
11 Correct 25 ms 23936 KB Output is correct
12 Correct 28 ms 23936 KB Output is correct
13 Correct 25 ms 23856 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 29 ms 23908 KB Output is correct
16 Correct 27 ms 23808 KB Output is correct
17 Correct 25 ms 23876 KB Output is correct
18 Correct 25 ms 23800 KB Output is correct
19 Correct 26 ms 23808 KB Output is correct
20 Correct 26 ms 23808 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 23 ms 23936 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 26 ms 23908 KB Output is correct
26 Correct 28 ms 24292 KB Output is correct
27 Correct 32 ms 24316 KB Output is correct
28 Correct 32 ms 24440 KB Output is correct
29 Correct 28 ms 24208 KB Output is correct
30 Correct 35 ms 24568 KB Output is correct
31 Correct 25 ms 23808 KB Output is correct
32 Correct 26 ms 24356 KB Output is correct
33 Correct 22 ms 23808 KB Output is correct
34 Correct 26 ms 24320 KB Output is correct
35 Correct 32 ms 24312 KB Output is correct
36 Correct 31 ms 24284 KB Output is correct
37 Correct 29 ms 24568 KB Output is correct
38 Correct 23 ms 23800 KB Output is correct
39 Correct 30 ms 24576 KB Output is correct
40 Correct 31 ms 24312 KB Output is correct
41 Correct 26 ms 24320 KB Output is correct
42 Correct 25 ms 24448 KB Output is correct
43 Correct 25 ms 24576 KB Output is correct
44 Correct 29 ms 23936 KB Output is correct
45 Correct 26 ms 24348 KB Output is correct
46 Correct 27 ms 24448 KB Output is correct
47 Correct 25 ms 23936 KB Output is correct
48 Correct 27 ms 24320 KB Output is correct
49 Correct 25 ms 24320 KB Output is correct
50 Correct 23 ms 24320 KB Output is correct
51 Correct 28 ms 24440 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 25 ms 24320 KB Output is correct
54 Correct 30 ms 24540 KB Output is correct
55 Correct 23 ms 23808 KB Output is correct
56 Correct 154 ms 41268 KB Output is correct
57 Correct 216 ms 47796 KB Output is correct
58 Correct 24 ms 24320 KB Output is correct
59 Correct 22 ms 23936 KB Output is correct
60 Correct 23 ms 23928 KB Output is correct
61 Correct 148 ms 41052 KB Output is correct
62 Correct 26 ms 24320 KB Output is correct
63 Correct 201 ms 51064 KB Output is correct
64 Correct 211 ms 46456 KB Output is correct
65 Correct 28 ms 24320 KB Output is correct
66 Correct 185 ms 42104 KB Output is correct
67 Correct 27 ms 24320 KB Output is correct
68 Correct 24 ms 24312 KB Output is correct
69 Correct 130 ms 41172 KB Output is correct
70 Correct 26 ms 24576 KB Output is correct
71 Correct 189 ms 40940 KB Output is correct
72 Correct 157 ms 44408 KB Output is correct
73 Correct 21 ms 23808 KB Output is correct
74 Correct 182 ms 51588 KB Output is correct
75 Correct 198 ms 47352 KB Output is correct
76 Correct 24 ms 24416 KB Output is correct
77 Correct 24 ms 24448 KB Output is correct
78 Correct 121 ms 41200 KB Output is correct
79 Correct 128 ms 40944 KB Output is correct
80 Correct 25 ms 24312 KB Output is correct
81 Correct 27 ms 24440 KB Output is correct
82 Correct 22 ms 23928 KB Output is correct
83 Correct 26 ms 23928 KB Output is correct
84 Correct 23 ms 24320 KB Output is correct
85 Correct 166 ms 44380 KB Output is correct
86 Correct 28 ms 24320 KB Output is correct
87 Correct 162 ms 41336 KB Output is correct
88 Correct 22 ms 23936 KB Output is correct
89 Correct 157 ms 41080 KB Output is correct
90 Correct 206 ms 41308 KB Output is correct
91 Correct 152 ms 40312 KB Output is correct
92 Correct 145 ms 41232 KB Output is correct
93 Correct 28 ms 24448 KB Output is correct
94 Correct 25 ms 23936 KB Output is correct
95 Correct 213 ms 41316 KB Output is correct
96 Correct 186 ms 45688 KB Output is correct
97 Correct 26 ms 24440 KB Output is correct
98 Correct 26 ms 23800 KB Output is correct
99 Correct 220 ms 41584 KB Output is correct
100 Correct 27 ms 24312 KB Output is correct
101 Correct 194 ms 41208 KB Output is correct
102 Correct 238 ms 43896 KB Output is correct
103 Correct 28 ms 24440 KB Output is correct
104 Correct 24 ms 24312 KB Output is correct
105 Correct 24 ms 24292 KB Output is correct
106 Correct 24 ms 24312 KB Output is correct
107 Correct 30 ms 24320 KB Output is correct
108 Correct 1487 ms 112856 KB Output is correct
109 Correct 1248 ms 134612 KB Output is correct
110 Correct 1309 ms 150288 KB Output is correct
111 Correct 1317 ms 134904 KB Output is correct
112 Correct 1085 ms 116180 KB Output is correct
113 Correct 1105 ms 108248 KB Output is correct
114 Correct 1120 ms 109076 KB Output is correct
115 Correct 1173 ms 109120 KB Output is correct
116 Correct 1440 ms 167216 KB Output is correct
117 Correct 1095 ms 104316 KB Output is correct
118 Correct 1079 ms 108036 KB Output is correct
119 Correct 1235 ms 104184 KB Output is correct
120 Correct 1300 ms 123500 KB Output is correct
121 Correct 1109 ms 104092 KB Output is correct
122 Correct 1811 ms 177340 KB Output is correct
123 Correct 910 ms 107028 KB Output is correct
124 Correct 1256 ms 96988 KB Output is correct