Submission #107427

# Submission time Handle Problem Language Result Execution time Memory
107427 2019-04-24T07:39:53 Z faceless Mergers (JOI19_mergers) C++14
100 / 100
1722 ms 136952 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 5e5 + 10;
const int maxl = 21;
int cnt, par[maxn][maxl + 2], h[maxn], s[maxn], sz[maxn], psz[maxn], dp[maxn];
vector<int> t[maxn], vec[maxn];
bool pd[maxn];

void DFS(int v){
	sz[v] = 1;
	for (auto u : t[v]){
		if (u != par[v][0]){
			DFS(u);
			sz[v] += sz[u];
			psz[v] += psz[u];
			dp[v] += dp[u];
		}
	}
	if (dp[v] == 0 and sz[v] == psz[v] and par[v][0] != -1){
		cnt ++;
		dp[v] = 1;
		pd[v] = 1;
	}
}

int lca(int v, int u){
	if (h[v] < h[u])
		swap(v, u);
//	cout << "LCA " << v << " " << u << " -> ";
	for (int i = maxl - 1; i >= 0; i--)
		if (h[v] - (1 << i) >= h[u])
			v = par[v][i];
//	cout << v << endl;
	if (v == u)
		return v;
	for (int i = maxl - 1; i >= 0; i--){
		if (par[v][i] != par[u][i]){
			v = par[v][i];
			u = par[u][i];
		}
	}
	return par[v][0];
}

