답안 #355598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
355598 2021-01-22T19:10:54 Z Fischer Mergers (JOI19_mergers) C++14
70 / 100
261 ms 28244 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
vector<int> g[maxn];
vector<int> color[maxn];
const int maxlog = 18;
int st[maxn][maxlog];
int lo[maxn], h[maxn];
int T;

void dfs(int x, int p=-1) {
	lo[x] = T++;
	h[x] = (~p ? h[p] + 1 : 0);
	st[x][0] = (~p ? p : x);
	for (int k = 1; k < maxlog; ++k) {
		st[x][k] = st[st[x][k-1]][k-1];
	}
	for (int v : g[x]) {
		if (v == p) continue;
		dfs(v, x);
	}
}

int lca(int x, int y) {
	if (h[x] > h[y]) swap(x, y);
	for (int i = maxlog-1; i >= 0; --i) {
		if (h[st[y][i]] >= h[x]) {
			y = st[y][i];
		}
	}
	if (x == y) return x;
	for (int i = maxlog-1; i >= 0; --i) {
		if (st[x][i] != st[y][i]) {
			x = st[x][i];
			y = st[y][i];
		}
	}
	return st[x][0];
}

int link[maxn];
int rnk[maxn];

int get(int x) {
	return x == link[x] ? x : link[x] = get(link[x]);
}

void merge(int x, int y) {
	x = get(x);
	y = get(y);
	if (x == y) return;
	if (rnk[x] < rnk[y]) swap(x, y);
	rnk[x] += rnk[x] == rnk[y];
	link[y] = x;
}

vector<pair<int, int>> edges;
int dp[maxn], deg[maxn];
void dfs_in_out(int x, int p=-1) {
	for (int v : g[x]) {
		if (v == p) continue;
		dfs_in_out(v, x);
		dp[x] += dp[v];
	}
	if (~p) {
		if (dp[x] == 0) {
			edges.push_back({x, p});
		} else {
			merge(x, p);
		}
	}
}

