Submission #920878

# Submission time Handle Problem Language Result Execution time Memory
920878 2024-02-03T07:12:43 Z yellowtoad Mergers (JOI19_mergers) C++17
100 / 100
1611 ms 180580 KB
#include <iostream>
#include <vector>
using namespace std;

int n, k, p[500010][20], d[500010], vis[500010], cnt, ans;
vector<int> edge[500010], a[500010], adj[500010], tmp, ed[500010];

void build(int u, int v, int depth) {
	adj[u].push_back(v);
	p[u][0] = v;
	d[u] = depth;
	for (int i = 1; i < 20; i++) p[u][i] = p[p[u][i-1]][i-1];
	for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != v) build(edge[u][i],u,depth+1);
}

int lca(int u, int v) {
	if (d[u] < d[v]) swap(u,v);
	int i = 19;
	while (i >= 0) {
		if (d[p[u][i]] >= d[v]) u = p[u][i];
		i--;
	}
	if (u == v) return u;
	i = 19;
	while (i >= 0) {
		if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
		i--;
	}
	return p[u][0];
}

void dfs(int u) {
	vis[u] = cnt;
	for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != p[u][0]) tmp.push_back(edge[u][i]);
	for (int i = 0; i < adj[u].size(); i++) if (!vis[adj[u][i]]) dfs(adj[u][i]);
}

int main() {
	cin >> n >> k;
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	for (int i = 1; i <= n; i++) {
		int u;
		cin >> u;
		a[u].push_back(i);
	}
	build(1,0,1);
	for (int i = 1; i <= k; i++) {
		int lcaa = a[i][0];
		for (int j = 1; j < a[i].size(); j++) lcaa = lca(lcaa,a[i][j]);
		for (int j = 0; j < a[i].size(); j++) adj[lcaa].push_back(a[i][j]);
	}
	tmp.push_back(1);
	while (tmp.size()) {
		int u = tmp.back();
		tmp.pop_back();
		if (vis[u]) continue;
		cnt++;
		dfs(u);
	}
	for (int i = 1; i <= n; i++) {
		if (vis[i] != vis[p[i][0]]) {
			ed[vis[p[i][0]]].push_back(vis[i]);
			ed[vis[i]].push_back(vis[p[i][0]]);
		}
	}
	for (int i = 1; i <= cnt; i++) if (ed[i].size() <= 1) ans++;
	if (cnt == 1) cout << 0 << endl;
	else cout << (ans+1)/2 << endl;
}

Compilation message

mergers.cpp: In function 'void build(int, int, int)':
mergers.cpp:13:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |  for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != v) build(edge[u][i],u,depth+1);
      |                  ~~^~~~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs(int)':
mergers.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  for (int i = 0; i < edge[u].size(); i++) if (edge[u][i] != p[u][0]) tmp.push_back(edge[u][i]);
      |                  ~~^~~~~~~~~~~~~~~~
mergers.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (int i = 0; i < adj[u].size(); i++) if (!vis[adj[u][i]]) dfs(adj[u][i]);
      |                  ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:54:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for (int j = 1; j < a[i].size(); j++) lcaa = lca(lcaa,a[i][j]);
      |                   ~~^~~~~~~~~~~~~
mergers.cpp:55:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |   for (int j = 0; j < a[i].size(); j++) adj[lcaa].push_back(a[i][j]);
      |                   ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 52056 KB Output is correct
