Submission #681455

# Submission time Handle Problem Language Result Execution time Memory
681455 2023-01-13T06:23:37 Z flappybird Mergers (JOI19_mergers) C++17
100 / 100
1274 ms 151908 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 505050
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define fmax(a, b) (a) = (max((a), (b))
vector<int> adj[MAX];
int sp[MAX][MAXS];
int dep[MAX] = { 0, 1 };
vector<int> locs[MAX];
void dfs(int x, int p = 0) {
	sp[x][0] = p;
	int i;
	for (i = 1; i < MAXS; i++) sp[x][i] = sp[sp[x][i - 1]][i - 1];
	for (auto v : adj[x]) if (v != p) {
		dep[v] = dep[x] + 1;
		dfs(v, x);
	}
}
int lca(int u, int v) {
	int i;
	if (dep[u] != dep[v]) {
		if (dep[u] > dep[v]) swap(u, v);
		int d = dep[v] - dep[u];
		for (i = 0; i < MAXS; i++) if ((d >> i) & 1) v = sp[v][i];
	}
	if (u == v) return u;
	for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
	return sp[u][0];
}
int p[MAX];
int psum[MAX];
int col[MAX];
int find(int x) {
	if (p[x] == x) return x;
	return p[x] = find(p[x]);
}
void uni(int u, int v) {
	u = find(u);
	v = find(v);
	if (u == v) return;
	if (adj[u].size() < adj[v].size()) swap(u, v);
	for (auto x : adj[v]) adj[u].push_back(x);
	p[v] = u;
}
void calc(int x, int p = 0) {
	for (auto v : adj[x]) if (v != p) {
		calc(v, x);
		psum[x] += psum[v];
	}
}
int nleaf(int x, int p = 0) {
	int cnt = 0;
	int sum = 0;
	for (auto v : adj[x]) {
		v = find(v);
		if (v == x) continue;
		cnt++;
		if (v == p) continue;
		sum += nleaf(v, x);
	}
	if (cnt == 1) sum++;
	return sum;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int N, K;
	cin >> N >> K;
	int i;
	int a, b;
	for (i = 1; i < N; i++) {
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1);
	for (i = 1; i <= N; i++) {
		cin >> col[i];
		locs[col[i]].push_back(i);
	}
	for (i = 1; i <= K; i++) {
		if (locs[i].size() > 1) {
			int j;
			for (j = 1; j < locs[i].size(); j++) {
				int l = lca(locs[i][j], locs[i][j - 1]);
				psum[locs[i][j]]++;
				psum[locs[i][j - 1]]++;
				psum[l] -= 2;
			}
		}
	}
	for (i = 1; i <= N; i++) p[i] = i;
	calc(1);
	for (i = 2; i <= N; i++) if (psum[i]) uni(i, sp[i][0]);
	cout << (nleaf(find(1)) + 1) / 2;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:94:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |    for (j = 1; j < locs[i].size(); j++) {
      |                ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24020 KB Output is correct
2 Correct 12 ms 24036 KB Output is correct
3 Correct 12 ms 24068 KB Output is correct
4 Correct 12 ms 24068 KB Output is correct
5 Correct 12 ms 24048 KB Output is correct
6 Correct 13 ms 24076 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 13 ms 24092 KB Output is correct
9 Correct 12 ms 24072 KB Output is correct
10 Correct 12 ms 24080 KB Output is correct
11 Correct 14 ms 24148 KB Output is correct
12 Correct 12 ms 24068 KB Output is correct
13 Correct 14 ms 23996 KB Output is correct
14 Correct 13 ms 23964 KB Output is correct
15 Correct 14 ms 24020 KB Output is correct
16 Correct 12 ms 24068 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 11 ms 24072 KB Output is correct
19 Correct 13 ms 24004 KB Output is correct
20 Correct 12 ms 24020 KB Output is correct
21 Correct 12 ms 23984 KB Output is correct
22 Correct 12 ms 24020 KB Output is correct
23 Correct 11 ms 24020 KB Output is correct
24 Correct 12 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24020 KB Output is correct
2 Correct 12 ms 24036 KB Output is correct
3 Correct 12 ms 24068 KB Output is correct
4 Correct 12 ms 24068 KB Output is correct
5 Correct 12 ms 24048 KB Output is correct
6 Correct 13 ms 24076 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 13 ms 24092 KB Output is correct
9 Correct 12 ms 24072 KB Output is correct
10 Correct 12 ms 24080 KB Output is correct
11 Correct 14 ms 24148 KB Output is correct
12 Correct 12 ms 24068 KB Output is correct
13 Correct 14 ms 23996 KB Output is correct
14 Correct 13 ms 23964 KB Output is correct
15 Correct 14 ms 24020 KB Output is correct
16 Correct 12 ms 24068 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 11 ms 24072 KB Output is correct
19 Correct 13 ms 24004 KB Output is correct
20 Correct 12 ms 24020 KB Output is correct
21 Correct 12 ms 23984 KB Output is correct
22 Correct 12 ms 24020 KB Output is correct
23 Correct 11 ms 24020 KB Output is correct
24 Correct 12 ms 24076 KB Output is correct
25 Correct 12 ms 24020 KB Output is correct
26 Correct 14 ms 24532 KB Output is correct
27 Correct 15 ms 24532 KB Output is correct
28 Correct 14 ms 24660 KB Output is correct
29 Correct 16 ms 24576 KB Output is correct
30 Correct 13 ms 24596 KB Output is correct
31 Correct 12 ms 24048 KB Output is correct
32 Correct 15 ms 24800 KB Output is correct
33 Correct 12 ms 23988 KB Output is correct
34 Correct 13 ms 24464 KB Output is correct
35 Correct 14 ms 24600 KB Output is correct
36 Correct 15 ms 24468 KB Output is correct
37 Correct 14 ms 24604 KB Output is correct
38 Correct 13 ms 24020 KB Output is correct
39 Correct 15 ms 24532 KB Output is correct
40 Correct 14 ms 24592 KB Output is correct
41 Correct 18 ms 24532 KB Output is correct
42 Correct 16 ms 24532 KB Output is correct
43 Correct 15 ms 24720 KB Output is correct
44 Correct 12 ms 24048 KB Output is correct
45 Correct 14 ms 24676 KB Output is correct
46 Correct 14 ms 24596 KB Output is correct
47 Correct 13 ms 24036 KB Output is correct
48 Correct 14 ms 24592 KB Output is correct
49 Correct 13 ms 24532 KB Output is correct
50 Correct 14 ms 24716 KB Output is correct
51 Correct 13 ms 24524 KB Output is correct
52 Correct 13 ms 24588 KB Output is correct
53 Correct 14 ms 24592 KB Output is correct
54 Correct 13 ms 24532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24020 KB Output is correct
2 Correct 12 ms 24036 KB Output is correct
3 Correct 12 ms 24068 KB Output is correct
4 Correct 12 ms 24068 KB Output is correct
5 Correct 12 ms 24048 KB Output is correct
6 Correct 13 ms 24076 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 13 ms 24092 KB Output is correct
9 Correct 12 ms 24072 KB Output is correct
10 Correct 12 ms 24080 KB Output is correct
11 Correct 14 ms 24148 KB Output is correct
12 Correct 12 ms 24068 KB Output is correct
13 Correct 14 ms 23996 KB Output is correct
14 Correct 13 ms 23964 KB Output is correct
15 Correct 14 ms 24020 KB Output is correct
16 Correct 12 ms 24068 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 11 ms 24072 KB Output is correct
19 Correct 13 ms 24004 KB Output is correct
20 Correct 12 ms 24020 KB Output is correct
21 Correct 12 ms 23984 KB Output is correct
22 Correct 12 ms 24020 KB Output is correct
23 Correct 11 ms 24020 KB Output is correct
24 Correct 12 ms 24076 KB Output is correct
25 Correct 12 ms 24020 KB Output is correct
26 Correct 65 ms 39624 KB Output is correct
27 Correct 111 ms 41704 KB Output is correct
28 Correct 15 ms 24532 KB Output is correct
29 Correct 13 ms 24020 KB Output is correct
30 Correct 14 ms 24072 KB Output is correct
31 Correct 105 ms 41084 KB Output is correct
32 Correct 13 ms 24524 KB Output is correct
33 Correct 165 ms 48860 KB Output is correct
34 Correct 146 ms 41408 KB Output is correct
35 Correct 16 ms 24504 KB Output is correct
36 Correct 121 ms 43668 KB Output is correct
37 Correct 14 ms 24564 KB Output is correct
38 Correct 15 ms 24548 KB Output is correct
39 Correct 74 ms 39668 KB Output is correct
40 Correct 15 ms 24780 KB Output is correct
41 Correct 142 ms 42456 KB Output is correct
42 Correct 151 ms 46568 KB Output is correct
43 Correct 13 ms 24100 KB Output is correct
44 Correct 171 ms 49464 KB Output is correct
45 Correct 152 ms 47404 KB Output is correct
46 Correct 15 ms 24548 KB Output is correct
47 Correct 17 ms 24532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 38212 KB Output is correct
2 Correct 67 ms 39760 KB Output is correct
3 Correct 17 ms 24508 KB Output is correct
4 Correct 16 ms 24532 KB Output is correct
5 Correct 14 ms 24120 KB Output is correct
6 Correct 14 ms 24008 KB Output is correct
7 Correct 13 ms 24524 KB Output is correct
8 Correct 127 ms 42064 KB Output is correct
9 Correct 14 ms 24500 KB Output is correct
10 Correct 103 ms 40856 KB Output is correct
11 Correct 12 ms 23992 KB Output is correct
12 Correct 129 ms 41976 KB Output is correct
13 Correct 131 ms 41736 KB Output is correct
14 Correct 97 ms 41592 KB Output is correct
15 Correct 69 ms 39668 KB Output is correct
16 Correct 14 ms 24596 KB Output is correct
17 Correct 15 ms 24020 KB Output is correct
18 Correct 74 ms 41852 KB Output is correct
19 Correct 119 ms 46156 KB Output is correct
20 Correct 18 ms 24584 KB Output is correct
21 Correct 13 ms 24028 KB Output is correct
22 Correct 73 ms 40776 KB Output is correct
23 Correct 15 ms 24524 KB Output is correct
24 Correct 141 ms 41260 KB Output is correct
25 Correct 129 ms 44808 KB Output is correct
26 Correct 16 ms 24492 KB Output is correct
27 Correct 15 ms 24660 KB Output is correct
28 Correct 18 ms 24532 KB Output is correct
29 Correct 13 ms 24596 KB Output is correct
30 Correct 14 ms 24528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24020 KB Output is correct
2 Correct 12 ms 24036 KB Output is correct
3 Correct 12 ms 24068 KB Output is correct
4 Correct 12 ms 24068 KB Output is correct
5 Correct 12 ms 24048 KB Output is correct
6 Correct 13 ms 24076 KB Output is correct
7 Correct 13 ms 24020 KB Output is correct
8 Correct 13 ms 24092 KB Output is correct
9 Correct 12 ms 24072 KB Output is correct
10 Correct 12 ms 24080 KB Output is correct
11 Correct 14 ms 24148 KB Output is correct
12 Correct 12 ms 24068 KB Output is correct
13 Correct 14 ms 23996 KB Output is correct
14 Correct 13 ms 23964 KB Output is correct
15 Correct 14 ms 24020 KB Output is correct
16 Correct 12 ms 24068 KB Output is correct
17 Correct 14 ms 24028 KB Output is correct
18 Correct 11 ms 24072 KB Output is correct
19 Correct 13 ms 24004 KB Output is correct
20 Correct 12 ms 24020 KB Output is correct
21 Correct 12 ms 23984 KB Output is correct
22 Correct 12 ms 24020 KB Output is correct
23 Correct 11 ms 24020 KB Output is correct
24 Correct 12 ms 24076 KB Output is correct
25 Correct 12 ms 24020 KB Output is correct
26 Correct 14 ms 24532 KB Output is correct
27 Correct 15 ms 24532 KB Output is correct
28 Correct 14 ms 24660 KB Output is correct
29 Correct 16 ms 24576 KB Output is correct
30 Correct 13 ms 24596 KB Output is correct
31 Correct 12 ms 24048 KB Output is correct
32 Correct 15 ms 24800 KB Output is correct
33 Correct 12 ms 23988 KB Output is correct
34 Correct 13 ms 24464 KB Output is correct
35 Correct 14 ms 24600 KB Output is correct
36 Correct 15 ms 24468 KB Output is correct
37 Correct 14 ms 24604 KB Output is correct
38 Correct 13 ms 24020 KB Output is correct
39 Correct 15 ms 24532 KB Output is correct
40 Correct 14 ms 24592 KB Output is correct
41 Correct 18 ms 24532 KB Output is correct
42 Correct 16 ms 24532 KB Output is correct
43 Correct 15 ms 24720 KB Output is correct
44 Correct 12 ms 24048 KB Output is correct
45 Correct 14 ms 24676 KB Output is correct
46 Correct 14 ms 24596 KB Output is correct
47 Correct 13 ms 24036 KB Output is correct
48 Correct 14 ms 24592 KB Output is correct
49 Correct 13 ms 24532 KB Output is correct
50 Correct 14 ms 24716 KB Output is correct
51 Correct 13 ms 24524 KB Output is correct
52 Correct 13 ms 24588 KB Output is correct
53 Correct 14 ms 24592 KB Output is correct
54 Correct 13 ms 24532 KB Output is correct
55 Correct 12 ms 24020 KB Output is correct
56 Correct 65 ms 39624 KB Output is correct
57 Correct 111 ms 41704 KB Output is correct
58 Correct 15 ms 24532 KB Output is correct
59 Correct 13 ms 24020 KB Output is correct
60 Correct 14 ms 24072 KB Output is correct
61 Correct 105 ms 41084 KB Output is correct
62 Correct 13 ms 24524 KB Output is correct
63 Correct 165 ms 48860 KB Output is correct
64 Correct 146 ms 41408 KB Output is correct
65 Correct 16 ms 24504 KB Output is correct
66 Correct 121 ms 43668 KB Output is correct
67 Correct 14 ms 24564 KB Output is correct
68 Correct 15 ms 24548 KB Output is correct
69 Correct 74 ms 39668 KB Output is correct
70 Correct 15 ms 24780 KB Output is correct
71 Correct 142 ms 42456 KB Output is correct
72 Correct 151 ms 46568 KB Output is correct
73 Correct 13 ms 24100 KB Output is correct
74 Correct 171 ms 49464 KB Output is correct
75 Correct 152 ms 47404 KB Output is correct
76 Correct 15 ms 24548 KB Output is correct
77 Correct 17 ms 24532 KB Output is correct
78 Correct 66 ms 38212 KB Output is correct
79 Correct 67 ms 39760 KB Output is correct
80 Correct 17 ms 24508 KB Output is correct
81 Correct 16 ms 24532 KB Output is correct
82 Correct 14 ms 24120 KB Output is correct
83 Correct 14 ms 24008 KB Output is correct
84 Correct 13 ms 24524 KB Output is correct
85 Correct 127 ms 42064 KB Output is correct
86 Correct 14 ms 24500 KB Output is correct
87 Correct 103 ms 40856 KB Output is correct
88 Correct 12 ms 23992 KB Output is correct
89 Correct 129 ms 41976 KB Output is correct
90 Correct 131 ms 41736 KB Output is correct
91 Correct 97 ms 41592 KB Output is correct
92 Correct 69 ms 39668 KB Output is correct
93 Correct 14 ms 24596 KB Output is correct
94 Correct 15 ms 24020 KB Output is correct
95 Correct 74 ms 41852 KB Output is correct
96 Correct 119 ms 46156 KB Output is correct
97 Correct 18 ms 24584 KB Output is correct
98 Correct 13 ms 24028 KB Output is correct
99 Correct 73 ms 40776 KB Output is correct
100 Correct 15 ms 24524 KB Output is correct
101 Correct 141 ms 41260 KB Output is correct
102 Correct 129 ms 44808 KB Output is correct
103 Correct 16 ms 24492 KB Output is correct
104 Correct 15 ms 24660 KB Output is correct
105 Correct 18 ms 24532 KB Output is correct
106 Correct 13 ms 24596 KB Output is correct
107 Correct 14 ms 24528 KB Output is correct
108 Correct 487 ms 107048 KB Output is correct
109 Correct 1177 ms 142492 KB Output is correct
110 Correct 1274 ms 151908 KB Output is correct
111 Correct 1056 ms 139936 KB Output is correct
112 Correct 969 ms 146936 KB Output is correct
113 Correct 495 ms 115944 KB Output is correct
114 Correct 732 ms 111100 KB Output is correct
115 Correct 752 ms 111644 KB Output is correct
116 Correct 938 ms 116420 KB Output is correct
117 Correct 719 ms 112860 KB Output is correct
118 Correct 769 ms 110456 KB Output is correct
119 Correct 692 ms 112796 KB Output is correct
120 Correct 823 ms 129720 KB Output is correct
121 Correct 857 ms 112952 KB Output is correct
122 Correct 951 ms 120024 KB Output is correct
123 Correct 476 ms 112364 KB Output is correct
124 Correct 1008 ms 118036 KB Output is correct