int main() {
	int n, k;
	cin >> n >> k; 
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		int d;
		cin >> d;
		color[d].push_back(i);
	}
	dfs(1);
	for (int d = 1; d <= k; ++d) {
		sort(color[d].begin(), color[d].end(), [&](int u, int v) {
			return lo[u] < lo[v];	
		});
		for (int j = 1; j < color[d].size(); ++j) {
			int u = color[d][j-1], v = color[d][j];
			int t = lca(u, v);
			dp[u] += 1;
			dp[v] += 1;
			dp[t] -= 2;
		}
	}
	for (int i = 1; i <= n; ++i) {
		link[i] = i;
		rnk[i] = 0;
	}
	dfs_in_out(1);
	for (auto e : edges) {
		int u = e.first, v = e.second;
		deg[get(u)] += 1;
		deg[get(v)] += 1;
	}
	int leaves = 0;
	for (int i = 1; i <= n; ++i) {
		if (deg[i] == 1 && get(i) == i) {
			leaves += 1;
		}	
	}
	cout << (leaves + 1) / 2 << endl;
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int j = 1; j < color[d].size(); ++j) {
      |                   ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 5 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 5 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 7 ms 5632 KB Output is correct
27 Correct 7 ms 5484 KB Output is correct
28 Correct 7 ms 5740 KB Output is correct
29 Correct 7 ms 5612 KB Output is correct
30 Correct 8 ms 5484 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 7 ms 5740 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 7 ms 5484 KB Output is correct
35 Correct 7 ms 5612 KB Output is correct
36 Correct 7 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 8 ms 5484 KB Output is correct
40 Correct 7 ms 5484 KB Output is correct
41 Correct 7 ms 5484 KB Output is correct
42 Correct 7 ms 5484 KB Output is correct
43 Correct 8 ms 5740 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 7 ms 5612 KB Output is correct
46 Correct 7 ms 5484 KB Output is correct
47 Correct 4 ms 5100 KB Output is correct
48 Correct 8 ms 5484 KB Output is correct
49 Correct 7 ms 5612 KB Output is correct
50 Correct 7 ms 5740 KB Output is correct
51 Correct 7 ms 5484 KB Output is correct
52 Correct 7 ms 5484 KB Output is correct
53 Correct 7 ms 5484 KB Output is correct
54 Correct 8 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 5 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 160 ms 19696 KB Output is correct
27 Correct 200 ms 19180 KB Output is correct
28 Correct 7 ms 5484 KB Output is correct
29 Correct 4 ms 5100 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 192 ms 19436 KB Output is correct
32 Correct 8 ms 5484 KB Output is correct
33 Correct 249 ms 27884 KB Output is correct
34 Correct 216 ms 19308 KB Output is correct
35 Correct 7 ms 5484 KB Output is correct
36 Correct 221 ms 20204 KB Output is correct
37 Correct 10 ms 5484 KB Output is correct
38 Correct 7 ms 5484 KB Output is correct
39 Correct 177 ms 19428 KB Output is correct
40 Correct 8 ms 5740 KB Output is correct
41 Correct 196 ms 19084 KB Output is correct
42 Correct 242 ms 22072 KB Output is correct
43 Correct 5 ms 5084 KB Output is correct
44 Correct 256 ms 28212 KB Output is correct
45 Correct 261 ms 23788 KB Output is correct
46 Correct 7 ms 5484 KB Output is correct
47 Correct 8 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 19684 KB Output is correct
2 Correct 145 ms 23392 KB Output is correct
3 Correct 8 ms 5612 KB Output is correct
4 Correct 8 ms 5484 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 7 ms 5484 KB Output is correct
8 Correct 211 ms 21228 KB Output is correct
9 Correct 8 ms 5484 KB Output is correct
10 Correct 198 ms 19948 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 210 ms 19840 KB Output is correct
13 Correct 199 ms 21232 KB Output is correct
14 Correct 182 ms 23392 KB Output is correct
15 Correct 160 ms 19592 KB Output is correct
16 Correct 7 ms 5484 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 174 ms 23448 KB Output is correct
19 Correct 208 ms 28244 KB Output is correct
20 Correct 8 ms 5484 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 163 ms 21604 KB Output is correct
23 Correct 7 ms 5484 KB Output is correct
24 Correct 218 ms 20280 KB Output is correct
25 Correct 198 ms 26548 KB Output is correct
26 Correct 7 ms 5612 KB Output is correct
27 Correct 7 ms 5740 KB Output is correct
28 Correct 7 ms 5484 KB Output is correct
29 Correct 7 ms 5484 KB Output is correct
30 Correct 4 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 3 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 5 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 7 ms 5632 KB Output is correct
27 Correct 7 ms 5484 KB Output is correct
28 Correct 7 ms 5740 KB Output is correct
29 Correct 7 ms 5612 KB Output is correct
30 Correct 8 ms 5484 KB Output is correct
31 Correct 4 ms 5100 KB Output is correct
32 Correct 7 ms 5740 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 7 ms 5484 KB Output is correct
35 Correct 7 ms 5612 KB Output is correct
36 Correct 7 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 4 ms 5100 KB Output is correct
39 Correct 8 ms 5484 KB Output is correct
40 Correct 7 ms 5484 KB Output is correct
41 Correct 7 ms 5484 KB Output is correct
42 Correct 7 ms 5484 KB Output is correct
43 Correct 8 ms 5740 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 7 ms 5612 KB Output is correct
46 Correct 7 ms 5484 KB Output is correct
47 Correct 4 ms 5100 KB Output is correct
48 Correct 8 ms 5484 KB Output is correct
49 Correct 7 ms 5612 KB Output is correct
50 Correct 7 ms 5740 KB Output is correct
51 Correct 7 ms 5484 KB Output is correct
52 Correct 7 ms 5484 KB Output is correct
53 Correct 7 ms 5484 KB Output is correct
54 Correct 8 ms 5484 KB Output is correct
55 Correct 4 ms 5100 KB Output is correct
56 Correct 160 ms 19696 KB Output is correct
57 Correct 200 ms 19180 KB Output is correct
58 Correct 7 ms 5484 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 4 ms 5120 KB Output is correct
61 Correct 192 ms 19436 KB Output is correct
62 Correct 8 ms 5484 KB Output is correct
63 Correct 249 ms 27884 KB Output is correct
64 Correct 216 ms 19308 KB Output is correct
65 Correct 7 ms 5484 KB Output is correct
66 Correct 221 ms 20204 KB Output is correct
67 Correct 10 ms 5484 KB Output is correct
68 Correct 7 ms 5484 KB Output is correct
69 Correct 177 ms 19428 KB Output is correct
70 Correct 8 ms 5740 KB Output is correct
71 Correct 196 ms 19084 KB Output is correct
72 Correct 242 ms 22072 KB Output is correct
73 Correct 5 ms 5084 KB Output is correct
74 Correct 256 ms 28212 KB Output is correct
75 Correct 261 ms 23788 KB Output is correct
76 Correct 7 ms 5484 KB Output is correct
77 Correct 8 ms 5484 KB Output is correct
78 Correct 159 ms 19684 KB Output is correct
79 Correct 145 ms 23392 KB Output is correct
80 Correct 8 ms 5612 KB Output is correct
81 Correct 8 ms 5484 KB Output is correct
82 Correct 4 ms 5100 KB Output is correct
83 Correct 4 ms 5100 KB Output is correct
84 Correct 7 ms 5484 KB Output is correct
85 Correct 211 ms 21228 KB Output is correct
86 Correct 8 ms 5484 KB Output is correct
87 Correct 198 ms 19948 KB Output is correct
88 Correct 4 ms 5100 KB Output is correct
89 Correct 210 ms 19840 KB Output is correct
90 Correct 199 ms 21232 KB Output is correct
91 Correct 182 ms 23392 KB Output is correct
92 Correct 160 ms 19592 KB Output is correct
93 Correct 7 ms 5484 KB Output is correct
94 Correct 4 ms 5100 KB Output is correct
95 Correct 174 ms 23448 KB Output is correct
96 Correct 208 ms 28244 KB Output is correct
97 Correct 8 ms 5484 KB Output is correct
98 Correct 4 ms 5100 KB Output is correct
99 Correct 163 ms 21604 KB Output is correct
100 Correct 7 ms 5484 KB Output is correct
101 Correct 218 ms 20280 KB Output is correct
102 Correct 198 ms 26548 KB Output is correct
103 Correct 7 ms 5612 KB Output is correct
104 Correct 7 ms 5740 KB Output is correct
105 Correct 7 ms 5484 KB Output is correct
106 Correct 7 ms 5484 KB Output is correct
107 Correct 4 ms 5484 KB Output is correct
108 Runtime error 10 ms 9964 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -