Submission #972702

# Submission time Handle Problem Language Result Execution time Memory
972702 2024-05-01T02:54:44 Z LCJLY Mergers (JOI19_mergers) C++14
100 / 100
1665 ms 183796 KB
#include <bits/stdc++.h>
using namespace std;
	
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,long long>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
 
int n,k;
vector<int>adj[500005];
int two[22][500005];
int in[500005];
int out[500005];
int d[500005];
int ptr=0;
 
 
void dfs(int index, int par){
	in[index]=++ptr;
	for(int x=0;x<20;x++){
		two[x+1][index]=two[x][two[x][index]];
	}
	
	for(auto it:adj[index]){
		if(it==par) continue;
		d[it]=d[index]+1;
		two[0][it]=index;
		dfs(it,index);
	}
	out[index]=ptr;
}
 
int lca(int a, int b){
	if(d[a]>d[b]) swap(a,b);
	int diff=d[b]-d[a];
	for(int x=0;x<20;x++){
		if(diff&(1<<x)){
			b=two[x][b];
		}
	}
	if(a==b) return a;
	for(int x=19;x>=0;x--){
		if(two[x][a]!=two[x][b]){
			a=two[x][a];
			b=two[x][b];
		}
	}
	return two[0][a];
}
 
struct DSU{
	vector<int>e;
	void init(int n){
		e=vector<int>(n,-1);
	}
	
	int get(int x){return e[x]<0?x:e[x]=get(e[x]);}
	
	bool unite(int x, int y){
		x=get(x); y=get(y);
		if(x==y) return 0;
		if(d[x]>d[y]) swap(x,y);
		e[x]+=e[y];
		e[y]=x;
		return 1;
	}
};
 
bool cmp(int a, int b){
	return in[a]<in[b];
}
 
DSU dsu;
bool visited[500005];
int deg[500005];
 
