Submission #232120

# Submission time Handle Problem Language Result Execution time Memory
232120 2020-05-16T06:02:51 Z oolimry Mergers (JOI19_mergers) C++14
100 / 100
870 ms 80972 KB
#include <bits/stdc++.h>
using namespace std;

int n, k; 
vector<int> adj[500005];
int sz[500005];
int heavy[500005];
int depth[500005];
int head[500005];
int p[500005];
int pos[500005];

void dfs(int u){
	sz[u] = 1;
	int maxChild = 0;
	for(int v : adj[u]){
		if(sz[v] != 0) continue;
		depth[v] = depth[u] + 1;
		dfs(v);
		sz[u] += sz[v];
		p[v] = u;
		if(sz[v] > maxChild){
			heavy[u] = v;
			maxChild = sz[v];
		}
	}
}

int cnt = 0;
void decompose(int u, int h){
	head[u] = h;
	pos[u] = cnt; cnt++;
	if(heavy[u] != 0) decompose(heavy[u], h);
	for(int v : adj[u]){
		if(sz[v] < sz[u] && v != heavy[u]){
			decompose(v,v);
		}
	}
}

int preState[500005];

int prefix[500005];

void update(int l, int r){
	prefix[l]++;
	prefix[r+1]--;
}

struct UFDS{
	int p[500005];
	void init() { for(int i = 0;i <= n;i++) p[i] = i; }
	
	int findSet(int u){
		if(p[u] == u) return u;
		else return p[u] = findSet(p[u]);
	}
	
	void unionSet(int u, int v){
		u = findSet(u); v = findSet(v);
		if(u == v) return;
		
		if((u^v)&1) swap(u,v);
		p[u] = v;
	}
} uf;