2 Correct 13 ms 51804 KB Output is correct
3 Correct 13 ms 51804 KB Output is correct
4 Correct 14 ms 51804 KB Output is correct
5 Correct 13 ms 51804 KB Output is correct
6 Correct 13 ms 51804 KB Output is correct
7 Correct 13 ms 51800 KB Output is correct
8 Correct 14 ms 51804 KB Output is correct
9 Correct 14 ms 51804 KB Output is correct
10 Correct 14 ms 51696 KB Output is correct
11 Correct 14 ms 51804 KB Output is correct
12 Correct 14 ms 51804 KB Output is correct
13 Correct 15 ms 51700 KB Output is correct
14 Correct 14 ms 51804 KB Output is correct
15 Correct 14 ms 51800 KB Output is correct
16 Correct 13 ms 51804 KB Output is correct
17 Correct 13 ms 51804 KB Output is correct
18 Correct 14 ms 51896 KB Output is correct
19 Correct 13 ms 51804 KB Output is correct
20 Correct 13 ms 51804 KB Output is correct
21 Correct 14 ms 51916 KB Output is correct
22 Correct 12 ms 51804 KB Output is correct
23 Correct 13 ms 51804 KB Output is correct
24 Correct 14 ms 51804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 52056 KB Output is correct
2 Correct 13 ms 51804 KB Output is correct
3 Correct 13 ms 51804 KB Output is correct
4 Correct 14 ms 51804 KB Output is correct
5 Correct 13 ms 51804 KB Output is correct
6 Correct 13 ms 51804 KB Output is correct
7 Correct 13 ms 51800 KB Output is correct
8 Correct 14 ms 51804 KB Output is correct
9 Correct 14 ms 51804 KB Output is correct
10 Correct 14 ms 51696 KB Output is correct
11 Correct 14 ms 51804 KB Output is correct
12 Correct 14 ms 51804 KB Output is correct
13 Correct 15 ms 51700 KB Output is correct
14 Correct 14 ms 51804 KB Output is correct
15 Correct 14 ms 51800 KB Output is correct
16 Correct 13 ms 51804 KB Output is correct
17 Correct 13 ms 51804 KB Output is correct
18 Correct 14 ms 51896 KB Output is correct
19 Correct 13 ms 51804 KB Output is correct
20 Correct 13 ms 51804 KB Output is correct
21 Correct 14 ms 51916 KB Output is correct
22 Correct 12 ms 51804 KB Output is correct
23 Correct 13 ms 51804 KB Output is correct
24 Correct 14 ms 51804 KB Output is correct
25 Correct 13 ms 51800 KB Output is correct
26 Correct 19 ms 52056 KB Output is correct
27 Correct 15 ms 52060 KB Output is correct
28 Correct 17 ms 52316 KB Output is correct
29 Correct 17 ms 52180 KB Output is correct
30 Correct 16 ms 52060 KB Output is correct
31 Correct 15 ms 52060 KB Output is correct
32 Correct 16 ms 52312 KB Output is correct
33 Correct 12 ms 51804 KB Output is correct
34 Correct 15 ms 52056 KB Output is correct
35 Correct 16 ms 52316 KB Output is correct
36 Correct 16 ms 52060 KB Output is correct
37 Correct 16 ms 52060 KB Output is correct
38 Correct 13 ms 52056 KB Output is correct
39 Correct 17 ms 52060 KB Output is correct
40 Correct 16 ms 52056 KB Output is correct
41 Correct 16 ms 52060 KB Output is correct
42 Correct 16 ms 52060 KB Output is correct
43 Correct 16 ms 52316 KB Output is correct
44 Correct 13 ms 51804 KB Output is correct
45 Correct 16 ms 52060 KB Output is correct
46 Correct 17 ms 52188 KB Output is correct
47 Correct 13 ms 51804 KB Output is correct
48 Correct 16 ms 52060 KB Output is correct
49 Correct 18 ms 52196 KB Output is correct
50 Correct 16 ms 52316 KB Output is correct
51 Correct 17 ms 52060 KB Output is correct
52 Correct 19 ms 52188 KB Output is correct
53 Correct 18 ms 52360 KB Output is correct
54 Correct 15 ms 52056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 52056 KB Output is correct
2 Correct 13 ms 51804 KB Output is correct
3 Correct 13 ms 51804 KB Output is correct
4 Correct 14 ms 51804 KB Output is correct
5 Correct 13 ms 51804 KB Output is correct
6 Correct 13 ms 51804 KB Output is correct
7 Correct 13 ms 51800 KB Output is correct
8 Correct 14 ms 51804 KB Output is correct
9 Correct 14 ms 51804 KB Output is correct
10 Correct 14 ms 51696 KB Output is correct
11 Correct 14 ms 51804 KB Output is correct
12 Correct 14 ms 51804 KB Output is correct
13 Correct 15 ms 51700 KB Output is correct
14 Correct 14 ms 51804 KB Output is correct
15 Correct 14 ms 51800 KB Output is correct
16 Correct 13 ms 51804 KB Output is correct
17 Correct 13 ms 51804 KB Output is correct
18 Correct 14 ms 51896 KB Output is correct
19 Correct 13 ms 51804 KB Output is correct
20 Correct 13 ms 51804 KB Output is correct
21 Correct 14 ms 51916 KB Output is correct
22 Correct 12 ms 51804 KB Output is correct
23 Correct 13 ms 51804 KB Output is correct
24 Correct 14 ms 51804 KB Output is correct
25 Correct 13 ms 51804 KB Output is correct
26 Correct 131 ms 69712 KB Output is correct
27 Correct 165 ms 69348 KB Output is correct
28 Correct 16 ms 52056 KB Output is correct
29 Correct 14 ms 52056 KB Output is correct
30 Correct 13 ms 51804 KB Output is correct
31 Correct 154 ms 68808 KB Output is correct
32 Correct 18 ms 52056 KB Output is correct
33 Correct 206 ms 75180 KB Output is correct
34 Correct 177 ms 69540 KB Output is correct
35 Correct 16 ms 52060 KB Output is correct
36 Correct 152 ms 69456 KB Output is correct
37 Correct 16 ms 52312 KB Output is correct
38 Correct 16 ms 52060 KB Output is correct
39 Correct 146 ms 69648 KB Output is correct
40 Correct 17 ms 52312 KB Output is correct
41 Correct 139 ms 69204 KB Output is correct
42 Correct 158 ms 71104 KB Output is correct
43 Correct 12 ms 51804 KB Output is correct
44 Correct 163 ms 75232 KB Output is correct
45 Correct 159 ms 72444 KB Output is correct
46 Correct 16 ms 52056 KB Output is correct
47 Correct 18 ms 52060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 69648 KB Output is correct
2 Correct 166 ms 75412 KB Output is correct
3 Correct 16 ms 52056 KB Output is correct
4 Correct 16 ms 52056 KB Output is correct
5 Correct 13 ms 51800 KB Output is correct
6 Correct 13 ms 51800 KB Output is correct
7 Correct 15 ms 52056 KB Output is correct
8 Correct 216 ms 70536 KB Output is correct
9 Correct 16 ms 52060 KB Output is correct
10 Correct 145 ms 68944 KB Output is correct
11 Correct 13 ms 51896 KB Output is correct
12 Correct 158 ms 68948 KB Output is correct
13 Correct 172 ms 70468 KB Output is correct
14 Correct 201 ms 74572 KB Output is correct
15 Correct 130 ms 69708 KB Output is correct
16 Correct 16 ms 52060 KB Output is correct
17 Correct 14 ms 51804 KB Output is correct
18 Correct 159 ms 74704 KB Output is correct
19 Correct 201 ms 77520 KB Output is correct
20 Correct 17 ms 52056 KB Output is correct
21 Correct 13 ms 51912 KB Output is correct
22 Correct 176 ms 71116 KB Output is correct
23 Correct 17 ms 52056 KB Output is correct
24 Correct 179 ms 69580 KB Output is correct
25 Correct 193 ms 76372 KB Output is correct
26 Correct 16 ms 52316 KB Output is correct
27 Correct 17 ms 52316 KB Output is correct
28 Correct 16 ms 52168 KB Output is correct
29 Correct 16 ms 52048 KB Output is correct
30 Correct 16 ms 52060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 52056 KB Output is correct
2 Correct 13 ms 51804 KB Output is correct
3 Correct 13 ms 51804 KB Output is correct
4 Correct 14 ms 51804 KB Output is correct
5 Correct 13 ms 51804 KB Output is correct
6 Correct 13 ms 51804 KB Output is correct
7 Correct 13 ms 51800 KB Output is correct
8 Correct 14 ms 51804 KB Output is correct
9 Correct 14 ms 51804 KB Output is correct
10 Correct 14 ms 51696 KB Output is correct
11 Correct 14 ms 51804 KB Output is correct
12 Correct 14 ms 51804 KB Output is correct
13 Correct 15 ms 51700 KB Output is correct
14 Correct 14 ms 51804 KB Output is correct
15 Correct 14 ms 51800 KB Output is correct
16 Correct 13 ms 51804 KB Output is correct
17 Correct 13 ms 51804 KB Output is correct
18 Correct 14 ms 51896 KB Output is correct
19 Correct 13 ms 51804 KB Output is correct
20 Correct 13 ms 51804 KB Output is correct
21 Correct 14 ms 51916 KB Output is correct
22 Correct 12 ms 51804 KB Output is correct
23 Correct 13 ms 51804 KB Output is correct
24 Correct 14 ms 51804 KB Output is correct
25 Correct 13 ms 51800 KB Output is correct
26 Correct 19 ms 52056 KB Output is correct
27 Correct 15 ms 52060 KB Output is correct
28 Correct 17 ms 52316 KB Output is correct
29 Correct 17 ms 52180 KB Output is correct
30 Correct 16 ms 52060 KB Output is correct
31 Correct 15 ms 52060 KB Output is correct
32 Correct 16 ms 52312 KB Output is correct
33 Correct 12 ms 51804 KB Output is correct
34 Correct 15 ms 52056 KB Output is correct
35 Correct 16 ms 52316 KB Output is correct
36 Correct 16 ms 52060 KB Output is correct
37 Correct 16 ms 52060 KB Output is correct
38 Correct 13 ms 52056 KB Output is correct
39 Correct 17 ms 52060 KB Output is correct
40 Correct 16 ms 52056 KB Output is correct
41 Correct 16 ms 52060 KB Output is correct
42 Correct 16 ms 52060 KB Output is correct
43 Correct 16 ms 52316 KB Output is correct
44 Correct 13 ms 51804 KB Output is correct
45 Correct 16 ms 52060 KB Output is correct
46 Correct 17 ms 52188 KB Output is correct
47 Correct 13 ms 51804 KB Output is correct
48 Correct 16 ms 52060 KB Output is correct
49 Correct 18 ms 52196 KB Output is correct
50 Correct 16 ms 52316 KB Output is correct
51 Correct 17 ms 52060 KB Output is correct
52 Correct 19 ms 52188 KB Output is correct
53 Correct 18 ms 52360 KB Output is correct
54 Correct 15 ms 52056 KB Output is correct
55 Correct 13 ms 51804 KB Output is correct
56 Correct 131 ms 69712 KB Output is correct
57 Correct 165 ms 69348 KB Output is correct
58 Correct 16 ms 52056 KB Output is correct
59 Correct 14 ms 52056 KB Output is correct
60 Correct 13 ms 51804 KB Output is correct
61 Correct 154 ms 68808 KB Output is correct
62 Correct 18 ms 52056 KB Output is correct
63 Correct 206 ms 75180 KB Output is correct
64 Correct 177 ms 69540 KB Output is correct
65 Correct 16 ms 52060 KB Output is correct
66 Correct 152 ms 69456 KB Output is correct
67 Correct 16 ms 52312 KB Output is correct
68 Correct 16 ms 52060 KB Output is correct
69 Correct 146 ms 69648 KB Output is correct
70 Correct 17 ms 52312 KB Output is correct
71 Correct 139 ms 69204 KB Output is correct
72 Correct 158 ms 71104 KB Output is correct
73 Correct 12 ms 51804 KB Output is correct
74 Correct 163 ms 75232 KB Output is correct
75 Correct 159 ms 72444 KB Output is correct
76 Correct 16 ms 52056 KB Output is correct
77 Correct 18 ms 52060 KB Output is correct
78 Correct 145 ms 69648 KB Output is correct
79 Correct 166 ms 75412 KB Output is correct
80 Correct 16 ms 52056 KB Output is correct
81 Correct 16 ms 52056 KB Output is correct
82 Correct 13 ms 51800 KB Output is correct
83 Correct 13 ms 51800 KB Output is correct
84 Correct 15 ms 52056 KB Output is correct
85 Correct 216 ms 70536 KB Output is correct
86 Correct 16 ms 52060 KB Output is correct
87 Correct 145 ms 68944 KB Output is correct
88 Correct 13 ms 51896 KB Output is correct
89 Correct 158 ms 68948 KB Output is correct
90 Correct 172 ms 70468 KB Output is correct
91 Correct 201 ms 74572 KB Output is correct
92 Correct 130 ms 69708 KB Output is correct
93 Correct 16 ms 52060 KB Output is correct
94 Correct 14 ms 51804 KB Output is correct
95 Correct 159 ms 74704 KB Output is correct
96 Correct 201 ms 77520 KB Output is correct
97 Correct 17 ms 52056 KB Output is correct
98 Correct 13 ms 51912 KB Output is correct
99 Correct 176 ms 71116 KB Output is correct
100 Correct 17 ms 52056 KB Output is correct
101 Correct 179 ms 69580 KB Output is correct
102 Correct 193 ms 76372 KB Output is correct
103 Correct 16 ms 52316 KB Output is correct
104 Correct 17 ms 52316 KB Output is correct
105 Correct 16 ms 52168 KB Output is correct
106 Correct 16 ms 52048 KB Output is correct
107 Correct 16 ms 52060 KB Output is correct
108 Correct 985 ms 145284 KB Output is correct
109 Correct 1283 ms 150208 KB Output is correct
110 Correct 1308 ms 159944 KB Output is correct
111 Correct 1575 ms 180580 KB Output is correct
112 Correct 1518 ms 169340 KB Output is correct
113 Correct 1119 ms 164540 KB Output is correct
114 Correct 1018 ms 136096 KB Output is correct
115 Correct 1035 ms 136684 KB Output is correct
116 Correct 1399 ms 139332 KB Output is correct
117 Correct 1447 ms 164028 KB Output is correct
118 Correct 1077 ms 135124 KB Output is correct
119 Correct 1421 ms 164456 KB Output is correct
120 Correct 1611 ms 174896 KB Output is correct
121 Correct 1463 ms 164176 KB Output is correct
122 Correct 1302 ms 148104 KB Output is correct
123 Correct 1106 ms 169000 KB Output is correct
124 Correct 1481 ms 155744 KB Output is correct