void dfs(int v, int p = -1){
	par[v][0] = p;
	for (int i = 1; i < maxl and par[v][i - 1] != -1; i++)
		par[v][i] = par[par[v][i - 1]][i - 1];
	for (auto u : t[v]){
		if (u != p){
			h[u] = h[v] + 1;
			dfs(u, v);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n - 1; i++){
		int v, u;
		cin >> v >> u;
		t[v].push_back(u);
		t[u].push_back(v);
	}
	int nonleaf = -1;
	for (int v = 1; v <= n; v++)
		if (t[v].size() > 1)
			nonleaf = v;
	memset(par, -1, sizeof par);
	dfs(nonleaf);

	for (int v = 1; v <= n; v++){
		cin >> s[v];
		vec[s[v]].push_back(v);
	}
	for (int i = 1; i <= k; i++){
		int v = vec[i][0];
		for (auto u : vec[i])
			v = lca(v, u);
		psz[v] += vec[i].size();
	}
	DFS(nonleaf);
	bool flag = 0;
	for (int v = 1; v <= n; v++)
		if (par[v][0] != -1 and pd[v] == 0 and dp[v] == cnt and psz[v] == sz[v])
			flag = 1;
	if (cnt & 1)
		cout << (cnt + 1) / 2 << endl;
	else
		cout << cnt / 2 + flag << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 68856 KB Output is correct
2 Correct 65 ms 68856 KB Output is correct
3 Correct 62 ms 68856 KB Output is correct
4 Correct 67 ms 68856 KB Output is correct
5 Correct 61 ms 68884 KB Output is correct
6 Correct 57 ms 68856 KB Output is correct
7 Correct 59 ms 68856 KB Output is correct
8 Correct 58 ms 68844 KB Output is correct
9 Correct 57 ms 68856 KB Output is correct
10 Correct 67 ms 68860 KB Output is correct
11 Correct 61 ms 68856 KB Output is correct
12 Correct 59 ms 68860 KB Output is correct
13 Correct 66 ms 68984 KB Output is correct
14 Correct 58 ms 68940 KB Output is correct
15 Correct 69 ms 68856 KB Output is correct
16 Correct 57 ms 68984 KB Output is correct
17 Correct 60 ms 68856 KB Output is correct
18 Correct 59 ms 68984 KB Output is correct
19 Correct 62 ms 68856 KB Output is correct
20 Correct 61 ms 68856 KB Output is correct
21 Correct 67 ms 68984 KB Output is correct
22 Correct 65 ms 68888 KB Output is correct
23 Correct 59 ms 68856 KB Output is correct
24 Correct 66 ms 68828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 68856 KB Output is correct
2 Correct 65 ms 68856 KB Output is correct
3 Correct 62 ms 68856 KB Output is correct
4 Correct 67 ms 68856 KB Output is correct
5 Correct 61 ms 68884 KB Output is correct
6 Correct 57 ms 68856 KB Output is correct
7 Correct 59 ms 68856 KB Output is correct
8 Correct 58 ms 68844 KB Output is correct
9 Correct 57 ms 68856 KB Output is correct
10 Correct 67 ms 68860 KB Output is correct
11 Correct 61 ms 68856 KB Output is correct
12 Correct 59 ms 68860 KB Output is correct
13 Correct 66 ms 68984 KB Output is correct
14 Correct 58 ms 68940 KB Output is correct
15 Correct 69 ms 68856 KB Output is correct
16 Correct 57 ms 68984 KB Output is correct
17 Correct 60 ms 68856 KB Output is correct
18 Correct 59 ms 68984 KB Output is correct
19 Correct 62 ms 68856 KB Output is correct
20 Correct 61 ms 68856 KB Output is correct
21 Correct 67 ms 68984 KB Output is correct
22 Correct 65 ms 68888 KB Output is correct
23 Correct 59 ms 68856 KB Output is correct
24 Correct 66 ms 68828 KB Output is correct
25 Correct 67 ms 68900 KB Output is correct
26 Correct 63 ms 69056 KB Output is correct
27 Correct 60 ms 69112 KB Output is correct
28 Correct 63 ms 69268 KB Output is correct
29 Correct 69 ms 69240 KB Output is correct
30 Correct 72 ms 69060 KB Output is correct
31 Correct 65 ms 68856 KB Output is correct
32 Correct 67 ms 69388 KB Output is correct
33 Correct 70 ms 68856 KB Output is correct
34 Correct 67 ms 69112 KB Output is correct
35 Correct 70 ms 69240 KB Output is correct
36 Correct 77 ms 69112 KB Output is correct
37 Correct 75 ms 69112 KB Output is correct
38 Correct 72 ms 68828 KB Output is correct
39 Correct 69 ms 69160 KB Output is correct
40 Correct 76 ms 69112 KB Output is correct
41 Correct 69 ms 69140 KB Output is correct
42 Correct 65 ms 69056 KB Output is correct
43 Correct 74 ms 69264 KB Output is correct
44 Correct 64 ms 68856 KB Output is correct
45 Correct 72 ms 69112 KB Output is correct
46 Correct 69 ms 69112 KB Output is correct
47 Correct 66 ms 68856 KB Output is correct
48 Correct 74 ms 69112 KB Output is correct
49 Correct 71 ms 69240 KB Output is correct
50 Correct 73 ms 69240 KB Output is correct
51 Correct 68 ms 69116 KB Output is correct
52 Correct 67 ms 69116 KB Output is correct
53 Correct 68 ms 69108 KB Output is correct
54 Correct 76 ms 69116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 68856 KB Output is correct
2 Correct 65 ms 68856 KB Output is correct
3 Correct 62 ms 68856 KB Output is correct
4 Correct 67 ms 68856 KB Output is correct
5 Correct 61 ms 68884 KB Output is correct
6 Correct 57 ms 68856 KB Output is correct
7 Correct 59 ms 68856 KB Output is correct
8 Correct 58 ms 68844 KB Output is correct
9 Correct 57 ms 68856 KB Output is correct
10 Correct 67 ms 68860 KB Output is correct
11 Correct 61 ms 68856 KB Output is correct
12 Correct 59 ms 68860 KB Output is correct
13 Correct 66 ms 68984 KB Output is correct
14 Correct 58 ms 68940 KB Output is correct
15 Correct 69 ms 68856 KB Output is correct
16 Correct 57 ms 68984 KB Output is correct
17 Correct 60 ms 68856 KB Output is correct
18 Correct 59 ms 68984 KB Output is correct
19 Correct 62 ms 68856 KB Output is correct
20 Correct 61 ms 68856 KB Output is correct
21 Correct 67 ms 68984 KB Output is correct
22 Correct 65 ms 68888 KB Output is correct
23 Correct 59 ms 68856 KB Output is correct
24 Correct 66 ms 68828 KB Output is correct
25 Correct 64 ms 68856 KB Output is correct
26 Correct 188 ms 74352 KB Output is correct
27 Correct 218 ms 74668 KB Output is correct
28 Correct 64 ms 69116 KB Output is correct
29 Correct 63 ms 68856 KB Output is correct
30 Correct 65 ms 68856 KB Output is correct
31 Correct 204 ms 74744 KB Output is correct
32 Correct 68 ms 69084 KB Output is correct
33 Correct 272 ms 78712 KB Output is correct
34 Correct 217 ms 76232 KB Output is correct
35 Correct 65 ms 69112 KB Output is correct
36 Correct 230 ms 76252 KB Output is correct
37 Correct 74 ms 69112 KB Output is correct
38 Correct 64 ms 69112 KB Output is correct
39 Correct 194 ms 75628 KB Output is correct
40 Correct 68 ms 69112 KB Output is correct
41 Correct 196 ms 75900 KB Output is correct
42 Correct 256 ms 77688 KB Output is correct
43 Correct 62 ms 68856 KB Output is correct
44 Correct 263 ms 78292 KB Output is correct
45 Correct 277 ms 78300 KB Output is correct
46 Correct 63 ms 69112 KB Output is correct
47 Correct 73 ms 69112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 74096 KB Output is correct
2 Correct 204 ms 77752 KB Output is correct
3 Correct 65 ms 69112 KB Output is correct
4 Correct 72 ms 69088 KB Output is correct
5 Correct 60 ms 68856 KB Output is correct
6 Correct 60 ms 68856 KB Output is correct
7 Correct 65 ms 69112 KB Output is correct
8 Correct 299 ms 76024 KB Output is correct
9 Correct 66 ms 69112 KB Output is correct
10 Correct 207 ms 76280 KB Output is correct
11 Correct 65 ms 68984 KB Output is correct
12 Correct 224 ms 76208 KB Output is correct
13 Correct 243 ms 77320 KB Output is correct
14 Correct 220 ms 79136 KB Output is correct
15 Correct 154 ms 75632 KB Output is correct
16 Correct 64 ms 69112 KB Output is correct
17 Correct 58 ms 68984 KB Output is correct
18 Correct 170 ms 79088 KB Output is correct
19 Correct 293 ms 81400 KB Output is correct
20 Correct 64 ms 69116 KB Output is correct
21 Correct 62 ms 68808 KB Output is correct
22 Correct 180 ms 77680 KB Output is correct
23 Correct 65 ms 69112 KB Output is correct
24 Correct 295 ms 76792 KB Output is correct
25 Correct 271 ms 80844 KB Output is correct
26 Correct 65 ms 69240 KB Output is correct
27 Correct 62 ms 69300 KB Output is correct
28 Correct 67 ms 69240 KB Output is correct
29 Correct 70 ms 69112 KB Output is correct
30 Correct 71 ms 69112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 68856 KB Output is correct
2 Correct 65 ms 68856 KB Output is correct
3 Correct 62 ms 68856 KB Output is correct
4 Correct 67 ms 68856 KB Output is correct
5 Correct 61 ms 68884 KB Output is correct
6 Correct 57 ms 68856 KB Output is correct
7 Correct 59 ms 68856 KB Output is correct
8 Correct 58 ms 68844 KB Output is correct
9 Correct 57 ms 68856 KB Output is correct
10 Correct 67 ms 68860 KB Output is correct
11 Correct 61 ms 68856 KB Output is correct
12 Correct 59 ms 68860 KB Output is correct
13 Correct 66 ms 68984 KB Output is correct
14 Correct 58 ms 68940 KB Output is correct
15 Correct 69 ms 68856 KB Output is correct
16 Correct 57 ms 68984 KB Output is correct
17 Correct 60 ms 68856 KB Output is correct
18 Correct 59 ms 68984 KB Output is correct
19 Correct 62 ms 68856 KB Output is correct
20 Correct 61 ms 68856 KB Output is correct
21 Correct 67 ms 68984 KB Output is correct
22 Correct 65 ms 68888 KB Output is correct
23 Correct 59 ms 68856 KB Output is correct
24 Correct 66 ms 68828 KB Output is correct
25 Correct 67 ms 68900 KB Output is correct
26 Correct 63 ms 69056 KB Output is correct
27 Correct 60 ms 69112 KB Output is correct
28 Correct 63 ms 69268 KB Output is correct
29 Correct 69 ms 69240 KB Output is correct
30 Correct 72 ms 69060 KB Output is correct
31 Correct 65 ms 68856 KB Output is correct
32 Correct 67 ms 69388 KB Output is correct
33 Correct 70 ms 68856 KB Output is correct
34 Correct 67 ms 69112 KB Output is correct
35 Correct 70 ms 69240 KB Output is correct
36 Correct 77 ms 69112 KB Output is correct
37 Correct 75 ms 69112 KB Output is correct
38 Correct 72 ms 68828 KB Output is correct
39 Correct 69 ms 69160 KB Output is correct
40 Correct 76 ms 69112 KB Output is correct
41 Correct 69 ms 69140 KB Output is correct
42 Correct 65 ms 69056 KB Output is correct
43 Correct 74 ms 69264 KB Output is correct
44 Correct 64 ms 68856 KB Output is correct
45 Correct 72 ms 69112 KB Output is correct
46 Correct 69 ms 69112 KB Output is correct
47 Correct 66 ms 68856 KB Output is correct
48 Correct 74 ms 69112 KB Output is correct
49 Correct 71 ms 69240 KB Output is correct
50 Correct 73 ms 69240 KB Output is correct
51 Correct 68 ms 69116 KB Output is correct
52 Correct 67 ms 69116 KB Output is correct
53 Correct 68 ms 69108 KB Output is correct
54 Correct 76 ms 69116 KB Output is correct
55 Correct 64 ms 68856 KB Output is correct
56 Correct 188 ms 74352 KB Output is correct
57 Correct 218 ms 74668 KB Output is correct
58 Correct 64 ms 69116 KB Output is correct
59 Correct 63 ms 68856 KB Output is correct
60 Correct 65 ms 68856 KB Output is correct
61 Correct 204 ms 74744 KB Output is correct
62 Correct 68 ms 69084 KB Output is correct
63 Correct 272 ms 78712 KB Output is correct
64 Correct 217 ms 76232 KB Output is correct
65 Correct 65 ms 69112 KB Output is correct
66 Correct 230 ms 76252 KB Output is correct
67 Correct 74 ms 69112 KB Output is correct
68 Correct 64 ms 69112 KB Output is correct
69 Correct 194 ms 75628 KB Output is correct
70 Correct 68 ms 69112 KB Output is correct
71 Correct 196 ms 75900 KB Output is correct
72 Correct 256 ms 77688 KB Output is correct
73 Correct 62 ms 68856 KB Output is correct
74 Correct 263 ms 78292 KB Output is correct
75 Correct 277 ms 78300 KB Output is correct
76 Correct 63 ms 69112 KB Output is correct
77 Correct 73 ms 69112 KB Output is correct
78 Correct 141 ms 74096 KB Output is correct
79 Correct 204 ms 77752 KB Output is correct
80 Correct 65 ms 69112 KB Output is correct
81 Correct 72 ms 69088 KB Output is correct
82 Correct 60 ms 68856 KB Output is correct
83 Correct 60 ms 68856 KB Output is correct
84 Correct 65 ms 69112 KB Output is correct
85 Correct 299 ms 76024 KB Output is correct
86 Correct 66 ms 69112 KB Output is correct
87 Correct 207 ms 76280 KB Output is correct
88 Correct 65 ms 68984 KB Output is correct
89 Correct 224 ms 76208 KB Output is correct
90 Correct 243 ms 77320 KB Output is correct
91 Correct 220 ms 79136 KB Output is correct
92 Correct 154 ms 75632 KB Output is correct
93 Correct 64 ms 69112 KB Output is correct
94 Correct 58 ms 68984 KB Output is correct
95 Correct 170 ms 79088 KB Output is correct
96 Correct 293 ms 81400 KB Output is correct
97 Correct 64 ms 69116 KB Output is correct
98 Correct 62 ms 68808 KB Output is correct
99 Correct 180 ms 77680 KB Output is correct
100 Correct 65 ms 69112 KB Output is correct
101 Correct 295 ms 76792 KB Output is correct
102 Correct 271 ms 80844 KB Output is correct
103 Correct 65 ms 69240 KB Output is correct
104 Correct 62 ms 69300 KB Output is correct
105 Correct 67 ms 69240 KB Output is correct
106 Correct 70 ms 69112 KB Output is correct
107 Correct 71 ms 69112 KB Output is correct
108 Correct 893 ms 113064 KB Output is correct
109 Correct 1614 ms 112716 KB Output is correct
110 Correct 1722 ms 121304 KB Output is correct
111 Correct 1565 ms 136952 KB Output is correct
112 Correct 1498 ms 131300 KB Output is correct
113 Correct 884 ms 120884 KB Output is correct
114 Correct 944 ms 104452 KB Output is correct
115 Correct 894 ms 104604 KB Output is correct
116 Correct 1248 ms 109668 KB Output is correct
117 Correct 1222 ms 120916 KB Output is correct
118 Correct 1022 ms 106256 KB Output is correct
119 Correct 1331 ms 120968 KB Output is correct
120 Correct 1688 ms 131284 KB Output is correct
121 Correct 1194 ms 120968 KB Output is correct
122 Correct 1216 ms 114632 KB Output is correct
123 Correct 848 ms 122448 KB Output is correct
124 Correct 1263 ms 119532 KB Output is correct