Submission #411027

# Submission time Handle Problem Language Result Execution time Memory
411027 2021-05-24T06:55:13 Z alireza_kaviani Mergers (JOI19_mergers) C++11
100 / 100
1029 ms 141372 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define all(x)						(x).begin(),(x).end()
#define X							first
#define Y							second
#define sep							' '
#define endl						'\n'
#define SZ(x)						ll(x.size())

const ll MAXN = 5e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;

int n , k , timer , ans , cnt , col[MAXN] , par[LOG][MAXN] , H[MAXN] , st[MAXN] , ps[MAXN] , mark[MAXN];
vector<int> adj[MAXN] , vec[MAXN];

int cmp(int x , int y){
	return st[x] < st[y];
}

void PreDFS(int v , int p = 0){
	par[0][v] = p;
	st[v] = timer++;
	H[v] = H[p] + 1;
	for(int u : adj[v]){
		if(u == p)	continue;
		PreDFS(u , v);
	}
}

int getPar(int v , int h){
	for(int i = 0 ; i < LOG ; i++){
		if(h & (1 << i)){
			v = par[i][v];
		}
	}
	return v;
}

int LCA(int v , int u){
	if(H[v] > H[u])	swap(v , u);
	u = getPar(u , H[u] - H[v]);
	if(v == u)	return v;
	for(int i = LOG - 1 ; i >= 0 ; i--){
		if(par[i][v] != par[i][u]){
			v = par[i][v];
			u = par[i][u];
		}
	}
	return par[0][v];
}

void SolveDFS(int v , int p = -1){
	for(int u : adj[v]){
		if(u == p)	continue;
		SolveDFS(u , v);
		ps[v] += ps[u];
		if(ps[u] == 0 && mark[u] == 0)	ans++;
		if(ps[u] == 0)	mark[v] = 1;
		mark[v] |= mark[u];
	}
}

