Submission #630346

# Submission time Handle Problem Language Result Execution time Memory
630346 2022-08-16T08:38:34 Z Arnch Mergers (JOI19_mergers) C++17
56 / 100
3000 ms 102120 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 ll INF = 1e18, N = 1e6 + 10, MOD = 1e9 + 7, LOG = 20;

int s[N];
int par[N][LOG], h[N], st[N], fn[N], tim;
int sz[N], pv[N];
int ans;
vector<int> vc[N], adj[N], nei[N];

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];
	h[x] = h[p] + 1;
	st[x] = tim++;
	for(auto j : adj[x]) {
		if(j == p) continue;
		dfs(j, x);
	}
	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) {
	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];
}

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

	merge(ver[0], 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();
		
		merge(x, v);
		v = par[v][0];
		while(v != p) {
			merge(x, v);
			v = par[v][0];
		}
		mt.push(v);
	}
}

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

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

	int n, k; 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);
	}
	for(int i = 0; i < n; i++) {
		cin >>s[i];
		--s[i], s[i] += n;
		vc[s[i]].push_back(i);
	}

	dfs(0);

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

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

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

	for(int i = 0; i < N; 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 46 ms 78548 KB Output is correct
2 Correct 40 ms 78648 KB Output is correct
3 Correct 42 ms 78520 KB Output is correct
4 Correct 47 ms 78616 KB Output is correct
5 Correct 41 ms 78568 KB Output is correct
6 Correct 44 ms 78632 KB Output is correct
7 Correct 43 ms 78620 KB Output is correct
8 Correct 42 ms 78600 KB Output is correct
9 Correct 42 ms 78508 KB Output is correct
10 Correct 42 ms 78536 KB Output is correct
11 Correct 43 ms 78548 KB Output is correct
12 Correct 47 ms 78608 KB Output is correct
13 Correct 44 ms 78616 KB Output is correct
14 Correct 46 ms 78564 KB Output is correct
15 Correct 43 ms 78548 KB Output is correct
16 Correct 47 ms 78608 KB Output is correct
17 Correct 42 ms 78616 KB Output is correct
18 Correct 43 ms 78616 KB Output is correct
19 Correct 42 ms 78540 KB Output is correct
20 Correct 45 ms 78548 KB Output is correct
21 Correct 42 ms 78564 KB Output is correct
22 Correct 43 ms 78604 KB Output is correct
23 Correct 42 ms 78604 KB Output is correct
24 Correct 44 ms 78540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 78548 KB Output is correct
2 Correct 40 ms 78648 KB Output is correct
3 Correct 42 ms 78520 KB Output is correct
4 Correct 47 ms 78616 KB Output is correct
5 Correct 41 ms 78568 KB Output is correct
6 Correct 44 ms 78632 KB Output is correct
7 Correct 43 ms 78620 KB Output is correct
8 Correct 42 ms 78600 KB Output is correct
9 Correct 42 ms 78508 KB Output is correct
10 Correct 42 ms 78536 KB Output is correct
11 Correct 43 ms 78548 KB Output is correct
12 Correct 47 ms 78608 KB Output is correct
13 Correct 44 ms 78616 KB Output is correct
14 Correct 46 ms 78564 KB Output is correct
15 Correct 43 ms 78548 KB Output is correct
16 Correct 47 ms 78608 KB Output is correct
17 Correct 42 ms 78616 KB Output is correct
18 Correct 43 ms 78616 KB Output is correct
19 Correct 42 ms 78540 KB Output is correct
20 Correct 45 ms 78548 KB Output is correct
21 Correct 42 ms 78564 KB Output is correct
22 Correct 43 ms 78604 KB Output is correct
23 Correct 42 ms 78604 KB Output is correct
24 Correct 44 ms 78540 KB Output is correct
25 Correct 44 ms 78528 KB Output is correct
26 Correct 51 ms 79184 KB Output is correct
27 Correct 45 ms 78992 KB Output is correct
28 Correct 44 ms 79284 KB Output is correct
29 Correct 46 ms 79248 KB Output is correct
30 Correct 50 ms 78984 KB Output is correct
31 Correct 50 ms 78548 KB Output is correct
32 Correct 46 ms 79244 KB Output is correct
33 Correct 47 ms 78712 KB Output is correct
34 Correct 45 ms 79036 KB Output is correct
35 Correct 46 ms 79180 KB Output is correct
36 Correct 46 ms 78968 KB Output is correct
37 Correct 48 ms 79004 KB Output is correct
38 Correct 46 ms 78592 KB Output is correct
39 Correct 45 ms 79016 KB Output is correct
40 Correct 46 ms 79072 KB Output is correct
41 Correct 45 ms 79064 KB Output is correct
42 Correct 45 ms 79076 KB Output is correct
43 Correct 88 ms 79320 KB Output is correct
44 Correct 46 ms 78552 KB Output is correct
45 Correct 49 ms 79164 KB Output is correct
46 Correct 45 ms 79076 KB Output is correct
47 Correct 43 ms 78648 KB Output is correct
48 Correct 52 ms 79044 KB Output is correct
49 Correct 44 ms 79176 KB Output is correct
50 Correct 47 ms 79200 KB Output is correct
51 Correct 45 ms 79060 KB Output is correct
52 Correct 56 ms 79028 KB Output is correct
53 Correct 57 ms 79128 KB Output is correct
54 Correct 45 ms 78972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 78548 KB Output is correct
2 Correct 40 ms 78648 KB Output is correct
3 Correct 42 ms 78520 KB Output is correct
4 Correct 47 ms 78616 KB Output is correct
5 Correct 41 ms 78568 KB Output is correct
6 Correct 44 ms 78632 KB Output is correct
7 Correct 43 ms 78620 KB Output is correct
8 Correct 42 ms 78600 KB Output is correct
9 Correct 42 ms 78508 KB Output is correct
10 Correct 42 ms 78536 KB Output is correct
11 Correct 43 ms 78548 KB Output is correct
12 Correct 47 ms 78608 KB Output is correct
13 Correct 44 ms 78616 KB Output is correct
14 Correct 46 ms 78564 KB Output is correct
15 Correct 43 ms 78548 KB Output is correct
16 Correct 47 ms 78608 KB Output is correct
17 Correct 42 ms 78616 KB Output is correct
18 Correct 43 ms 78616 KB Output is correct
19 Correct 42 ms 78540 KB Output is correct
20 Correct 45 ms 78548 KB Output is correct
21 Correct 42 ms 78564 KB Output is correct
22 Correct 43 ms 78604 KB Output is correct
23 Correct 42 ms 78604 KB Output is correct
24 Correct 44 ms 78540 KB Output is correct
25 Correct 44 ms 78544 KB Output is correct
26 Correct 130 ms 93636 KB Output is correct
27 Correct 212 ms 93148 KB Output is correct
28 Correct 45 ms 78968 KB Output is correct
29 Correct 43 ms 78636 KB Output is correct
30 Correct 48 ms 78608 KB Output is correct
31 Correct 165 ms 93212 KB Output is correct
32 Correct 59 ms 79060 KB Output is correct
33 Execution timed out 3087 ms 97408 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 145 ms 92036 KB Output is correct
2 Correct 136 ms 98500 KB Output is correct
3 Correct 56 ms 79060 KB Output is correct
4 Correct 47 ms 78924 KB Output is correct
5 Correct 44 ms 78524 KB Output is correct
6 Correct 43 ms 78572 KB Output is correct
7 Correct 43 ms 78948 KB Output is correct
8 Correct 150 ms 93272 KB Output is correct
9 Correct 45 ms 79012 KB Output is correct
10 Correct 177 ms 93404 KB Output is correct
11 Correct 43 ms 78628 KB Output is correct
12 Correct 176 ms 93320 KB Output is correct
13 Correct 155 ms 95260 KB Output is correct
14 Correct 196 ms 99904 KB Output is correct
15 Correct 127 ms 93456 KB Output is correct
16 Correct 46 ms 79052 KB Output is correct
17 Correct 42 ms 78548 KB Output is correct
18 Correct 138 ms 99620 KB Output is correct
19 Correct 206 ms 101568 KB Output is correct
20 Correct 46 ms 79096 KB Output is correct
21 Correct 44 ms 78564 KB Output is correct
22 Correct 137 ms 95520 KB Output is correct
23 Correct 48 ms 79072 KB Output is correct
24 Correct 152 ms 93864 KB Output is correct
25 Correct 180 ms 102120 KB Output is correct
26 Correct 45 ms 79172 KB Output is correct
27 Correct 46 ms 79308 KB Output is correct
28 Correct 47 ms 79052 KB Output is correct
29 Correct 48 ms 79084 KB Output is correct
30 Correct 46 ms 79112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 78548 KB Output is correct
2 Correct 40 ms 78648 KB Output is correct
3 Correct 42 ms 78520 KB Output is correct
4 Correct 47 ms 78616 KB Output is correct
5 Correct 41 ms 78568 KB Output is correct
6 Correct 44 ms 78632 KB Output is correct
7 Correct 43 ms 78620 KB Output is correct
8 Correct 42 ms 78600 KB Output is correct
9 Correct 42 ms 78508 KB Output is correct
10 Correct 42 ms 78536 KB Output is correct
11 Correct 43 ms 78548 KB Output is correct
12 Correct 47 ms 78608 KB Output is correct
13 Correct 44 ms 78616 KB Output is correct
14 Correct 46 ms 78564 KB Output is correct
15 Correct 43 ms 78548 KB Output is correct
16 Correct 47 ms 78608 KB Output is correct
17 Correct 42 ms 78616 KB Output is correct
18 Correct 43 ms 78616 KB Output is correct
19 Correct 42 ms 78540 KB Output is correct
20 Correct 45 ms 78548 KB Output is correct
21 Correct 42 ms 78564 KB Output is correct
22 Correct 43 ms 78604 KB Output is correct
23 Correct 42 ms 78604 KB Output is correct
24 Correct 44 ms 78540 KB Output is correct
25 Correct 44 ms 78528 KB Output is correct
26 Correct 51 ms 79184 KB Output is correct
27 Correct 45 ms 78992 KB Output is correct
28 Correct 44 ms 79284 KB Output is correct
29 Correct 46 ms 79248 KB Output is correct
30 Correct 50 ms 78984 KB Output is correct
31 Correct 50 ms 78548 KB Output is correct
32 Correct 46 ms 79244 KB Output is correct
33 Correct 47 ms 78712 KB Output is correct
34 Correct 45 ms 79036 KB Output is correct
35 Correct 46 ms 79180 KB Output is correct
36 Correct 46 ms 78968 KB Output is correct
37 Correct 48 ms 79004 KB Output is correct
38 Correct 46 ms 78592 KB Output is correct
39 Correct 45 ms 79016 KB Output is correct
40 Correct 46 ms 79072 KB Output is correct
41 Correct 45 ms 79064 KB Output is correct
42 Correct 45 ms 79076 KB Output is correct
43 Correct 88 ms 79320 KB Output is correct
44 Correct 46 ms 78552 KB Output is correct
45 Correct 49 ms 79164 KB Output is correct
46 Correct 45 ms 79076 KB Output is correct
47 Correct 43 ms 78648 KB Output is correct
48 Correct 52 ms 79044 KB Output is correct
49 Correct 44 ms 79176 KB Output is correct
50 Correct 47 ms 79200 KB Output is correct
51 Correct 45 ms 79060 KB Output is correct
52 Correct 56 ms 79028 KB Output is correct
53 Correct 57 ms 79128 KB Output is correct
54 Correct 45 ms 78972 KB Output is correct
55 Correct 44 ms 78544 KB Output is correct
56 Correct 130 ms 93636 KB Output is correct
57 Correct 212 ms 93148 KB Output is correct
58 Correct 45 ms 78968 KB Output is correct
59 Correct 43 ms 78636 KB Output is correct
60 Correct 48 ms 78608 KB Output is correct
61 Correct 165 ms 93212 KB Output is correct
62 Correct 59 ms 79060 KB Output is correct
63 Execution timed out 3087 ms 97408 KB Time limit exceeded
64 Halted 0 ms 0 KB -