답안 #706227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706227 2023-03-06T06:55:17 Z alvingogo Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 169196 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

/* Merger:
first, find out the LCA of all stages

second, for each node find out whether we can cut the edge its father to the node, which can be done by storing every node's stage's LCA
if all its subtree's node's stage's LCA is in its subtree then we can cut it

last, for all the node we can build a virtual tree
we know that if we see the edges between virtual tree's node to node as a stage, the answer won't change

if we merge two stages, then the nodes on the path of the two stages are all uncuttable, so we only have to find out the minimum path cover

and i think it's equal to (the number of leaf (deg == 1) + 1) / 2, done!

guess a few coding will lead us to the ac :happy_mention:
*/
vector<int> e;
struct no{
	vector<int> ch,ch2;
	int as[21]={0};
	int dep=-1;
	int md=1e9;
	int in=0;
	int t;
};
vector<no> v;
vector<int> can;
int cnt=0;
void dfs(int r,int f){
	v[r].dep=v[f].dep+1;
	v[r].as[0]=f;
	v[r].in=cnt;
	cnt++;
	for(auto h:v[r].ch){
		if(h!=f){
			dfs(h,r);
		}
	}
}
void dfs2(int r,int f){
	v[r].md=v[e[v[r].t]].dep;
	for(auto h:v[r].ch){
		if(h!=f){
			dfs2(h,r);
			v[r].md=min(v[r].md,v[h].md);
		}
	}
	if(r!=f){
		if(v[r].md>=v[r].dep){
			can.push_back(f);
			can.push_back(r);
		}
	}
}
int lca(int a,int b){
	if(v[a].dep<v[b].dep){
		swap(a,b);
	}
	for(int i=20;i>=0;i--){
		if(v[v[a].as[i]].dep>=v[b].dep){
			a=v[a].as[i];
		}
	}
	if(a==b){
		return a;
	}
	for(int i=20;i>=0;i--){
		if(v[a].as[i]!=v[b].as[i]){
			a=v[a].as[i];
			b=v[b].as[i];
		}
	}
	return v[a].as[0];
}
bool cmp(int a,int b){
	return v[a].in<v[b].in;
}
int bvt(vector<int> x,int flag){
	sort(x.begin(),x.end(),cmp);
	int u=x.size();
	for(int i=1;i<u;i++){
		x.push_back(lca(x[i],x[i-1]));
	}
	sort(x.begin(),x.end(),cmp);
	if(flag==1){	
		x.erase(unique(x.begin(),x.end()),x.end());
		u=x.size();
		for(int i=0;i<u-1;i++){
			int z=lca(x[i],x[i+1]);
			v[z].ch2.push_back(x[i+1]);
			v[x[i+1]].ch2.push_back(z);
		}
		can=x;
	}
	return x[0];
}
int main(){
	AquA;
	int n,k;
	cin >> n >> k;
	v.resize(n);
	e.resize(k);
	for(int i=1;i<n;i++){
		int a,b;
		cin >> a >> b;
		a--;
		b--;
		v[a].ch.push_back(b);
		v[b].ch.push_back(a);
	}
	dfs(0,0);
	for(int i=1;i<21;i++){
		for(int j=0;j<n;j++){
			v[j].as[i]=v[v[j].as[i-1]].as[i-1];
		}
	}
	vector<vector<int> > gg(k);
	for(int i=0;i<n;i++){
		int a;
		cin >> a;
		a--;
		v[i].t=a;
		gg[a].push_back(i);
	}
	for(int i=0;i<k;i++){
		e[i]=bvt(gg[i],0);
	}
	dfs2(0,0);
	if(!can.size()){
		cout << 0 << "\n";
		return 0;
	}
	if(can.size()==1){
		cout << 1 << "\n";
		return 0;
	}
	bvt(can,1);
	int ans=0;
	for(auto h:can){
		if(v[h].ch2.size()==1){
			ans++;
		}
	}
	cout << (ans+1)/2 << '\n';
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 4 ms 1108 KB Output is correct
27 Correct 3 ms 932 KB Output is correct
28 Correct 5 ms 1236 KB Output is correct
29 Correct 7 ms 1240 KB Output is correct
30 Correct 3 ms 844 KB Output is correct
31 Correct 1 ms 320 KB Output is correct
32 Correct 3 ms 1108 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 3 ms 852 KB Output is correct
35 Correct 6 ms 1236 KB Output is correct
36 Correct 3 ms 852 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 3 ms 980 KB Output is correct
40 Correct 4 ms 848 KB Output is correct
41 Correct 3 ms 852 KB Output is correct
42 Correct 3 ms 980 KB Output is correct
43 Correct 3 ms 980 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 3 ms 972 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 5 ms 1228 KB Output is correct
50 Correct 6 ms 1232 KB Output is correct
51 Correct 3 ms 980 KB Output is correct
52 Correct 3 ms 856 KB Output is correct
53 Correct 3 ms 980 KB Output is correct
54 Correct 3 ms 864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 129 ms 20880 KB Output is correct
27 Correct 168 ms 20504 KB Output is correct
28 Correct 3 ms 868 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 161 ms 20356 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 257 ms 24648 KB Output is correct
34 Correct 167 ms 20428 KB Output is correct
35 Correct 3 ms 848 KB Output is correct
36 Correct 179 ms 20804 KB Output is correct
37 Correct 4 ms 968 KB Output is correct
38 Correct 3 ms 852 KB Output is correct
39 Correct 118 ms 20664 KB Output is correct
40 Correct 3 ms 980 KB Output is correct
41 Correct 209 ms 21012 KB Output is correct
42 Correct 236 ms 21684 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 261 ms 24620 KB Output is correct
45 Correct 219 ms 22496 KB Output is correct
46 Correct 3 ms 852 KB Output is correct
47 Correct 3 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 19224 KB Output is correct
2 Correct 226 ms 30456 KB Output is correct
3 Correct 4 ms 1108 KB Output is correct
4 Correct 3 ms 852 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 164 ms 23972 KB Output is correct
9 Correct 3 ms 852 KB Output is correct
10 Correct 149 ms 20656 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 169 ms 20500 KB Output is correct
13 Correct 199 ms 24524 KB Output is correct
14 Correct 387 ms 31568 KB Output is correct
15 Correct 126 ms 20800 KB Output is correct
16 Correct 3 ms 980 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 215 ms 30388 KB Output is correct
19 Correct 466 ms 33908 KB Output is correct
20 Correct 3 ms 980 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 126 ms 24392 KB Output is correct
23 Correct 3 ms 980 KB Output is correct
24 Correct 186 ms 21804 KB Output is correct
25 Correct 349 ms 32944 KB Output is correct
26 Correct 5 ms 1232 KB Output is correct
27 Correct 6 ms 1236 KB Output is correct
28 Correct 3 ms 980 KB Output is correct
29 Correct 3 ms 848 KB Output is correct
30 Correct 3 ms 1052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 4 ms 1108 KB Output is correct
27 Correct 3 ms 932 KB Output is correct
28 Correct 5 ms 1236 KB Output is correct
29 Correct 7 ms 1240 KB Output is correct
30 Correct 3 ms 844 KB Output is correct
31 Correct 1 ms 320 KB Output is correct
32 Correct 3 ms 1108 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 3 ms 852 KB Output is correct
35 Correct 6 ms 1236 KB Output is correct
36 Correct 3 ms 852 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 3 ms 980 KB Output is correct
40 Correct 4 ms 848 KB Output is correct
41 Correct 3 ms 852 KB Output is correct
42 Correct 3 ms 980 KB Output is correct
43 Correct 3 ms 980 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 3 ms 972 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 5 ms 1228 KB Output is correct
50 Correct 6 ms 1232 KB Output is correct
51 Correct 3 ms 980 KB Output is correct
52 Correct 3 ms 856 KB Output is correct
53 Correct 3 ms 980 KB Output is correct
54 Correct 3 ms 864 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 129 ms 20880 KB Output is correct
57 Correct 168 ms 20504 KB Output is correct
58 Correct 3 ms 868 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 161 ms 20356 KB Output is correct
62 Correct 2 ms 852 KB Output is correct
63 Correct 257 ms 24648 KB Output is correct
64 Correct 167 ms 20428 KB Output is correct
65 Correct 3 ms 848 KB Output is correct
66 Correct 179 ms 20804 KB Output is correct
67 Correct 4 ms 968 KB Output is correct
68 Correct 3 ms 852 KB Output is correct
69 Correct 118 ms 20664 KB Output is correct
70 Correct 3 ms 980 KB Output is correct
71 Correct 209 ms 21012 KB Output is correct
72 Correct 236 ms 21684 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 261 ms 24620 KB Output is correct
75 Correct 219 ms 22496 KB Output is correct
76 Correct 3 ms 852 KB Output is correct
77 Correct 3 ms 852 KB Output is correct
78 Correct 115 ms 19224 KB Output is correct
79 Correct 226 ms 30456 KB Output is correct
80 Correct 4 ms 1108 KB Output is correct
81 Correct 3 ms 852 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 320 KB Output is correct
84 Correct 2 ms 852 KB Output is correct
85 Correct 164 ms 23972 KB Output is correct
86 Correct 3 ms 852 KB Output is correct
87 Correct 149 ms 20656 KB Output is correct
88 Correct 0 ms 340 KB Output is correct
89 Correct 169 ms 20500 KB Output is correct
90 Correct 199 ms 24524 KB Output is correct
91 Correct 387 ms 31568 KB Output is correct
92 Correct 126 ms 20800 KB Output is correct
93 Correct 3 ms 980 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 215 ms 30388 KB Output is correct
96 Correct 466 ms 33908 KB Output is correct
97 Correct 3 ms 980 KB Output is correct
98 Correct 1 ms 340 KB Output is correct
99 Correct 126 ms 24392 KB Output is correct
100 Correct 3 ms 980 KB Output is correct
101 Correct 186 ms 21804 KB Output is correct
102 Correct 349 ms 32944 KB Output is correct
103 Correct 5 ms 1232 KB Output is correct
104 Correct 6 ms 1236 KB Output is correct
105 Correct 3 ms 980 KB Output is correct
106 Correct 3 ms 848 KB Output is correct
107 Correct 3 ms 1052 KB Output is correct
108 Correct 702 ms 116820 KB Output is correct
109 Correct 1886 ms 113984 KB Output is correct
110 Correct 1953 ms 117908 KB Output is correct
111 Execution timed out 3043 ms 169196 KB Time limit exceeded
112 Halted 0 ms 0 KB -