int compressAdj[500005];

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> k;
	for(int i = 0;i < n-1;i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	
	int root = -1;
	for(int i = 1;i <= n;i++){
		if(adj[i].size() != 1){
			root = i;
			break;
		}
	}
	
	if(root == -1){ ///n==2 or n==1
		cout << k-1; return 0;
	}
	
	dfs(root);
	decompose(root, root);
	
	//for(int i = 1;i <= n;i++) cout << pos[i] << " " << head[i] << "\n";
	
	for(int i = 1;i <= n;i++){
		int state; cin >> state;
		if(preState[state] != 0){
			int a = i, b = preState[state];
			if(depth[a] > depth[b]) swap(a,b);
			for(;head[a] != head[b];b = p[head[b]]){
				if(depth[head[a]] > depth[head[b]]) swap(a,b);
				update(pos[head[b]], pos[b]);
			}
			if(depth[a] > depth[b]) swap(a,b);
			update(pos[a]+1,pos[b]);
		}
		preState[state] = i;
	}
	
	for(int i = 1;i <= n;i++) prefix[i] += prefix[i-1];
	
	uf.init();
	
	for(int i = 1;i <= n;i++){
		if(i == root) continue;
		if(prefix[pos[i]] == 0) continue;
		
		uf.unionSet(i, p[i]);
	}
	
	for(int i = 1;i <= n;i++){
		if(i == root) continue;
		if(prefix[pos[i]] != 0) continue;
		
		compressAdj[uf.findSet(i)]++;
		compressAdj[uf.findSet(p[i])]++;
	}
	
	int leaves = 0;
	for(int i = 1;i <= n;i++){
		if(compressAdj[i] == 1) leaves++;
	}
	
	cout << (leaves+1)/2;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 11 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 11 ms 12160 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 11 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 12 ms 12160 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 11 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 11 ms 12160 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 11 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 12 ms 12160 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 12 ms 12160 KB Output is correct
26 Correct 14 ms 12416 KB Output is correct
27 Correct 13 ms 12416 KB Output is correct
28 Correct 13 ms 12544 KB Output is correct
29 Correct 13 ms 12416 KB Output is correct
30 Correct 13 ms 12416 KB Output is correct
31 Correct 11 ms 12160 KB Output is correct
32 Correct 17 ms 12544 KB Output is correct
33 Correct 12 ms 12160 KB Output is correct
34 Correct 12 ms 12416 KB Output is correct
35 Correct 13 ms 12544 KB Output is correct
36 Correct 13 ms 12416 KB Output is correct
37 Correct 13 ms 12416 KB Output is correct
38 Correct 12 ms 12160 KB Output is correct
39 Correct 14 ms 12416 KB Output is correct
40 Correct 13 ms 12416 KB Output is correct
41 Correct 13 ms 12416 KB Output is correct
42 Correct 13 ms 12336 KB Output is correct
43 Correct 13 ms 12544 KB Output is correct
44 Correct 11 ms 12160 KB Output is correct
45 Correct 13 ms 12416 KB Output is correct
46 Correct 14 ms 12416 KB Output is correct
47 Correct 12 ms 12160 KB Output is correct
48 Correct 13 ms 12416 KB Output is correct
49 Correct 13 ms 12416 KB Output is correct
50 Correct 13 ms 12416 KB Output is correct
51 Correct 16 ms 12416 KB Output is correct
52 Correct 13 ms 12416 KB Output is correct
53 Correct 13 ms 12416 KB Output is correct
54 Correct 13 ms 12416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 11 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 11 ms 12160 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 11 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 12 ms 12160 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 11 ms 12160 KB Output is correct
26 Correct 61 ms 19824 KB Output is correct
27 Correct 98 ms 19960 KB Output is correct
28 Correct 13 ms 12416 KB Output is correct
29 Correct 12 ms 12160 KB Output is correct
30 Correct 12 ms 12288 KB Output is correct
31 Correct 104 ms 20168 KB Output is correct
32 Correct 13 ms 12416 KB Output is correct
33 Correct 98 ms 27128 KB Output is correct
34 Correct 105 ms 19960 KB Output is correct
35 Correct 14 ms 12416 KB Output is correct
36 Correct 92 ms 20856 KB Output is correct
37 Correct 13 ms 12416 KB Output is correct
38 Correct 13 ms 12416 KB Output is correct
39 Correct 60 ms 19824 KB Output is correct
40 Correct 13 ms 12544 KB Output is correct
41 Correct 126 ms 19960 KB Output is correct
42 Correct 103 ms 22264 KB Output is correct
43 Correct 11 ms 12160 KB Output is correct
44 Correct 96 ms 27384 KB Output is correct
45 Correct 101 ms 23672 KB Output is correct
46 Correct 13 ms 12416 KB Output is correct
47 Correct 13 ms 12416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 19820 KB Output is correct
2 Correct 69 ms 20976 KB Output is correct
3 Correct 14 ms 12544 KB Output is correct
4 Correct 13 ms 12416 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 13 ms 12416 KB Output is correct
8 Correct 102 ms 20856 KB Output is correct
9 Correct 13 ms 12416 KB Output is correct
10 Correct 98 ms 20472 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 97 ms 20472 KB Output is correct
13 Correct 94 ms 20984 KB Output is correct
14 Correct 84 ms 20984 KB Output is correct
15 Correct 71 ms 19824 KB Output is correct
16 Correct 13 ms 12416 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 70 ms 20976 KB Output is correct
19 Correct 96 ms 24984 KB Output is correct
20 Correct 13 ms 12416 KB Output is correct
21 Correct 11 ms 12160 KB Output is correct
22 Correct 67 ms 20720 KB Output is correct
23 Correct 13 ms 12416 KB Output is correct
24 Correct 122 ms 20472 KB Output is correct
25 Correct 101 ms 23672 KB Output is correct
26 Correct 13 ms 12416 KB Output is correct
27 Correct 13 ms 12416 KB Output is correct
28 Correct 13 ms 12416 KB Output is correct
29 Correct 13 ms 12416 KB Output is correct
30 Correct 13 ms 12424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12160 KB Output is correct
2 Correct 12 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 12 ms 12160 KB Output is correct
5 Correct 12 ms 12160 KB Output is correct
6 Correct 12 ms 12160 KB Output is correct
7 Correct 11 ms 12160 KB Output is correct
8 Correct 12 ms 12160 KB Output is correct
9 Correct 11 ms 12160 KB Output is correct
10 Correct 12 ms 12160 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 12 ms 12160 KB Output is correct
13 Correct 12 ms 12160 KB Output is correct
14 Correct 12 ms 12160 KB Output is correct
15 Correct 12 ms 12160 KB Output is correct
16 Correct 11 ms 12160 KB Output is correct
17 Correct 12 ms 12160 KB Output is correct
18 Correct 12 ms 12160 KB Output is correct
19 Correct 11 ms 12160 KB Output is correct
20 Correct 12 ms 12160 KB Output is correct
21 Correct 12 ms 12160 KB Output is correct
22 Correct 11 ms 12160 KB Output is correct
23 Correct 12 ms 12160 KB Output is correct
24 Correct 12 ms 12160 KB Output is correct
25 Correct 12 ms 12160 KB Output is correct
26 Correct 14 ms 12416 KB Output is correct
27 Correct 13 ms 12416 KB Output is correct
28 Correct 13 ms 12544 KB Output is correct
29 Correct 13 ms 12416 KB Output is correct
30 Correct 13 ms 12416 KB Output is correct
31 Correct 11 ms 12160 KB Output is correct
32 Correct 17 ms 12544 KB Output is correct
33 Correct 12 ms 12160 KB Output is correct
34 Correct 12 ms 12416 KB Output is correct
35 Correct 13 ms 12544 KB Output is correct
36 Correct 13 ms 12416 KB Output is correct
37 Correct 13 ms 12416 KB Output is correct
38 Correct 12 ms 12160 KB Output is correct
39 Correct 14 ms 12416 KB Output is correct
40 Correct 13 ms 12416 KB Output is correct
41 Correct 13 ms 12416 KB Output is correct
42 Correct 13 ms 12336 KB Output is correct
43 Correct 13 ms 12544 KB Output is correct
44 Correct 11 ms 12160 KB Output is correct
45 Correct 13 ms 12416 KB Output is correct
46 Correct 14 ms 12416 KB Output is correct
47 Correct 12 ms 12160 KB Output is correct
48 Correct 13 ms 12416 KB Output is correct
49 Correct 13 ms 12416 KB Output is correct
50 Correct 13 ms 12416 KB Output is correct
51 Correct 16 ms 12416 KB Output is correct
52 Correct 13 ms 12416 KB Output is correct
53 Correct 13 ms 12416 KB Output is correct
54 Correct 13 ms 12416 KB Output is correct
55 Correct 11 ms 12160 KB Output is correct
56 Correct 61 ms 19824 KB Output is correct
57 Correct 98 ms 19960 KB Output is correct
58 Correct 13 ms 12416 KB Output is correct
59 Correct 12 ms 12160 KB Output is correct
60 Correct 12 ms 12288 KB Output is correct
61 Correct 104 ms 20168 KB Output is correct
62 Correct 13 ms 12416 KB Output is correct
63 Correct 98 ms 27128 KB Output is correct
64 Correct 105 ms 19960 KB Output is correct
65 Correct 14 ms 12416 KB Output is correct
66 Correct 92 ms 20856 KB Output is correct
67 Correct 13 ms 12416 KB Output is correct
68 Correct 13 ms 12416 KB Output is correct
69 Correct 60 ms 19824 KB Output is correct
70 Correct 13 ms 12544 KB Output is correct
71 Correct 126 ms 19960 KB Output is correct
72 Correct 103 ms 22264 KB Output is correct
73 Correct 11 ms 12160 KB Output is correct
74 Correct 96 ms 27384 KB Output is correct
75 Correct 101 ms 23672 KB Output is correct
76 Correct 13 ms 12416 KB Output is correct
77 Correct 13 ms 12416 KB Output is correct
78 Correct 64 ms 19820 KB Output is correct
79 Correct 69 ms 20976 KB Output is correct
80 Correct 14 ms 12544 KB Output is correct
81 Correct 13 ms 12416 KB Output is correct
82 Correct 12 ms 12160 KB Output is correct
83 Correct 12 ms 12160 KB Output is correct
84 Correct 13 ms 12416 KB Output is correct
85 Correct 102 ms 20856 KB Output is correct
86 Correct 13 ms 12416 KB Output is correct
87 Correct 98 ms 20472 KB Output is correct
88 Correct 11 ms 12160 KB Output is correct
89 Correct 97 ms 20472 KB Output is correct
90 Correct 94 ms 20984 KB Output is correct
91 Correct 84 ms 20984 KB Output is correct
92 Correct 71 ms 19824 KB Output is correct
93 Correct 13 ms 12416 KB Output is correct
94 Correct 12 ms 12160 KB Output is correct
95 Correct 70 ms 20976 KB Output is correct
96 Correct 96 ms 24984 KB Output is correct
97 Correct 13 ms 12416 KB Output is correct
98 Correct 11 ms 12160 KB Output is correct
99 Correct 67 ms 20720 KB Output is correct
100 Correct 13 ms 12416 KB Output is correct
101 Correct 122 ms 20472 KB Output is correct
102 Correct 101 ms 23672 KB Output is correct
103 Correct 13 ms 12416 KB Output is correct
104 Correct 13 ms 12416 KB Output is correct
105 Correct 13 ms 12416 KB Output is correct
106 Correct 13 ms 12416 KB Output is correct
107 Correct 13 ms 12424 KB Output is correct
108 Correct 446 ms 56036 KB Output is correct
109 Correct 663 ms 69668 KB Output is correct
110 Correct 681 ms 80760 KB Output is correct
111 Correct 691 ms 80972 KB Output is correct
112 Correct 691 ms 71112 KB Output is correct
113 Correct 416 ms 57180 KB Output is correct
114 Correct 817 ms 51600 KB Output is correct
115 Correct 789 ms 51704 KB Output is correct
116 Correct 870 ms 55544 KB Output is correct
117 Correct 628 ms 57976 KB Output is correct
118 Correct 760 ms 52608 KB Output is correct
119 Correct 600 ms 57888 KB Output is correct
120 Correct 690 ms 74616 KB Output is correct
121 Correct 648 ms 58104 KB Output is correct
122 Correct 804 ms 57208 KB Output is correct
123 Correct 378 ms 57420 KB Output is correct
124 Correct 690 ms 56948 KB Output is correct