Submission #355601

# Submission time Handle Problem Language Result Execution time Memory
355601 2021-01-22T19:14:32 Z Fischer Mergers (JOI19_mergers) C++14
100 / 100
1519 ms 144248 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e5 + 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) {
      |                   ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 16 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23936 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23916 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 16 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 17 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 16 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 16 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 16 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23936 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23916 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 16 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 17 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 16 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 16 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 21 ms 24448 KB Output is correct
27 Correct 20 ms 24300 KB Output is correct
28 Correct 20 ms 24428 KB Output is correct
29 Correct 20 ms 24428 KB Output is correct
30 Correct 20 ms 24300 KB Output is correct
31 Correct 17 ms 23916 KB Output is correct
32 Correct 20 ms 24556 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 19 ms 24300 KB Output is correct
35 Correct 20 ms 24428 KB Output is correct
36 Correct 23 ms 24300 KB Output is correct
37 Correct 20 ms 24300 KB Output is correct
38 Correct 16 ms 23916 KB Output is correct
39 Correct 20 ms 24300 KB Output is correct
40 Correct 20 ms 24300 KB Output is correct
41 Correct 20 ms 24300 KB Output is correct
42 Correct 23 ms 24300 KB Output is correct
43 Correct 20 ms 24556 KB Output is correct
44 Correct 18 ms 23916 KB Output is correct
45 Correct 22 ms 24356 KB Output is correct
46 Correct 20 ms 24300 KB Output is correct
47 Correct 16 ms 23916 KB Output is correct
48 Correct 20 ms 24300 KB Output is correct
49 Correct 20 ms 24428 KB Output is correct
50 Correct 20 ms 24428 KB Output is correct
51 Correct 20 ms 24300 KB Output is correct
52 Correct 20 ms 24300 KB Output is correct
53 Correct 20 ms 24300 KB Output is correct
54 Correct 20 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 16 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23936 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23916 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 16 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 17 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 16 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 16 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 168 ms 37056 KB Output is correct
27 Correct 223 ms 36716 KB Output is correct
28 Correct 20 ms 24300 KB Output is correct
29 Correct 16 ms 23916 KB Output is correct
30 Correct 17 ms 23916 KB Output is correct
31 Correct 193 ms 36716 KB Output is correct
32 Correct 20 ms 24300 KB Output is correct
33 Correct 279 ms 45256 KB Output is correct
34 Correct 222 ms 36716 KB Output is correct
35 Correct 20 ms 24300 KB Output is correct
36 Correct 220 ms 37740 KB Output is correct
37 Correct 20 ms 24300 KB Output is correct
38 Correct 20 ms 24300 KB Output is correct
39 Correct 169 ms 36964 KB Output is correct
40 Correct 20 ms 24556 KB Output is correct
41 Correct 208 ms 36584 KB Output is correct
42 Correct 231 ms 39660 KB Output is correct
43 Correct 17 ms 23916 KB Output is correct
44 Correct 234 ms 45676 KB Output is correct
45 Correct 261 ms 41216 KB Output is correct
46 Correct 20 ms 24300 KB Output is correct
47 Correct 20 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 36940 KB Output is correct
2 Correct 163 ms 40416 KB Output is correct
3 Correct 22 ms 24300 KB Output is correct
4 Correct 20 ms 24300 KB Output is correct
5 Correct 17 ms 23916 KB Output is correct
6 Correct 16 ms 23936 KB Output is correct
7 Correct 20 ms 24300 KB Output is correct
8 Correct 242 ms 38412 KB Output is correct
9 Correct 20 ms 24428 KB Output is correct
10 Correct 205 ms 37100 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 221 ms 37100 KB Output is correct
13 Correct 221 ms 38384 KB Output is correct
14 Correct 203 ms 40640 KB Output is correct
15 Correct 167 ms 36964 KB Output is correct
16 Correct 20 ms 24300 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 167 ms 40524 KB Output is correct
19 Correct 218 ms 45280 KB Output is correct
20 Correct 20 ms 24300 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Correct 186 ms 38756 KB Output is correct
23 Correct 20 ms 24300 KB Output is correct
24 Correct 213 ms 37740 KB Output is correct
25 Correct 206 ms 43616 KB Output is correct
26 Correct 21 ms 24428 KB Output is correct
27 Correct 20 ms 24428 KB Output is correct
28 Correct 20 ms 24300 KB Output is correct
29 Correct 20 ms 24300 KB Output is correct
30 Correct 20 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 16 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23788 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23936 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23916 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 17 ms 23916 KB Output is correct
18 Correct 16 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 17 ms 23916 KB Output is correct
21 Correct 17 ms 23916 KB Output is correct
22 Correct 16 ms 23916 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 16 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 21 ms 24448 KB Output is correct
27 Correct 20 ms 24300 KB Output is correct
28 Correct 20 ms 24428 KB Output is correct
29 Correct 20 ms 24428 KB Output is correct
30 Correct 20 ms 24300 KB Output is correct
31 Correct 17 ms 23916 KB Output is correct
32 Correct 20 ms 24556 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 19 ms 24300 KB Output is correct
35 Correct 20 ms 24428 KB Output is correct
36 Correct 23 ms 24300 KB Output is correct
37 Correct 20 ms 24300 KB Output is correct
38 Correct 16 ms 23916 KB Output is correct
39 Correct 20 ms 24300 KB Output is correct
40 Correct 20 ms 24300 KB Output is correct
41 Correct 20 ms 24300 KB Output is correct
42 Correct 23 ms 24300 KB Output is correct
43 Correct 20 ms 24556 KB Output is correct
44 Correct 18 ms 23916 KB Output is correct
45 Correct 22 ms 24356 KB Output is correct
46 Correct 20 ms 24300 KB Output is correct
47 Correct 16 ms 23916 KB Output is correct
48 Correct 20 ms 24300 KB Output is correct
49 Correct 20 ms 24428 KB Output is correct
50 Correct 20 ms 24428 KB Output is correct
51 Correct 20 ms 24300 KB Output is correct
52 Correct 20 ms 24300 KB Output is correct
53 Correct 20 ms 24300 KB Output is correct
54 Correct 20 ms 24300 KB Output is correct
55 Correct 16 ms 23916 KB Output is correct
56 Correct 168 ms 37056 KB Output is correct
57 Correct 223 ms 36716 KB Output is correct
58 Correct 20 ms 24300 KB Output is correct
59 Correct 16 ms 23916 KB Output is correct
60 Correct 17 ms 23916 KB Output is correct
61 Correct 193 ms 36716 KB Output is correct
62 Correct 20 ms 24300 KB Output is correct
63 Correct 279 ms 45256 KB Output is correct
64 Correct 222 ms 36716 KB Output is correct
65 Correct 20 ms 24300 KB Output is correct
66 Correct 220 ms 37740 KB Output is correct
67 Correct 20 ms 24300 KB Output is correct
68 Correct 20 ms 24300 KB Output is correct
69 Correct 169 ms 36964 KB Output is correct
70 Correct 20 ms 24556 KB Output is correct
71 Correct 208 ms 36584 KB Output is correct
72 Correct 231 ms 39660 KB Output is correct
73 Correct 17 ms 23916 KB Output is correct
74 Correct 234 ms 45676 KB Output is correct
75 Correct 261 ms 41216 KB Output is correct
76 Correct 20 ms 24300 KB Output is correct
77 Correct 20 ms 24300 KB Output is correct
78 Correct 165 ms 36940 KB Output is correct
79 Correct 163 ms 40416 KB Output is correct
80 Correct 22 ms 24300 KB Output is correct
81 Correct 20 ms 24300 KB Output is correct
82 Correct 17 ms 23916 KB Output is correct
83 Correct 16 ms 23936 KB Output is correct
84 Correct 20 ms 24300 KB Output is correct
85 Correct 242 ms 38412 KB Output is correct
86 Correct 20 ms 24428 KB Output is correct
87 Correct 205 ms 37100 KB Output is correct
88 Correct 16 ms 23916 KB Output is correct
89 Correct 221 ms 37100 KB Output is correct
90 Correct 221 ms 38384 KB Output is correct
91 Correct 203 ms 40640 KB Output is correct
92 Correct 167 ms 36964 KB Output is correct
93 Correct 20 ms 24300 KB Output is correct
94 Correct 17 ms 23916 KB Output is correct
95 Correct 167 ms 40524 KB Output is correct
96 Correct 218 ms 45280 KB Output is correct
97 Correct 20 ms 24300 KB Output is correct
98 Correct 16 ms 23916 KB Output is correct
99 Correct 186 ms 38756 KB Output is correct
100 Correct 20 ms 24300 KB Output is correct
101 Correct 213 ms 37740 KB Output is correct
102 Correct 206 ms 43616 KB Output is correct
103 Correct 21 ms 24428 KB Output is correct
104 Correct 20 ms 24428 KB Output is correct
105 Correct 20 ms 24300 KB Output is correct
106 Correct 20 ms 24300 KB Output is correct
107 Correct 20 ms 24300 KB Output is correct
108 Correct 1065 ms 105728 KB Output is correct
109 Correct 1486 ms 116592 KB Output is correct
110 Correct 1519 ms 130240 KB Output is correct
111 Correct 1480 ms 144248 KB Output is correct
112 Correct 1453 ms 131132 KB Output is correct
113 Correct 1008 ms 115896 KB Output is correct
114 Correct 1190 ms 95064 KB Output is correct
115 Correct 1192 ms 95076 KB Output is correct
116 Correct 1469 ms 101336 KB Output is correct
117 Correct 1214 ms 116704 KB Output is correct
118 Correct 1176 ms 96984 KB Output is correct
119 Correct 1215 ms 116728 KB Output is correct
120 Correct 1383 ms 133592 KB Output is correct
121 Correct 1211 ms 116696 KB Output is correct
122 Correct 1412 ms 107260 KB Output is correct
123 Correct 936 ms 116296 KB Output is correct
124 Correct 1289 ms 113752 KB Output is correct