Submission #630527

# Submission time Handle Problem Language Result Execution time Memory
630527 2022-08-16T13:02:25 Z Arnch Mergers (JOI19_mergers) C++17
100 / 100
1782 ms 210936 KB
// oooo
/*
 har chi delet mikhad bebar ~
 gitar o ba khodet nabar! ~
 ;Amoo_Hasan;
*/

#include<bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")

using namespace std;

typedef long long ll;
typedef long double ld;

#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
#define mak make_pair

//constexpr int PRI = 1000696969;
constexpr int N = 2e6 + 2, MAXN = 1e6 + 2, LOG = 22;

int n, k;
int s[MAXN], sub[MAXN], head[MAXN], link[MAXN];
int par[MAXN][LOG], h[MAXN], st[MAXN], fn[MAXN], tim;
int sz[MAXN], pv[MAXN];
int ans, total;
int seg[N];
vector<int> vc[MAXN], adj[MAXN], nei[MAXN];

void dfs(int x, int p = -1) {
	par[x][0] = p;
	for(int i = 1; i < LOG; i++) par[x][i] = par[par[x][i - 1]][i - 1];
	if(p != -1) 
		h[x] = h[p] + 1;
	sub[x] = 1;
	for(auto j : adj[x]) {
		if(j == p) continue;
		dfs(j, x);
		sub[x] += sub[j];
	}
}
void hld(int x, int p = -1, int hi = 0) {
	link[tim] = x;
	st[x] = tim++;
	head[x] = hi;

	int bz = -1;
	for(auto j : adj[x]) {
		if(j == p) continue;
		if(bz == -1 || sub[bz] < sub[j]) bz = j;
	}
	if(bz == -1) {
		fn[x] = tim;
		return;
	}

	hld(bz, x, hi);
	for(auto j : adj[x]) {
		if(j == p || j == bz) continue;
		hld(j, x, j);
	}
	fn[x] = tim;
}

inline int get_par(int x, int y) {
	for(int i = 0; i < LOG; i++)
		if((y >> i) & 1)
			x = par[x][i];
	return x;
}
inline int lca(int x, int y) {
	if(h[x] > h[y]) swap(x, y);
	y = get_par(y, h[y] - h[x]);
	if(x == y) return x;
	for(int i = LOG - 1; i >= 0; i--)
		if(par[x][i] != par[y][i])
			x = par[x][i], y = par[y][i];
	return par[x][0];
}

int find(int x) {
	if(pv[x] == x) return x;
	return pv[x] = find(pv[x]);
}
inline void merge(int x, int y) {
//	cout<<"^^" <<x <<' ' <<y <<endl;
	if(max(x, y) >= MAXN) assert(0);
	int X = find(x), Y = find(y);
	if(X == Y) return;
	if(sz[X] < sz[Y]) swap(X, Y);
	pv[Y] = X, sz[X] += sz[Y];
}

void build(int l = 0, int r = n, int v = 1) {
	seg[v] = -1;
	if(r - l < 2) {
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, 2 * v), build(mid, r, 2 * v + 1);
	total = max(total, v);
}
void upd(int s, int e, int val, int l = 0, int r = n, int v = 1) {
	if(r <= s || l >= e) return;
	if(l >= s && r <= e) {
		if(seg[v] == -1) seg[v] = val;
		else merge(seg[v], val);
		return;
	}
	int mid = (l + r) >> 1;
	upd(s, e, val, l, mid, 2 * v), upd(s, e, val, mid, r, 2 * v + 1);
}
void relax(int l = 0, int r = n, int v = 1) {
	if(r - l < 2) {
		if(seg[v] != -1) merge(seg[v], s[link[l]]);
		return;
	}
	int mid = (l + r) >> 1;
	if(seg[v] != -1) {
		if(seg[2 * v] == -1) seg[2 * v] = seg[v];
		else merge(seg[v], seg[2 * v]);

		if(seg[2 * v + 1] == -1) seg[2 * v + 1] = seg[v];
		else merge(seg[v], seg[2 * v + 1]);
	}
	relax(l, mid, 2 * v), relax(mid, r, 2 * v + 1);
}