void solve(){
	cin >> n >> k; 
	
	vector<pii>edge;
	int temp,temp2;
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2;
		adj[temp].push_back(temp2);
		adj[temp2].push_back(temp);
		edge.push_back({temp,temp2});
	}
	
	dfs(1,-1);
	vector<int>v[k+5];
	
	for(int x=1;x<=n;x++){
		cin >> temp;
		v[temp].push_back(x);
	}
	
	dsu.init(n+5);
	
	for(int x=1;x<=k;x++){
		if((int)v[x].size()<=1) continue;
		//sort(v[x].begin(),v[x].end(),cmp);
		for(int y=0;y<(int)v[x].size();y++){
			int hold=lca(v[x][y],v[x][0]);
			int cur=dsu.get(v[x][0]);
			while(d[cur]>d[hold]){
				dsu.unite(cur,two[0][cur]);
				cur=dsu.get(cur);
			}
			cur=dsu.get(v[x][y]);
			while(d[cur]>d[hold]){
				dsu.unite(cur,two[0][cur]);
				cur=dsu.get(cur);
			}
		}
	}
	
	int counter=0;
	for(int x=0;x<n-1;x++){
		int a=edge[x].first;
		int b=edge[x].second;
		a=dsu.get(a);
		b=dsu.get(b);
		if(a==b) continue;
		deg[a]++;
		deg[b]++;
	}
	
	for(int x=1;x<=n;x++){
		int hold=dsu.get(x);
		if(visited[hold]) continue;
		visited[hold]=true;
		if(deg[hold]==1) counter++;
	}
	
	cout << (counter+1)/2;
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//freopen("in.txt","r",stdin);
	//cin >> t;
	while(t--){
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 60756 KB Output is correct
2 Correct 11 ms 60764 KB Output is correct
3 Correct 9 ms 60944 KB Output is correct
4 Correct 9 ms 60760 KB Output is correct
5 Correct 10 ms 60856 KB Output is correct
6 Correct 10 ms 60760 KB Output is correct
7 Correct 9 ms 60760 KB Output is correct
8 Correct 9 ms 60760 KB Output is correct
9 Correct 9 ms 60760 KB Output is correct
10 Correct 9 ms 60764 KB Output is correct
11 Correct 9 ms 60764 KB Output is correct
12 Correct 9 ms 60764 KB Output is correct
13 Correct 9 ms 60760 KB Output is correct
14 Correct 9 ms 60772 KB Output is correct
15 Correct 9 ms 60972 KB Output is correct
16 Correct 9 ms 60772 KB Output is correct
17 Correct 10 ms 60772 KB Output is correct
18 Correct 9 ms 60768 KB Output is correct
19 Correct 9 ms 60772 KB Output is correct
20 Correct 9 ms 60908 KB Output is correct
21 Correct 9 ms 60772 KB Output is correct
22 Correct 9 ms 60772 KB Output is correct
23 Correct 9 ms 60772 KB Output is correct
24 Correct 10 ms 60772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 60756 KB Output is correct
2 Correct 11 ms 60764 KB Output is correct
3 Correct 9 ms 60944 KB Output is correct
4 Correct 9 ms 60760 KB Output is correct
5 Correct 10 ms 60856 KB Output is correct
6 Correct 10 ms 60760 KB Output is correct
7 Correct 9 ms 60760 KB Output is correct
8 Correct 9 ms 60760 KB Output is correct
9 Correct 9 ms 60760 KB Output is correct
10 Correct 9 ms 60764 KB Output is correct
11 Correct 9 ms 60764 KB Output is correct
12 Correct 9 ms 60764 KB Output is correct
13 Correct 9 ms 60760 KB Output is correct
14 Correct 9 ms 60772 KB Output is correct
15 Correct 9 ms 60972 KB Output is correct
16 Correct 9 ms 60772 KB Output is correct
17 Correct 10 ms 60772 KB Output is correct
18 Correct 9 ms 60768 KB Output is correct
19 Correct 9 ms 60772 KB Output is correct
20 Correct 9 ms 60908 KB Output is correct
21 Correct 9 ms 60772 KB Output is correct
22 Correct 9 ms 60772 KB Output is correct
23 Correct 9 ms 60772 KB Output is correct
24 Correct 10 ms 60772 KB Output is correct
25 Correct 9 ms 60760 KB Output is correct
26 Correct 11 ms 61100 KB Output is correct
27 Correct 11 ms 61028 KB Output is correct
28 Correct 10 ms 61304 KB Output is correct
29 Correct 11 ms 61176 KB Output is correct
30 Correct 10 ms 61028 KB Output is correct
31 Correct 9 ms 60772 KB Output is correct
32 Correct 10 ms 61288 KB Output is correct
33 Correct 8 ms 60776 KB Output is correct
34 Correct 10 ms 61020 KB Output is correct
35 Correct 12 ms 61272 KB Output is correct
36 Correct 11 ms 61020 KB Output is correct
37 Correct 11 ms 61276 KB Output is correct
38 Correct 9 ms 60764 KB Output is correct
39 Correct 11 ms 61276 KB Output is correct
40 Correct 10 ms 61084 KB Output is correct
41 Correct 11 ms 61128 KB Output is correct
42 Correct 11 ms 61276 KB Output is correct
43 Correct 11 ms 61276 KB Output is correct
44 Correct 9 ms 60760 KB Output is correct
45 Correct 10 ms 61132 KB Output is correct
46 Correct 11 ms 61112 KB Output is correct
47 Correct 9 ms 60760 KB Output is correct
48 Correct 10 ms 61276 KB Output is correct
49 Correct 10 ms 61276 KB Output is correct
50 Correct 10 ms 61256 KB Output is correct
51 Correct 11 ms 61272 KB Output is correct
52 Correct 11 ms 61128 KB Output is correct
53 Correct 11 ms 61276 KB Output is correct
54 Correct 11 ms 61020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 60756 KB Output is correct
2 Correct 11 ms 60764 KB Output is correct
3 Correct 9 ms 60944 KB Output is correct
4 Correct 9 ms 60760 KB Output is correct
5 Correct 10 ms 60856 KB Output is correct
6 Correct 10 ms 60760 KB Output is correct
7 Correct 9 ms 60760 KB Output is correct
8 Correct 9 ms 60760 KB Output is correct
9 Correct 9 ms 60760 KB Output is correct
10 Correct 9 ms 60764 KB Output is correct
11 Correct 9 ms 60764 KB Output is correct
12 Correct 9 ms 60764 KB Output is correct
13 Correct 9 ms 60760 KB Output is correct
14 Correct 9 ms 60772 KB Output is correct
15 Correct 9 ms 60972 KB Output is correct
16 Correct 9 ms 60772 KB Output is correct
17 Correct 10 ms 60772 KB Output is correct
18 Correct 9 ms 60768 KB Output is correct
19 Correct 9 ms 60772 KB Output is correct
20 Correct 9 ms 60908 KB Output is correct
21 Correct 9 ms 60772 KB Output is correct
22 Correct 9 ms 60772 KB Output is correct
23 Correct 9 ms 60772 KB Output is correct
24 Correct 10 ms 60772 KB Output is correct
25 Correct 9 ms 60764 KB Output is correct
26 Correct 137 ms 73656 KB Output is correct
27 Correct 131 ms 75172 KB Output is correct
28 Correct 11 ms 61020 KB Output is correct
29 Correct 10 ms 60760 KB Output is correct
30 Correct 9 ms 60764 KB Output is correct
31 Correct 147 ms 75020 KB Output is correct
32 Correct 10 ms 61016 KB Output is correct
33 Correct 112 ms 80316 KB Output is correct
34 Correct 136 ms 75184 KB Output is correct
35 Correct 10 ms 61016 KB Output is correct
36 Correct 106 ms 73884 KB Output is correct
37 Correct 11 ms 61032 KB Output is correct
38 Correct 10 ms 61144 KB Output is correct
39 Correct 120 ms 75212 KB Output is correct
40 Correct 10 ms 61288 KB Output is correct
41 Correct 83 ms 74632 KB Output is correct
42 Correct 122 ms 75960 KB Output is correct
43 Correct 10 ms 60760 KB Output is correct
44 Correct 98 ms 79944 KB Output is correct
45 Correct 156 ms 78928 KB Output is correct
46 Correct 12 ms 61016 KB Output is correct
47 Correct 11 ms 61020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 75288 KB Output is correct
2 Correct 73 ms 80940 KB Output is correct
3 Correct 10 ms 61272 KB Output is correct
4 Correct 11 ms 61020 KB Output is correct
5 Correct 9 ms 60764 KB Output is correct
6 Correct 10 ms 60764 KB Output is correct
7 Correct 10 ms 61140 KB Output is correct
8 Correct 148 ms 76476 KB Output is correct
9 Correct 10 ms 61128 KB Output is correct
10 Correct 176 ms 74636 KB Output is correct
11 Correct 9 ms 60764 KB Output is correct
12 Correct 107 ms 74072 KB Output is correct
13 Correct 126 ms 77764 KB Output is correct
14 Correct 89 ms 78880 KB Output is correct
15 Correct 113 ms 75156 KB Output is correct
16 Correct 10 ms 61276 KB Output is correct
17 Correct 8 ms 60840 KB Output is correct
18 Correct 90 ms 80068 KB Output is correct
19 Correct 96 ms 80836 KB Output is correct
20 Correct 11 ms 61276 KB Output is correct
21 Correct 9 ms 60760 KB Output is correct
22 Correct 151 ms 78248 KB Output is correct
23 Correct 10 ms 61276 KB Output is correct
24 Correct 111 ms 75044 KB Output is correct
25 Correct 96 ms 79200 KB Output is correct
26 Correct 11 ms 61272 KB Output is correct
27 Correct 10 ms 61276 KB Output is correct
28 Correct 11 ms 61276 KB Output is correct
29 Correct 10 ms 61020 KB Output is correct
30 Correct 11 ms 61276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 60756 KB Output is correct
2 Correct 11 ms 60764 KB Output is correct
3 Correct 9 ms 60944 KB Output is correct
4 Correct 9 ms 60760 KB Output is correct
5 Correct 10 ms 60856 KB Output is correct
6 Correct 10 ms 60760 KB Output is correct
7 Correct 9 ms 60760 KB Output is correct
8 Correct 9 ms 60760 KB Output is correct
9 Correct 9 ms 60760 KB Output is correct
10 Correct 9 ms 60764 KB Output is correct
11 Correct 9 ms 60764 KB Output is correct
12 Correct 9 ms 60764 KB Output is correct
13 Correct 9 ms 60760 KB Output is correct
14 Correct 9 ms 60772 KB Output is correct
15 Correct 9 ms 60972 KB Output is correct
16 Correct 9 ms 60772 KB Output is correct
17 Correct 10 ms 60772 KB Output is correct
18 Correct 9 ms 60768 KB Output is correct
19 Correct 9 ms 60772 KB Output is correct
20 Correct 9 ms 60908 KB Output is correct
21 Correct 9 ms 60772 KB Output is correct
22 Correct 9 ms 60772 KB Output is correct
23 Correct 9 ms 60772 KB Output is correct
24 Correct 10 ms 60772 KB Output is correct
25 Correct 9 ms 60760 KB Output is correct
26 Correct 11 ms 61100 KB Output is correct
27 Correct 11 ms 61028 KB Output is correct
28 Correct 10 ms 61304 KB Output is correct
29 Correct 11 ms 61176 KB Output is correct
30 Correct 10 ms 61028 KB Output is correct
31 Correct 9 ms 60772 KB Output is correct
32 Correct 10 ms 61288 KB Output is correct
33 Correct 8 ms 60776 KB Output is correct
34 Correct 10 ms 61020 KB Output is correct
35 Correct 12 ms 61272 KB Output is correct
36 Correct 11 ms 61020 KB Output is correct
37 Correct 11 ms 61276 KB Output is correct
38 Correct 9 ms 60764 KB Output is correct
39 Correct 11 ms 61276 KB Output is correct
40 Correct 10 ms 61084 KB Output is correct
41 Correct 11 ms 61128 KB Output is correct
42 Correct 11 ms 61276 KB Output is correct
43 Correct 11 ms 61276 KB Output is correct
44 Correct 9 ms 60760 KB Output is correct
45 Correct 10 ms 61132 KB Output is correct
46 Correct 11 ms 61112 KB Output is correct
47 Correct 9 ms 60760 KB Output is correct
48 Correct 10 ms 61276 KB Output is correct
49 Correct 10 ms 61276 KB Output is correct
50 Correct 10 ms 61256 KB Output is correct
51 Correct 11 ms 61272 KB Output is correct
52 Correct 11 ms 61128 KB Output is correct
53 Correct 11 ms 61276 KB Output is correct
54 Correct 11 ms 61020 KB Output is correct
55 Correct 9 ms 60764 KB Output is correct
56 Correct 137 ms 73656 KB Output is correct
57 Correct 131 ms 75172 KB Output is correct
58 Correct 11 ms 61020 KB Output is correct
59 Correct 10 ms 60760 KB Output is correct
60 Correct 9 ms 60764 KB Output is correct
61 Correct 147 ms 75020 KB Output is correct
62 Correct 10 ms 61016 KB Output is correct
63 Correct 112 ms 80316 KB Output is correct
64 Correct 136 ms 75184 KB Output is correct
65 Correct 10 ms 61016 KB Output is correct
66 Correct 106 ms 73884 KB Output is correct
67 Correct 11 ms 61032 KB Output is correct
68 Correct 10 ms 61144 KB Output is correct
69 Correct 120 ms 75212 KB Output is correct
70 Correct 10 ms 61288 KB Output is correct
71 Correct 83 ms 74632 KB Output is correct
72 Correct 122 ms 75960 KB Output is correct
73 Correct 10 ms 60760 KB Output is correct
74 Correct 98 ms 79944 KB Output is correct
75 Correct 156 ms 78928 KB Output is correct
76 Correct 12 ms 61016 KB Output is correct
77 Correct 11 ms 61020 KB Output is correct
78 Correct 130 ms 75288 KB Output is correct
79 Correct 73 ms 80940 KB Output is correct
80 Correct 10 ms 61272 KB Output is correct
81 Correct 11 ms 61020 KB Output is correct
82 Correct 9 ms 60764 KB Output is correct
83 Correct 10 ms 60764 KB Output is correct
84 Correct 10 ms 61140 KB Output is correct
85 Correct 148 ms 76476 KB Output is correct
86 Correct 10 ms 61128 KB Output is correct
87 Correct 176 ms 74636 KB Output is correct
88 Correct 9 ms 60764 KB Output is correct
89 Correct 107 ms 74072 KB Output is correct
90 Correct 126 ms 77764 KB Output is correct
91 Correct 89 ms 78880 KB Output is correct
92 Correct 113 ms 75156 KB Output is correct
93 Correct 10 ms 61276 KB Output is correct
94 Correct 8 ms 60840 KB Output is correct
95 Correct 90 ms 80068 KB Output is correct
96 Correct 96 ms 80836 KB Output is correct
97 Correct 11 ms 61276 KB Output is correct
98 Correct 9 ms 60760 KB Output is correct
99 Correct 151 ms 78248 KB Output is correct
100 Correct 10 ms 61276 KB Output is correct
101 Correct 111 ms 75044 KB Output is correct
102 Correct 96 ms 79200 KB Output is correct
103 Correct 11 ms 61272 KB Output is correct
104 Correct 10 ms 61276 KB Output is correct
105 Correct 11 ms 61276 KB Output is correct
106 Correct 10 ms 61020 KB Output is correct
107 Correct 11 ms 61276 KB Output is correct
108 Correct 1296 ms 166488 KB Output is correct
109 Correct 1020 ms 166892 KB Output is correct
110 Correct 893 ms 170700 KB Output is correct
111 Correct 913 ms 183796 KB Output is correct
112 Correct 929 ms 181956 KB Output is correct
113 Correct 641 ms 178332 KB Output is correct
114 Correct 798 ms 151452 KB Output is correct
115 Correct 602 ms 149964 KB Output is correct
116 Correct 1665 ms 160432 KB Output is correct
117 Correct 609 ms 179652 KB Output is correct
118 Correct 1224 ms 154512 KB Output is correct
119 Correct 615 ms 179752 KB Output is correct
120 Correct 908 ms 180968 KB Output is correct
121 Correct 614 ms 179800 KB Output is correct
122 Correct 1228 ms 169360 KB Output is correct
123 Correct 474 ms 180640 KB Output is correct
124 Correct 653 ms 175116 KB Output is correct