답안 #630495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630495 2022-08-16T12:28:27 Z Arnch Mergers (JOI19_mergers) C++17
70 / 100
1275 ms 259476 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 = 5e5 + 2, LOG = 20;

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;
}

int get_par(int x, int y) {
	for(int i = 0; i < LOG; i++)
		if((y >> i) & 1)
			x = par[x][i];
	return x;
}
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]);
}
void merge(int x, int y) {
//	cout<<"^^" <<x <<' ' <<y <<endl;
	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];
}
void solve(int x) {
	vector<int> ver;
	for(auto i : vc[x]) ver.push_back(i);
	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());

	stack<int> mt;
	mt.push(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.top()] < fn[v]) mt.pop();
		int p = mt.top();
	
		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(v);
	}
	ver.clear();
	while(!mt.empty()) mt.pop();
}

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]);
		}
	}

	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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39508 KB Output is correct
2 Correct 20 ms 39488 KB Output is correct
3 Correct 24 ms 39508 KB Output is correct
4 Correct 25 ms 39420 KB Output is correct
5 Correct 22 ms 39528 KB Output is correct
6 Correct 20 ms 39520 KB Output is correct
7 Correct 20 ms 39520 KB Output is correct
8 Correct 20 ms 39428 KB Output is correct
9 Correct 20 ms 39424 KB Output is correct
10 Correct 20 ms 39444 KB Output is correct
11 Correct 20 ms 39508 KB Output is correct
12 Correct 20 ms 39460 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 26 ms 39452 KB Output is correct
15 Correct 21 ms 39540 KB Output is correct
16 Correct 22 ms 39508 KB Output is correct
17 Correct 20 ms 39448 KB Output is correct
18 Correct 20 ms 39508 KB Output is correct
19 Correct 22 ms 39496 KB Output is correct
20 Correct 20 ms 39508 KB Output is correct
21 Correct 20 ms 39508 KB Output is correct
22 Correct 21 ms 39500 KB Output is correct
23 Correct 21 ms 39508 KB Output is correct
24 Correct 20 ms 39464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39508 KB Output is correct
2 Correct 20 ms 39488 KB Output is correct
3 Correct 24 ms 39508 KB Output is correct
4 Correct 25 ms 39420 KB Output is correct
5 Correct 22 ms 39528 KB Output is correct
6 Correct 20 ms 39520 KB Output is correct
7 Correct 20 ms 39520 KB Output is correct
8 Correct 20 ms 39428 KB Output is correct
9 Correct 20 ms 39424 KB Output is correct
10 Correct 20 ms 39444 KB Output is correct
11 Correct 20 ms 39508 KB Output is correct
12 Correct 20 ms 39460 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 26 ms 39452 KB Output is correct
15 Correct 21 ms 39540 KB Output is correct
16 Correct 22 ms 39508 KB Output is correct
17 Correct 20 ms 39448 KB Output is correct
18 Correct 20 ms 39508 KB Output is correct
19 Correct 22 ms 39496 KB Output is correct
20 Correct 20 ms 39508 KB Output is correct
21 Correct 20 ms 39508 KB Output is correct
22 Correct 21 ms 39500 KB Output is correct
23 Correct 21 ms 39508 KB Output is correct
24 Correct 20 ms 39464 KB Output is correct
25 Correct 20 ms 39444 KB Output is correct
26 Correct 22 ms 40096 KB Output is correct
27 Correct 23 ms 39900 KB Output is correct
28 Correct 22 ms 40184 KB Output is correct
29 Correct 22 ms 40148 KB Output is correct
30 Correct 24 ms 39896 KB Output is correct
31 Correct 24 ms 39540 KB Output is correct
32 Correct 25 ms 40148 KB Output is correct
33 Correct 21 ms 39508 KB Output is correct
34 Correct 25 ms 39968 KB Output is correct
35 Correct 23 ms 40092 KB Output is correct
36 Correct 23 ms 39912 KB Output is correct
37 Correct 23 ms 39960 KB Output is correct
38 Correct 26 ms 39548 KB Output is correct
39 Correct 23 ms 40092 KB Output is correct
40 Correct 23 ms 40036 KB Output is correct
41 Correct 27 ms 40004 KB Output is correct
42 Correct 23 ms 40072 KB Output is correct
43 Correct 25 ms 40184 KB Output is correct
44 Correct 21 ms 39536 KB Output is correct
45 Correct 22 ms 40112 KB Output is correct
46 Correct 23 ms 40020 KB Output is correct
47 Correct 20 ms 39500 KB Output is correct
48 Correct 24 ms 39908 KB Output is correct
49 Correct 26 ms 40096 KB Output is correct
50 Correct 23 ms 40116 KB Output is correct
51 Correct 27 ms 39900 KB Output is correct
52 Correct 29 ms 40020 KB Output is correct
53 Correct 22 ms 39976 KB Output is correct
54 Correct 24 ms 40000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39508 KB Output is correct
2 Correct 20 ms 39488 KB Output is correct
3 Correct 24 ms 39508 KB Output is correct
4 Correct 25 ms 39420 KB Output is correct
5 Correct 22 ms 39528 KB Output is correct
6 Correct 20 ms 39520 KB Output is correct
7 Correct 20 ms 39520 KB Output is correct
8 Correct 20 ms 39428 KB Output is correct
9 Correct 20 ms 39424 KB Output is correct
10 Correct 20 ms 39444 KB Output is correct
11 Correct 20 ms 39508 KB Output is correct
12 Correct 20 ms 39460 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 26 ms 39452 KB Output is correct
15 Correct 21 ms 39540 KB Output is correct
16 Correct 22 ms 39508 KB Output is correct
17 Correct 20 ms 39448 KB Output is correct
18 Correct 20 ms 39508 KB Output is correct
19 Correct 22 ms 39496 KB Output is correct
20 Correct 20 ms 39508 KB Output is correct
21 Correct 20 ms 39508 KB Output is correct
22 Correct 21 ms 39500 KB Output is correct
23 Correct 21 ms 39508 KB Output is correct
24 Correct 20 ms 39464 KB Output is correct
25 Correct 20 ms 39636 KB Output is correct
26 Correct 125 ms 55108 KB Output is correct
27 Correct 212 ms 55020 KB Output is correct
28 Correct 24 ms 40020 KB Output is correct
29 Correct 25 ms 39508 KB Output is correct
30 Correct 21 ms 39428 KB Output is correct
31 Correct 157 ms 54876 KB Output is correct
32 Correct 23 ms 40020 KB Output is correct
33 Correct 193 ms 62056 KB Output is correct
34 Correct 208 ms 55044 KB Output is correct
35 Correct 23 ms 39888 KB Output is correct
36 Correct 161 ms 55596 KB Output is correct
37 Correct 22 ms 39996 KB Output is correct
38 Correct 27 ms 40000 KB Output is correct
39 Correct 132 ms 55240 KB Output is correct
40 Correct 24 ms 40212 KB Output is correct
41 Correct 168 ms 55812 KB Output is correct
42 Correct 183 ms 57208 KB Output is correct
43 Correct 19 ms 39508 KB Output is correct
44 Correct 188 ms 63392 KB Output is correct
45 Correct 226 ms 58628 KB Output is correct
46 Correct 22 ms 40020 KB Output is correct
47 Correct 25 ms 40004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 55208 KB Output is correct
2 Correct 123 ms 61228 KB Output is correct
3 Correct 24 ms 40020 KB Output is correct
4 Correct 24 ms 39892 KB Output is correct
5 Correct 22 ms 39432 KB Output is correct
6 Correct 20 ms 39508 KB Output is correct
7 Correct 22 ms 39956 KB Output is correct
8 Correct 207 ms 56272 KB Output is correct
9 Correct 24 ms 40008 KB Output is correct
10 Correct 149 ms 55044 KB Output is correct
11 Correct 22 ms 39508 KB Output is correct
12 Correct 143 ms 54860 KB Output is correct
13 Correct 174 ms 56496 KB Output is correct
14 Correct 144 ms 60724 KB Output is correct
15 Correct 144 ms 55204 KB Output is correct
16 Correct 24 ms 40020 KB Output is correct
17 Correct 20 ms 39508 KB Output is correct
18 Correct 124 ms 60212 KB Output is correct
19 Correct 158 ms 64432 KB Output is correct
20 Correct 24 ms 40032 KB Output is correct
21 Correct 20 ms 39464 KB Output is correct
22 Correct 149 ms 56844 KB Output is correct
23 Correct 23 ms 39892 KB Output is correct
24 Correct 142 ms 55284 KB Output is correct
25 Correct 168 ms 63180 KB Output is correct
26 Correct 23 ms 40108 KB Output is correct
27 Correct 24 ms 40148 KB Output is correct
28 Correct 24 ms 39940 KB Output is correct
29 Correct 23 ms 40020 KB Output is correct
30 Correct 23 ms 40020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39508 KB Output is correct
2 Correct 20 ms 39488 KB Output is correct
3 Correct 24 ms 39508 KB Output is correct
4 Correct 25 ms 39420 KB Output is correct
5 Correct 22 ms 39528 KB Output is correct
6 Correct 20 ms 39520 KB Output is correct
7 Correct 20 ms 39520 KB Output is correct
8 Correct 20 ms 39428 KB Output is correct
9 Correct 20 ms 39424 KB Output is correct
10 Correct 20 ms 39444 KB Output is correct
11 Correct 20 ms 39508 KB Output is correct
12 Correct 20 ms 39460 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 26 ms 39452 KB Output is correct
15 Correct 21 ms 39540 KB Output is correct
16 Correct 22 ms 39508 KB Output is correct
17 Correct 20 ms 39448 KB Output is correct
18 Correct 20 ms 39508 KB Output is correct
19 Correct 22 ms 39496 KB Output is correct
20 Correct 20 ms 39508 KB Output is correct
21 Correct 20 ms 39508 KB Output is correct
22 Correct 21 ms 39500 KB Output is correct
23 Correct 21 ms 39508 KB Output is correct
24 Correct 20 ms 39464 KB Output is correct
25 Correct 20 ms 39444 KB Output is correct
26 Correct 22 ms 40096 KB Output is correct
27 Correct 23 ms 39900 KB Output is correct
28 Correct 22 ms 40184 KB Output is correct
29 Correct 22 ms 40148 KB Output is correct
30 Correct 24 ms 39896 KB Output is correct
31 Correct 24 ms 39540 KB Output is correct
32 Correct 25 ms 40148 KB Output is correct
33 Correct 21 ms 39508 KB Output is correct
34 Correct 25 ms 39968 KB Output is correct
35 Correct 23 ms 40092 KB Output is correct
36 Correct 23 ms 39912 KB Output is correct
37 Correct 23 ms 39960 KB Output is correct
38 Correct 26 ms 39548 KB Output is correct
39 Correct 23 ms 40092 KB Output is correct
40 Correct 23 ms 40036 KB Output is correct
41 Correct 27 ms 40004 KB Output is correct
42 Correct 23 ms 40072 KB Output is correct
43 Correct 25 ms 40184 KB Output is correct
44 Correct 21 ms 39536 KB Output is correct
45 Correct 22 ms 40112 KB Output is correct
46 Correct 23 ms 40020 KB Output is correct
47 Correct 20 ms 39500 KB Output is correct
48 Correct 24 ms 39908 KB Output is correct
49 Correct 26 ms 40096 KB Output is correct
50 Correct 23 ms 40116 KB Output is correct
51 Correct 27 ms 39900 KB Output is correct
52 Correct 29 ms 40020 KB Output is correct
53 Correct 22 ms 39976 KB Output is correct
54 Correct 24 ms 40000 KB Output is correct
55 Correct 20 ms 39636 KB Output is correct
56 Correct 125 ms 55108 KB Output is correct
57 Correct 212 ms 55020 KB Output is correct
58 Correct 24 ms 40020 KB Output is correct
59 Correct 25 ms 39508 KB Output is correct
60 Correct 21 ms 39428 KB Output is correct
61 Correct 157 ms 54876 KB Output is correct
62 Correct 23 ms 40020 KB Output is correct
63 Correct 193 ms 62056 KB Output is correct
64 Correct 208 ms 55044 KB Output is correct
65 Correct 23 ms 39888 KB Output is correct
66 Correct 161 ms 55596 KB Output is correct
67 Correct 22 ms 39996 KB Output is correct
68 Correct 27 ms 40000 KB Output is correct
69 Correct 132 ms 55240 KB Output is correct
70 Correct 24 ms 40212 KB Output is correct
71 Correct 168 ms 55812 KB Output is correct
72 Correct 183 ms 57208 KB Output is correct
73 Correct 19 ms 39508 KB Output is correct
74 Correct 188 ms 63392 KB Output is correct
75 Correct 226 ms 58628 KB Output is correct
76 Correct 22 ms 40020 KB Output is correct
77 Correct 25 ms 40004 KB Output is correct
78 Correct 146 ms 55208 KB Output is correct
79 Correct 123 ms 61228 KB Output is correct
80 Correct 24 ms 40020 KB Output is correct
81 Correct 24 ms 39892 KB Output is correct
82 Correct 22 ms 39432 KB Output is correct
83 Correct 20 ms 39508 KB Output is correct
84 Correct 22 ms 39956 KB Output is correct
85 Correct 207 ms 56272 KB Output is correct
86 Correct 24 ms 40008 KB Output is correct
87 Correct 149 ms 55044 KB Output is correct
88 Correct 22 ms 39508 KB Output is correct
89 Correct 143 ms 54860 KB Output is correct
90 Correct 174 ms 56496 KB Output is correct
91 Correct 144 ms 60724 KB Output is correct
92 Correct 144 ms 55204 KB Output is correct
93 Correct 24 ms 40020 KB Output is correct
94 Correct 20 ms 39508 KB Output is correct
95 Correct 124 ms 60212 KB Output is correct
96 Correct 158 ms 64432 KB Output is correct
97 Correct 24 ms 40032 KB Output is correct
98 Correct 20 ms 39464 KB Output is correct
99 Correct 149 ms 56844 KB Output is correct
100 Correct 23 ms 39892 KB Output is correct
101 Correct 142 ms 55284 KB Output is correct
102 Correct 168 ms 63180 KB Output is correct
103 Correct 23 ms 40108 KB Output is correct
104 Correct 24 ms 40148 KB Output is correct
105 Correct 24 ms 39940 KB Output is correct
106 Correct 23 ms 40020 KB Output is correct
107 Correct 23 ms 40020 KB Output is correct
108 Correct 840 ms 122032 KB Output is correct
109 Correct 1275 ms 136652 KB Output is correct
110 Correct 1238 ms 149348 KB Output is correct
111 Correct 1212 ms 165808 KB Output is correct
112 Correct 1129 ms 150812 KB Output is correct
113 Runtime error 453 ms 259476 KB Execution killed with signal 11
114 Halted 0 ms 0 KB -