Submission #105105

# Submission time Handle Problem Language Result Execution time Memory
105105 2019-04-10T14:43:00 Z antimirage Mergers (JOI19_mergers) C++14
100 / 100
1806 ms 136192 KB
#include <bits/stdc++.h>
 
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
 
using namespace std;
 
const int N = 5e5 + 5;
 
int n, k, x, y, tin[N], tout[N], timer, up[N][21], pref[N], par[N], ans, m[N], sz[N], deg[N];
 
vector < vector <int> > g, vec;
 
void dfs (int v, int p = 0)
{
	par[v] = p;
	tin[v] = ++timer;
	
	up[v][0] = p;
	for (int i = 1; i < 20; i++)
		up[v][i] = up[ up[v][i - 1] ][i - 1];
		
	for (auto to : g[v]){
		if (to == p) continue;
		dfs(to, v);
	}
	tout[v] = ++timer;
}
bool upper (int a, int b)
{
	return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca (int a, int b)
{
	if (upper(a, b) ) return a;
	if (upper(b, a) ) return b;
		
	for (int i = 19; i >= 0; i--){
		if (up[a][i] && !upper(up[a][i], b) )
			a = up[a][i];
	}
	return up[a][0];
}
int get (int v){
	return v == m[v] ? v : m[v] = get(m[v]);
}
void unite (int a, int b)
{
	a = get(a);
	b = get(b);
	if (a != b){
		if (sz[a] > sz[b]) swap(a, b);
		
		sz[b] += sz[a];
		m[a] = b;
		deg[b] += deg[a];
	}
}
void Dfs (int v, int p = 0)
{
	for (auto to : g[v]){
		
		if (to == p) continue;
		Dfs(to, v);
		
		pref[v] += pref[to];
	}
	if (pref[v] > 0 && p){
		unite(v, p);
	}
	else if (p) {
		++deg[get(p)];
		++deg[get(v)];
	}
}
main(){
	
	cin >> n >> k;
	g.resize(n + 1);
	vec.resize(k + 1);
	
	for (int i = 1; i < n; i++){
		
		scanf("%d%d", &x, &y);
		g[x].pb(y);
		g[y].pb(x);
	}
	dfs(1);	
	
	for (int i = 1; i <= n; i++){
		scanf("%d", &x);
		vec[x].pb(i);
		
		m[i] = i;
		sz[i] = 1;
	}
	
	for (int i = 1; i <= k; i++){
		if (vec[i].empty() ) continue;
		
		int LCA = vec[i][0];
		
		for (int j = 1; j < (int)vec[i].size(); j++){
			LCA = lca(LCA, vec[i][j]);
		}
		for (int j = 0; j < (int)vec[i].size(); j++){
			pref[ vec[i][j] ]++;
			pref[ LCA ]--;
		}
	}
	
	Dfs(1);
	
	for (int i = 1; i <= n; i++){
		if (m[i] == i && deg[i] == 1)
			ans++;
	}
	if (sz[ get(1) ] == n){
		puts("0");
	}
	else{
		cout << (ans + 1) / 2 << endl;
	}
}
/**
5 4
1 2
2 3
2 4
2 5
4
1
2
3
4
5
2


**/

Compilation message

mergers.cpp:79:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
mergers.cpp: In function 'int main()':
mergers.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:94:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 6 ms 1024 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 8 ms 1152 KB Output is correct
29 Correct 4 ms 1024 KB Output is correct
30 Correct 5 ms 896 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 7 ms 1152 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 5 ms 896 KB Output is correct
35 Correct 5 ms 1152 KB Output is correct
36 Correct 7 ms 1024 KB Output is correct
37 Correct 5 ms 1024 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 7 ms 1024 KB Output is correct
40 Correct 6 ms 896 KB Output is correct
41 Correct 6 ms 896 KB Output is correct
42 Correct 6 ms 1024 KB Output is correct
43 Correct 5 ms 1152 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 0 ms 1024 KB Output is correct
46 Correct 9 ms 904 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 7 ms 1024 KB Output is correct
49 Correct 6 ms 1152 KB Output is correct
50 Correct 5 ms 1152 KB Output is correct
51 Correct 9 ms 1024 KB Output is correct
52 Correct 6 ms 1024 KB Output is correct
53 Correct 6 ms 1024 KB Output is correct
54 Correct 6 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 152 ms 18788 KB Output is correct
27 Correct 179 ms 18936 KB Output is correct
28 Correct 5 ms 896 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 159 ms 18808 KB Output is correct
32 Correct 5 ms 1024 KB Output is correct
33 Correct 238 ms 24164 KB Output is correct
34 Correct 195 ms 19240 KB Output is correct
35 Correct 5 ms 896 KB Output is correct
36 Correct 200 ms 19168 KB Output is correct
37 Correct 7 ms 896 KB Output is correct
38 Correct 7 ms 768 KB Output is correct
39 Correct 128 ms 18672 KB Output is correct
40 Correct 7 ms 1152 KB Output is correct
41 Correct 155 ms 18804 KB Output is correct
42 Correct 220 ms 20364 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 216 ms 24396 KB Output is correct
45 Correct 225 ms 22060 KB Output is correct
46 Correct 7 ms 1024 KB Output is correct
47 Correct 5 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 17364 KB Output is correct
2 Correct 163 ms 24388 KB Output is correct
3 Correct 8 ms 1024 KB Output is correct
4 Correct 7 ms 896 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 5 ms 896 KB Output is correct
8 Correct 212 ms 21340 KB Output is correct
9 Correct 5 ms 896 KB Output is correct
10 Correct 174 ms 19064 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 236 ms 19064 KB Output is correct
13 Correct 194 ms 21468 KB Output is correct
14 Correct 228 ms 24184 KB Output is correct
15 Correct 138 ms 18672 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 172 ms 23880 KB Output is correct
19 Correct 239 ms 27228 KB Output is correct
20 Correct 5 ms 1024 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 166 ms 21716 KB Output is correct
23 Correct 6 ms 896 KB Output is correct
24 Correct 188 ms 19700 KB Output is correct
25 Correct 219 ms 26232 KB Output is correct
26 Correct 6 ms 1152 KB Output is correct
27 Correct 6 ms 1152 KB Output is correct
28 Correct 6 ms 896 KB Output is correct
29 Correct 4 ms 896 KB Output is correct
30 Correct 5 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 6 ms 1024 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 8 ms 1152 KB Output is correct
29 Correct 4 ms 1024 KB Output is correct
30 Correct 5 ms 896 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 7 ms 1152 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 5 ms 896 KB Output is correct
35 Correct 5 ms 1152 KB Output is correct
36 Correct 7 ms 1024 KB Output is correct
37 Correct 5 ms 1024 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 7 ms 1024 KB Output is correct
40 Correct 6 ms 896 KB Output is correct
41 Correct 6 ms 896 KB Output is correct
42 Correct 6 ms 1024 KB Output is correct
43 Correct 5 ms 1152 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 0 ms 1024 KB Output is correct
46 Correct 9 ms 904 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 7 ms 1024 KB Output is correct
49 Correct 6 ms 1152 KB Output is correct
50 Correct 5 ms 1152 KB Output is correct
51 Correct 9 ms 1024 KB Output is correct
52 Correct 6 ms 1024 KB Output is correct
53 Correct 6 ms 1024 KB Output is correct
54 Correct 6 ms 896 KB Output is correct
55 Correct 2 ms 384 KB Output is correct
56 Correct 152 ms 18788 KB Output is correct
57 Correct 179 ms 18936 KB Output is correct
58 Correct 5 ms 896 KB Output is correct
59 Correct 3 ms 384 KB Output is correct
60 Correct 2 ms 384 KB Output is correct
61 Correct 159 ms 18808 KB Output is correct
62 Correct 5 ms 1024 KB Output is correct
63 Correct 238 ms 24164 KB Output is correct
64 Correct 195 ms 19240 KB Output is correct
65 Correct 5 ms 896 KB Output is correct
66 Correct 200 ms 19168 KB Output is correct
67 Correct 7 ms 896 KB Output is correct
68 Correct 7 ms 768 KB Output is correct
69 Correct 128 ms 18672 KB Output is correct
70 Correct 7 ms 1152 KB Output is correct
71 Correct 155 ms 18804 KB Output is correct
72 Correct 220 ms 20364 KB Output is correct
73 Correct 2 ms 384 KB Output is correct
74 Correct 216 ms 24396 KB Output is correct
75 Correct 225 ms 22060 KB Output is correct
76 Correct 7 ms 1024 KB Output is correct
77 Correct 5 ms 896 KB Output is correct
78 Correct 129 ms 17364 KB Output is correct
79 Correct 163 ms 24388 KB Output is correct
80 Correct 8 ms 1024 KB Output is correct
81 Correct 7 ms 896 KB Output is correct
82 Correct 3 ms 384 KB Output is correct
83 Correct 3 ms 384 KB Output is correct
84 Correct 5 ms 896 KB Output is correct
85 Correct 212 ms 21340 KB Output is correct
86 Correct 5 ms 896 KB Output is correct
87 Correct 174 ms 19064 KB Output is correct
88 Correct 2 ms 384 KB Output is correct
89 Correct 236 ms 19064 KB Output is correct
90 Correct 194 ms 21468 KB Output is correct
91 Correct 228 ms 24184 KB Output is correct
92 Correct 138 ms 18672 KB Output is correct
93 Correct 5 ms 1024 KB Output is correct
94 Correct 3 ms 384 KB Output is correct
95 Correct 172 ms 23880 KB Output is correct
96 Correct 239 ms 27228 KB Output is correct
97 Correct 5 ms 1024 KB Output is correct
98 Correct 3 ms 384 KB Output is correct
99 Correct 166 ms 21716 KB Output is correct
100 Correct 6 ms 896 KB Output is correct
101 Correct 188 ms 19700 KB Output is correct
102 Correct 219 ms 26232 KB Output is correct
103 Correct 6 ms 1152 KB Output is correct
104 Correct 6 ms 1152 KB Output is correct
105 Correct 6 ms 896 KB Output is correct
106 Correct 4 ms 896 KB Output is correct
107 Correct 5 ms 1024 KB Output is correct
108 Correct 1075 ms 103036 KB Output is correct
109 Correct 1493 ms 105200 KB Output is correct
110 Correct 1466 ms 114140 KB Output is correct
111 Correct 1806 ms 136192 KB Output is correct
112 Correct 1790 ms 128060 KB Output is correct
113 Correct 1129 ms 116212 KB Output is correct
114 Correct 1080 ms 93028 KB Output is correct
115 Correct 1059 ms 92880 KB Output is correct
116 Correct 1354 ms 97444 KB Output is correct
117 Correct 1473 ms 117648 KB Output is correct
118 Correct 1137 ms 92284 KB Output is correct
119 Correct 1483 ms 117660 KB Output is correct
120 Correct 1743 ms 128692 KB Output is correct
121 Correct 1537 ms 117720 KB Output is correct
122 Correct 1461 ms 106944 KB Output is correct
123 Correct 1141 ms 119216 KB Output is correct
124 Correct 1549 ms 115352 KB Output is correct