bool cmp(int i, int j) {
	return st[i] < st[j];
}
inline void solve(int x) {
	vector<int> ver;
	for(auto i : vc[x]) ver.push_back(i);
//	vc[x].clear();
	sort(All(ver), cmp);
	int sz = Sz(ver);
	for(int i = 1; i < sz; i++) {
		ver.push_back(lca(ver[i - 1], ver[i]));
	}
	sort(All(ver), cmp);
	ver.erase(unique(All(ver)), ver.end());

	vector<int> mt;
	mt.push_back(ver[0]);

	upd(st[ver[0]], st[ver[0]] + 1, x);
	for(int i = 1; i < Sz(ver); i++) {
		int v = ver[i];
		while(fn[mt.back()] < fn[v]) mt.pop_back();
		int p = mt.back();
	
		upd(st[v], st[v] + 1, x);
	
		int u = v;
		while(u != -1 && h[u] >= h[p]) {
			if(h[head[u]] < h[p]) break;
			upd(st[head[u]], st[u] + 1, x);
			u = par[head[u]][0];
		}
		if(h[u] >= h[p]) {
			upd(st[p], st[u] + 1, x);
		}

		mt.push_back(v);
	}
	ver.clear();
	mt.clear();
}

int main() {
	ios :: sync_with_stdio(0), cin.tie(0); cout.tie(0);

	for(int i = 0; i < MAXN; i++) sz[i] = 1, pv[i] = i;

	cin >>n >>k;
	for(int i = 0; i < n - 1; i++) {
		int u, v; cin >>u >>v;
		--u, --v;
		adj[u].push_back(v), adj[v].push_back(u);
	}
	dfs(0);
	hld(0);
		
	for(int i = 0; i < n; i++) {
		cin >>s[i];
	}
	
	build();
	
	for(int i = 0; i < n; i++) {
		--s[i];
		vc[s[i]].push_back(i);
	}

	for(int i = 0; i < k; i++) {
		solve(i);
	}

	relax();

	for(int i = 0; i < MAXN; i++) pv[i] = find(pv[i]);

	for(int i = 0; i < n; i++) {
		int u = s[i];
		for(auto j : adj[i]) {
			int v = s[j];
			if(pv[u] == pv[v]) continue;
			nei[pv[u]].push_back(pv[v]);
		}
		adj[i].clear();
	}

	for(int i = 0; i < MAXN; i++) {
		nei[i].erase(unique(All(nei[i])), nei[i].end());
		if(Sz(nei[i]) == 1) ans++;
	}
	cout<<(ans + 1) / 2;

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 39 ms 78540 KB Output is correct
2 Correct 39 ms 78612 KB Output is correct
3 Correct 39 ms 78628 KB Output is correct
4 Correct 41 ms 78596 KB Output is correct
5 Correct 40 ms 78548 KB Output is correct
6 Correct 39 ms 78676 KB Output is correct
7 Correct 51 ms 78540 KB Output is correct
8 Correct 40 ms 78596 KB Output is correct
9 Correct 40 ms 78548 KB Output is correct
10 Correct 40 ms 78656 KB Output is correct
11 Correct 47 ms 78600 KB Output is correct
12 Correct 41 ms 78668 KB Output is correct
13 Correct 44 ms 78536 KB Output is correct
14 Correct 44 ms 78632 KB Output is correct
15 Correct 50 ms 78632 KB Output is correct
16 Correct 45 ms 78664 KB Output is correct
17 Correct 44 ms 78564 KB Output is correct
18 Correct 49 ms 78756 KB Output is correct
19 Correct 48 ms 78548 KB Output is correct
20 Correct 49 ms 78592 KB Output is correct
21 Correct 41 ms 78556 KB Output is correct
22 Correct 46 ms 78596 KB Output is correct
23 Correct 48 ms 78668 KB Output is correct
24 Correct 42 ms 78668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 78540 KB Output is correct
2 Correct 39 ms 78612 KB Output is correct
3 Correct 39 ms 78628 KB Output is correct
4 Correct 41 ms 78596 KB Output is correct
5 Correct 40 ms 78548 KB Output is correct
6 Correct 39 ms 78676 KB Output is correct
7 Correct 51 ms 78540 KB Output is correct
8 Correct 40 ms 78596 KB Output is correct
9 Correct 40 ms 78548 KB Output is correct
10 Correct 40 ms 78656 KB Output is correct
11 Correct 47 ms 78600 KB Output is correct
12 Correct 41 ms 78668 KB Output is correct
13 Correct 44 ms 78536 KB Output is correct
14 Correct 44 ms 78632 KB Output is correct
15 Correct 50 ms 78632 KB Output is correct
16 Correct 45 ms 78664 KB Output is correct
17 Correct 44 ms 78564 KB Output is correct
18 Correct 49 ms 78756 KB Output is correct
19 Correct 48 ms 78548 KB Output is correct
20 Correct 49 ms 78592 KB Output is correct
21 Correct 41 ms 78556 KB Output is correct
22 Correct 46 ms 78596 KB Output is correct
23 Correct 48 ms 78668 KB Output is correct
24 Correct 42 ms 78668 KB Output is correct
25 Correct 41 ms 78548 KB Output is correct
26 Correct 61 ms 79140 KB Output is correct
27 Correct 44 ms 79068 KB Output is correct
28 Correct 53 ms 79376 KB Output is correct
29 Correct 43 ms 79308 KB Output is correct
30 Correct 54 ms 79116 KB Output is correct
31 Correct 41 ms 78548 KB Output is correct
32 Correct 44 ms 79312 KB Output is correct
33 Correct 41 ms 78540 KB Output is correct
34 Correct 49 ms 79104 KB Output is correct
35 Correct 45 ms 79308 KB Output is correct
36 Correct 45 ms 79140 KB Output is correct
37 Correct 47 ms 79052 KB Output is correct
38 Correct 48 ms 78668 KB Output is correct
39 Correct 45 ms 79080 KB Output is correct
40 Correct 49 ms 79132 KB Output is correct
41 Correct 51 ms 79052 KB Output is correct
42 Correct 55 ms 79180 KB Output is correct
43 Correct 45 ms 79344 KB Output is correct
44 Correct 45 ms 78656 KB Output is correct
45 Correct 45 ms 79272 KB Output is correct
46 Correct 46 ms 79188 KB Output is correct
47 Correct 45 ms 78664 KB Output is correct
48 Correct 46 ms 79148 KB Output is correct
49 Correct 46 ms 79332 KB Output is correct
50 Correct 44 ms 79252 KB Output is correct
51 Correct 45 ms 79032 KB Output is correct
52 Correct 44 ms 79060 KB Output is correct
53 Correct 44 ms 79316 KB Output is correct
54 Correct 51 ms 79148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 78540 KB Output is correct
2 Correct 39 ms 78612 KB Output is correct
3 Correct 39 ms 78628 KB Output is correct
4 Correct 41 ms 78596 KB Output is correct
5 Correct 40 ms 78548 KB Output is correct
6 Correct 39 ms 78676 KB Output is correct
7 Correct 51 ms 78540 KB Output is correct
8 Correct 40 ms 78596 KB Output is correct
9 Correct 40 ms 78548 KB Output is correct
10 Correct 40 ms 78656 KB Output is correct
11 Correct 47 ms 78600 KB Output is correct
12 Correct 41 ms 78668 KB Output is correct
13 Correct 44 ms 78536 KB Output is correct
14 Correct 44 ms 78632 KB Output is correct
15 Correct 50 ms 78632 KB Output is correct
16 Correct 45 ms 78664 KB Output is correct
17 Correct 44 ms 78564 KB Output is correct
18 Correct 49 ms 78756 KB Output is correct
19 Correct 48 ms 78548 KB Output is correct
20 Correct 49 ms 78592 KB Output is correct
21 Correct 41 ms 78556 KB Output is correct
22 Correct 46 ms 78596 KB Output is correct
23 Correct 48 ms 78668 KB Output is correct
24 Correct 42 ms 78668 KB Output is correct
25 Correct 42 ms 78548 KB Output is correct
26 Correct 188 ms 95148 KB Output is correct
27 Correct 234 ms 94896 KB Output is correct
28 Correct 44 ms 79148 KB Output is correct
29 Correct 42 ms 78652 KB Output is correct
30 Correct 43 ms 78652 KB Output is correct
31 Correct 226 ms 94776 KB Output is correct
32 Correct 43 ms 79136 KB Output is correct
33 Correct 230 ms 102044 KB Output is correct
34 Correct 249 ms 94868 KB Output is correct
35 Correct 46 ms 79140 KB Output is correct
36 Correct 185 ms 95500 KB Output is correct
37 Correct 45 ms 79180 KB Output is correct
38 Correct 45 ms 79088 KB Output is correct
39 Correct 171 ms 95204 KB Output is correct
40 Correct 44 ms 79340 KB Output is correct
41 Correct 199 ms 95828 KB Output is correct
42 Correct 211 ms 97056 KB Output is correct
43 Correct 47 ms 78556 KB Output is correct
44 Correct 247 ms 103764 KB Output is correct
45 Correct 233 ms 98572 KB Output is correct
46 Correct 44 ms 79156 KB Output is correct
47 Correct 45 ms 79040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 95136 KB Output is correct
2 Correct 141 ms 101172 KB Output is correct
3 Correct 49 ms 79180 KB Output is correct
4 Correct 43 ms 79044 KB Output is correct
5 Correct 42 ms 78540 KB Output is correct
6 Correct 43 ms 78656 KB Output is correct
7 Correct 46 ms 79152 KB Output is correct
8 Correct 223 ms 96232 KB Output is correct
9 Correct 43 ms 79120 KB Output is correct
10 Correct 180 ms 94936 KB Output is correct
11 Correct 48 ms 78564 KB Output is correct
12 Correct 169 ms 94912 KB Output is correct
13 Correct 187 ms 96348 KB Output is correct
14 Correct 178 ms 100684 KB Output is correct
15 Correct 161 ms 95020 KB Output is correct
16 Correct 46 ms 79184 KB Output is correct
17 Correct 43 ms 78624 KB Output is correct
18 Correct 162 ms 100236 KB Output is correct
19 Correct 225 ms 104480 KB Output is correct
20 Correct 46 ms 79172 KB Output is correct
21 Correct 41 ms 78684 KB Output is correct
22 Correct 173 ms 96784 KB Output is correct
23 Correct 45 ms 79048 KB Output is correct
24 Correct 185 ms 95272 KB Output is correct
25 Correct 223 ms 103108 KB Output is correct
26 Correct 54 ms 79308 KB Output is correct
27 Correct 50 ms 79256 KB Output is correct
28 Correct 44 ms 79060 KB Output is correct
29 Correct 44 ms 79160 KB Output is correct
30 Correct 45 ms 79208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 78540 KB Output is correct
2 Correct 39 ms 78612 KB Output is correct
3 Correct 39 ms 78628 KB Output is correct
4 Correct 41 ms 78596 KB Output is correct
5 Correct 40 ms 78548 KB Output is correct
6 Correct 39 ms 78676 KB Output is correct
7 Correct 51 ms 78540 KB Output is correct
8 Correct 40 ms 78596 KB Output is correct
9 Correct 40 ms 78548 KB Output is correct
10 Correct 40 ms 78656 KB Output is correct
11 Correct 47 ms 78600 KB Output is correct
12 Correct 41 ms 78668 KB Output is correct
13 Correct 44 ms 78536 KB Output is correct
14 Correct 44 ms 78632 KB Output is correct
15 Correct 50 ms 78632 KB Output is correct
16 Correct 45 ms 78664 KB Output is correct
17 Correct 44 ms 78564 KB Output is correct
18 Correct 49 ms 78756 KB Output is correct
19 Correct 48 ms 78548 KB Output is correct
20 Correct 49 ms 78592 KB Output is correct
21 Correct 41 ms 78556 KB Output is correct
22 Correct 46 ms 78596 KB Output is correct
23 Correct 48 ms 78668 KB Output is correct
24 Correct 42 ms 78668 KB Output is correct
25 Correct 41 ms 78548 KB Output is correct
26 Correct 61 ms 79140 KB Output is correct
27 Correct 44 ms 79068 KB Output is correct
28 Correct 53 ms 79376 KB Output is correct
29 Correct 43 ms 79308 KB Output is correct
30 Correct 54 ms 79116 KB Output is correct
31 Correct 41 ms 78548 KB Output is correct
32 Correct 44 ms 79312 KB Output is correct
33 Correct 41 ms 78540 KB Output is correct
34 Correct 49 ms 79104 KB Output is correct
35 Correct 45 ms 79308 KB Output is correct
36 Correct 45 ms 79140 KB Output is correct
37 Correct 47 ms 79052 KB Output is correct
38 Correct 48 ms 78668 KB Output is correct
39 Correct 45 ms 79080 KB Output is correct
40 Correct 49 ms 79132 KB Output is correct
41 Correct 51 ms 79052 KB Output is correct
42 Correct 55 ms 79180 KB Output is correct
43 Correct 45 ms 79344 KB Output is correct
44 Correct 45 ms 78656 KB Output is correct
45 Correct 45 ms 79272 KB Output is correct
46 Correct 46 ms 79188 KB Output is correct
47 Correct 45 ms 78664 KB Output is correct
48 Correct 46 ms 79148 KB Output is correct
49 Correct 46 ms 79332 KB Output is correct
50 Correct 44 ms 79252 KB Output is correct
51 Correct 45 ms 79032 KB Output is correct
52 Correct 44 ms 79060 KB Output is correct
53 Correct 44 ms 79316 KB Output is correct
54 Correct 51 ms 79148 KB Output is correct
55 Correct 42 ms 78548 KB Output is correct
56 Correct 188 ms 95148 KB Output is correct
57 Correct 234 ms 94896 KB Output is correct
58 Correct 44 ms 79148 KB Output is correct
59 Correct 42 ms 78652 KB Output is correct
60 Correct 43 ms 78652 KB Output is correct
61 Correct 226 ms 94776 KB Output is correct
62 Correct 43 ms 79136 KB Output is correct
63 Correct 230 ms 102044 KB Output is correct
64 Correct 249 ms 94868 KB Output is correct
65 Correct 46 ms 79140 KB Output is correct
66 Correct 185 ms 95500 KB Output is correct
67 Correct 45 ms 79180 KB Output is correct
68 Correct 45 ms 79088 KB Output is correct
69 Correct 171 ms 95204 KB Output is correct
70 Correct 44 ms 79340 KB Output is correct
71 Correct 199 ms 95828 KB Output is correct
72 Correct 211 ms 97056 KB Output is correct
73 Correct 47 ms 78556 KB Output is correct
74 Correct 247 ms 103764 KB Output is correct
75 Correct 233 ms 98572 KB Output is correct
76 Correct 44 ms 79156 KB Output is correct
77 Correct 45 ms 79040 KB Output is correct
78 Correct 186 ms 95136 KB Output is correct
79 Correct 141 ms 101172 KB Output is correct
80 Correct 49 ms 79180 KB Output is correct
81 Correct 43 ms 79044 KB Output is correct
82 Correct 42 ms 78540 KB Output is correct
83 Correct 43 ms 78656 KB Output is correct
84 Correct 46 ms 79152 KB Output is correct
85 Correct 223 ms 96232 KB Output is correct
86 Correct 43 ms 79120 KB Output is correct
87 Correct 180 ms 94936 KB Output is correct
88 Correct 48 ms 78564 KB Output is correct
89 Correct 169 ms 94912 KB Output is correct
90 Correct 187 ms 96348 KB Output is correct
91 Correct 178 ms 100684 KB Output is correct
92 Correct 161 ms 95020 KB Output is correct
93 Correct 46 ms 79184 KB Output is correct
94 Correct 43 ms 78624 KB Output is correct
95 Correct 162 ms 100236 KB Output is correct
96 Correct 225 ms 104480 KB Output is correct
97 Correct 46 ms 79172 KB Output is correct
98 Correct 41 ms 78684 KB Output is correct
99 Correct 173 ms 96784 KB Output is correct
100 Correct 45 ms 79048 KB Output is correct
101 Correct 185 ms 95272 KB Output is correct
102 Correct 223 ms 103108 KB Output is correct
103 Correct 54 ms 79308 KB Output is correct
104 Correct 50 ms 79256 KB Output is correct
105 Correct 44 ms 79060 KB Output is correct
106 Correct 44 ms 79160 KB Output is correct
107 Correct 45 ms 79208 KB Output is correct
108 Correct 874 ms 165272 KB Output is correct
109 Correct 1292 ms 179740 KB Output is correct
110 Correct 1402 ms 194364 KB Output is correct
111 Correct 1376 ms 208848 KB Output is correct
112 Correct 1229 ms 193964 KB Output is correct
113 Correct 883 ms 185476 KB Output is correct
114 Correct 1159 ms 169280 KB Output is correct
115 Correct 1194 ms 169540 KB Output is correct
116 Correct 1782 ms 170092 KB Output is correct
117 Correct 1131 ms 197448 KB Output is correct
118 Correct 1027 ms 166000 KB Output is correct
119 Correct 1079 ms 197468 KB Output is correct
120 Correct 1303 ms 210936 KB Output is correct
121 Correct 1114 ms 197592 KB Output is correct
122 Correct 1606 ms 177948 KB Output is correct
123 Correct 794 ms 200524 KB Output is correct
124 Correct 1161 ms 189216 KB Output is correct