void LeafDFS(int v , int p = -1){
	for(int u : adj[v]){
		if(u == p)	continue;
		if(ps[u] == 0){
			cnt++;
			continue;
		}
		LeafDFS(u , v);
	}
}

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

	cin >> n >> k;
	for(int i = 1 ; i < n ; i++){
		int v , u;
		cin >> v >> u;
		adj[v].push_back(u);
		adj[u].push_back(v);
	}
	for(int i = 1 ; i <= n ; i++){
		cin >> col[i];
		vec[col[i]].push_back(i);
	}
	PreDFS(1);
	for(int i = 1 ; i < LOG ; i++){
		for(int j = 0 ; j <= n ; j++){
			par[i][j] = par[i - 1][par[i - 1][j]];
		}
	}
	for(int i = 1 ; i <= k ; i++){
		sort(all(vec[i]) , cmp);
		for(int j = 0 ; j + 1 < SZ(vec[i]) ; j++){
			int v = vec[i][j] , u = vec[i][j + 1];
			int lca = LCA(v , u);
			ps[v]++; ps[u]++; ps[lca] -= 2;
		}
	}
	SolveDFS(1);
	LeafDFS(1);
	cout << (ans + (cnt == 1) + 1) / 2 << endl;

    return 0;
}
/*

*/
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 15 ms 24024 KB Output is correct
3 Correct 13 ms 23980 KB Output is correct
4 Correct 15 ms 23900 KB Output is correct
5 Correct 14 ms 24000 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 13 ms 24028 KB Output is correct
9 Correct 13 ms 24012 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 24004 KB Output is correct
12 Correct 15 ms 23940 KB Output is correct
13 Correct 14 ms 24004 KB Output is correct
14 Correct 14 ms 23984 KB Output is correct
15 Correct 14 ms 23976 KB Output is correct
16 Correct 14 ms 24016 KB Output is correct
17 Correct 14 ms 23936 KB Output is correct
18 Correct 14 ms 24012 KB Output is correct
19 Correct 13 ms 23932 KB Output is correct
20 Correct 13 ms 23944 KB Output is correct
21 Correct 13 ms 24000 KB Output is correct
22 Correct 13 ms 23916 KB Output is correct
23 Correct 13 ms 23976 KB Output is correct
24 Correct 14 ms 23984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 15 ms 24024 KB Output is correct
3 Correct 13 ms 23980 KB Output is correct
4 Correct 15 ms 23900 KB Output is correct
5 Correct 14 ms 24000 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 13 ms 24028 KB Output is correct
9 Correct 13 ms 24012 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 24004 KB Output is correct
12 Correct 15 ms 23940 KB Output is correct
13 Correct 14 ms 24004 KB Output is correct
14 Correct 14 ms 23984 KB Output is correct
15 Correct 14 ms 23976 KB Output is correct
16 Correct 14 ms 24016 KB Output is correct
17 Correct 14 ms 23936 KB Output is correct
18 Correct 14 ms 24012 KB Output is correct
19 Correct 13 ms 23932 KB Output is correct
20 Correct 13 ms 23944 KB Output is correct
21 Correct 13 ms 24000 KB Output is correct
22 Correct 13 ms 23916 KB Output is correct
23 Correct 13 ms 23976 KB Output is correct
24 Correct 14 ms 23984 KB Output is correct
25 Correct 13 ms 23960 KB Output is correct
26 Correct 17 ms 24536 KB Output is correct
27 Correct 16 ms 24464 KB Output is correct
28 Correct 16 ms 24592 KB Output is correct
29 Correct 16 ms 24452 KB Output is correct
30 Correct 17 ms 24468 KB Output is correct
31 Correct 15 ms 23996 KB Output is correct
32 Correct 16 ms 24732 KB Output is correct
33 Correct 15 ms 23944 KB Output is correct
34 Correct 15 ms 24396 KB Output is correct
35 Correct 17 ms 24468 KB Output is correct
36 Correct 16 ms 24396 KB Output is correct
37 Correct 16 ms 24440 KB Output is correct
38 Correct 15 ms 24052 KB Output is correct
39 Correct 16 ms 24464 KB Output is correct
40 Correct 16 ms 24396 KB Output is correct
41 Correct 16 ms 24464 KB Output is correct
42 Correct 15 ms 24408 KB Output is correct
43 Correct 15 ms 24652 KB Output is correct
44 Correct 13 ms 24012 KB Output is correct
45 Correct 16 ms 24592 KB Output is correct
46 Correct 16 ms 24520 KB Output is correct
47 Correct 14 ms 24012 KB Output is correct
48 Correct 16 ms 24464 KB Output is correct
49 Correct 15 ms 24464 KB Output is correct
50 Correct 16 ms 24624 KB Output is correct
51 Correct 17 ms 24396 KB Output is correct
52 Correct 15 ms 24420 KB Output is correct
53 Correct 16 ms 24520 KB Output is correct
54 Correct 16 ms 24460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 15 ms 24024 KB Output is correct
3 Correct 13 ms 23980 KB Output is correct
4 Correct 15 ms 23900 KB Output is correct
5 Correct 14 ms 24000 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 13 ms 24028 KB Output is correct
9 Correct 13 ms 24012 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 24004 KB Output is correct
12 Correct 15 ms 23940 KB Output is correct
13 Correct 14 ms 24004 KB Output is correct
14 Correct 14 ms 23984 KB Output is correct
15 Correct 14 ms 23976 KB Output is correct
16 Correct 14 ms 24016 KB Output is correct
17 Correct 14 ms 23936 KB Output is correct
18 Correct 14 ms 24012 KB Output is correct
19 Correct 13 ms 23932 KB Output is correct
20 Correct 13 ms 23944 KB Output is correct
21 Correct 13 ms 24000 KB Output is correct
22 Correct 13 ms 23916 KB Output is correct
23 Correct 13 ms 23976 KB Output is correct
24 Correct 14 ms 23984 KB Output is correct
25 Correct 13 ms 24012 KB Output is correct
26 Correct 114 ms 39596 KB Output is correct
27 Correct 144 ms 39716 KB Output is correct
28 Correct 19 ms 24512 KB Output is correct
29 Correct 13 ms 24012 KB Output is correct
30 Correct 15 ms 24012 KB Output is correct
31 Correct 113 ms 39704 KB Output is correct
32 Correct 16 ms 24492 KB Output is correct
33 Correct 127 ms 46796 KB Output is correct
34 Correct 130 ms 39676 KB Output is correct
35 Correct 15 ms 24396 KB Output is correct
36 Correct 106 ms 40344 KB Output is correct
37 Correct 16 ms 24396 KB Output is correct
38 Correct 16 ms 24516 KB Output is correct
39 Correct 122 ms 39620 KB Output is correct
40 Correct 17 ms 24716 KB Output is correct
41 Correct 129 ms 39612 KB Output is correct
42 Correct 122 ms 42052 KB Output is correct
43 Correct 14 ms 23932 KB Output is correct
44 Correct 128 ms 47020 KB Output is correct
45 Correct 145 ms 43548 KB Output is correct
46 Correct 16 ms 24396 KB Output is correct
47 Correct 16 ms 24424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 38152 KB Output is correct
2 Correct 77 ms 40332 KB Output is correct
3 Correct 17 ms 24524 KB Output is correct
4 Correct 16 ms 24464 KB Output is correct
5 Correct 14 ms 23936 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 16 ms 24396 KB Output is correct
8 Correct 140 ms 41004 KB Output is correct
9 Correct 16 ms 24484 KB Output is correct
10 Correct 112 ms 39876 KB Output is correct
11 Correct 16 ms 24024 KB Output is correct
12 Correct 103 ms 39752 KB Output is correct
13 Correct 126 ms 41228 KB Output is correct
14 Correct 98 ms 42636 KB Output is correct
15 Correct 105 ms 39632 KB Output is correct
16 Correct 15 ms 24524 KB Output is correct
17 Correct 14 ms 24012 KB Output is correct
18 Correct 89 ms 42288 KB Output is correct
19 Correct 125 ms 46644 KB Output is correct
20 Correct 17 ms 24396 KB Output is correct
21 Correct 14 ms 23928 KB Output is correct
22 Correct 99 ms 40836 KB Output is correct
23 Correct 16 ms 24424 KB Output is correct
24 Correct 108 ms 40136 KB Output is correct
25 Correct 129 ms 45124 KB Output is correct
26 Correct 15 ms 24544 KB Output is correct
27 Correct 16 ms 24568 KB Output is correct
28 Correct 17 ms 24564 KB Output is correct
29 Correct 16 ms 24396 KB Output is correct
30 Correct 15 ms 24444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24012 KB Output is correct
2 Correct 15 ms 24024 KB Output is correct
3 Correct 13 ms 23980 KB Output is correct
4 Correct 15 ms 23900 KB Output is correct
5 Correct 14 ms 24000 KB Output is correct
6 Correct 14 ms 24012 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 13 ms 24028 KB Output is correct
9 Correct 13 ms 24012 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 13 ms 24004 KB Output is correct
12 Correct 15 ms 23940 KB Output is correct
13 Correct 14 ms 24004 KB Output is correct
14 Correct 14 ms 23984 KB Output is correct
15 Correct 14 ms 23976 KB Output is correct
16 Correct 14 ms 24016 KB Output is correct
17 Correct 14 ms 23936 KB Output is correct
18 Correct 14 ms 24012 KB Output is correct
19 Correct 13 ms 23932 KB Output is correct
20 Correct 13 ms 23944 KB Output is correct
21 Correct 13 ms 24000 KB Output is correct
22 Correct 13 ms 23916 KB Output is correct
23 Correct 13 ms 23976 KB Output is correct
24 Correct 14 ms 23984 KB Output is correct
25 Correct 13 ms 23960 KB Output is correct
26 Correct 17 ms 24536 KB Output is correct
27 Correct 16 ms 24464 KB Output is correct
28 Correct 16 ms 24592 KB Output is correct
29 Correct 16 ms 24452 KB Output is correct
30 Correct 17 ms 24468 KB Output is correct
31 Correct 15 ms 23996 KB Output is correct
32 Correct 16 ms 24732 KB Output is correct
33 Correct 15 ms 23944 KB Output is correct
34 Correct 15 ms 24396 KB Output is correct
35 Correct 17 ms 24468 KB Output is correct
36 Correct 16 ms 24396 KB Output is correct
37 Correct 16 ms 24440 KB Output is correct
38 Correct 15 ms 24052 KB Output is correct
39 Correct 16 ms 24464 KB Output is correct
40 Correct 16 ms 24396 KB Output is correct
41 Correct 16 ms 24464 KB Output is correct
42 Correct 15 ms 24408 KB Output is correct
43 Correct 15 ms 24652 KB Output is correct
44 Correct 13 ms 24012 KB Output is correct
45 Correct 16 ms 24592 KB Output is correct
46 Correct 16 ms 24520 KB Output is correct
47 Correct 14 ms 24012 KB Output is correct
48 Correct 16 ms 24464 KB Output is correct
49 Correct 15 ms 24464 KB Output is correct
50 Correct 16 ms 24624 KB Output is correct
51 Correct 17 ms 24396 KB Output is correct
52 Correct 15 ms 24420 KB Output is correct
53 Correct 16 ms 24520 KB Output is correct
54 Correct 16 ms 24460 KB Output is correct
55 Correct 13 ms 24012 KB Output is correct
56 Correct 114 ms 39596 KB Output is correct
57 Correct 144 ms 39716 KB Output is correct
58 Correct 19 ms 24512 KB Output is correct
59 Correct 13 ms 24012 KB Output is correct
60 Correct 15 ms 24012 KB Output is correct
61 Correct 113 ms 39704 KB Output is correct
62 Correct 16 ms 24492 KB Output is correct
63 Correct 127 ms 46796 KB Output is correct
64 Correct 130 ms 39676 KB Output is correct
65 Correct 15 ms 24396 KB Output is correct
66 Correct 106 ms 40344 KB Output is correct
67 Correct 16 ms 24396 KB Output is correct
68 Correct 16 ms 24516 KB Output is correct
69 Correct 122 ms 39620 KB Output is correct
70 Correct 17 ms 24716 KB Output is correct
71 Correct 129 ms 39612 KB Output is correct
72 Correct 122 ms 42052 KB Output is correct
73 Correct 14 ms 23932 KB Output is correct
74 Correct 128 ms 47020 KB Output is correct
75 Correct 145 ms 43548 KB Output is correct
76 Correct 16 ms 24396 KB Output is correct
77 Correct 16 ms 24424 KB Output is correct
78 Correct 104 ms 38152 KB Output is correct
79 Correct 77 ms 40332 KB Output is correct
80 Correct 17 ms 24524 KB Output is correct
81 Correct 16 ms 24464 KB Output is correct
82 Correct 14 ms 23936 KB Output is correct
83 Correct 14 ms 24012 KB Output is correct
84 Correct 16 ms 24396 KB Output is correct
85 Correct 140 ms 41004 KB Output is correct
86 Correct 16 ms 24484 KB Output is correct
87 Correct 112 ms 39876 KB Output is correct
88 Correct 16 ms 24024 KB Output is correct
89 Correct 103 ms 39752 KB Output is correct
90 Correct 126 ms 41228 KB Output is correct
91 Correct 98 ms 42636 KB Output is correct
92 Correct 105 ms 39632 KB Output is correct
93 Correct 15 ms 24524 KB Output is correct
94 Correct 14 ms 24012 KB Output is correct
95 Correct 89 ms 42288 KB Output is correct
96 Correct 125 ms 46644 KB Output is correct
97 Correct 17 ms 24396 KB Output is correct
98 Correct 14 ms 23928 KB Output is correct
99 Correct 99 ms 40836 KB Output is correct
100 Correct 16 ms 24424 KB Output is correct
101 Correct 108 ms 40136 KB Output is correct
102 Correct 129 ms 45124 KB Output is correct
103 Correct 15 ms 24544 KB Output is correct
104 Correct 16 ms 24568 KB Output is correct
105 Correct 17 ms 24564 KB Output is correct
106 Correct 16 ms 24396 KB Output is correct
107 Correct 15 ms 24444 KB Output is correct
108 Correct 674 ms 108560 KB Output is correct
109 Correct 854 ms 120620 KB Output is correct
110 Correct 901 ms 131704 KB Output is correct
111 Correct 815 ms 141372 KB Output is correct
112 Correct 802 ms 131596 KB Output is correct
113 Correct 544 ms 116448 KB Output is correct
114 Correct 862 ms 102696 KB Output is correct
115 Correct 911 ms 102612 KB Output is correct
116 Correct 1029 ms 107156 KB Output is correct
117 Correct 712 ms 118380 KB Output is correct
118 Correct 812 ms 103056 KB Output is correct
119 Correct 728 ms 118376 KB Output is correct
120 Correct 770 ms 132344 KB Output is correct
121 Correct 722 ms 118536 KB Output is correct
122 Correct 985 ms 112056 KB Output is correct
123 Correct 482 ms 116084 KB Output is correct
124 Correct 796 ms 116580 KB Output is correct