답안 #949627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949627 2024-03-19T13:03:17 Z pcc Mergers (JOI19_mergers) C++17
100 / 100
1937 ms 143448 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>

const int B = 20;
const int mxn = 5e5+10;
vector<int> tree[mxn];
int N,K;
int arr[mxn];
vector<int> col[mxn];
int par[mxn][B],dep[mxn];
bitset<mxn> done;
int deg[mxn];

struct DSU{
	int dsu[mxn],sz[mxn],high[mxn];
	DSU(){
		for(int i = 1;i<mxn;i++){
			dsu[i] = i;
			sz[i] = 1;
			high[i] = i;
		}
	}
	int root(int k){
		return k == dsu[k]?k:dsu[k] = root(dsu[k]);
	}
	void onion(int a,int b){
		a = root(a),b = root(b);
		if(a == b)return;
		if(sz[a]<sz[b])swap(a,b);
		dsu[b] = a;
		sz[a] += sz[b];
		if(dep[high[b]]<dep[high[a]])high[a] = high[b];
		return;
	}
};
DSU dsu;

void dfs(int now){
	for(int i = 1;i<B;i++){
		par[now][i] = par[par[now][i-1]][i-1];
	}
	for(auto nxt:tree[now]){
		if(nxt == par[now][0])continue;
		par[nxt][0] = now;
		dep[nxt] = dep[now]+1;
		dfs(nxt);
	}
	return;
}
int LCA(int a,int b){
	if(dep[a]<dep[b])swap(a,b);
	int d = dep[a]-dep[b];
	for(int i = B-1;i>=0;i--){
		if(d&(1<<i))a = par[a][i];
	}
	for(int i = B-1;i>=0;i--){
		if(par[a][i] != par[b][i])a = par[a][i],b = par[b][i];
	}
	return a == b?a:par[a][0];
}

void shrink(int c){
	int p = col[c][0];
	for(auto &i:col[c])p = LCA(i,p);
	for(auto now:col[c]){
		while(dep[now]>dep[p]){
			dsu.onion(now,par[now][0]);
			now = dsu.high[dsu.root(now)];
		}
	}
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>K;
	for(int i = 1;i<N;i++){
		int a,b;
		cin>>a>>b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	for(int i = 1;i<=N;i++){
		cin>>arr[i];
		col[arr[i]].push_back(i);
	}
	par[1][0] = 1;
	dfs(1);
	for(int i = 1;i<=K;i++){
		if(!done[i])shrink(i);
	}
	int cnt = 0;
	int one = 0;
	for(int i = 1;i<=N;i++){
		cnt += (dsu.root(i) == i);
		for(auto nxt:tree[i]){
			if(dsu.root(i) == dsu.root(nxt))continue;
			deg[dsu.root(nxt)]++;
		}
	}
	cerr<<cnt<<endl;
	for(int i = 1;i<=N;i++)cerr<<dsu.root(i)<<' ';cerr<<endl;
	for(int i = 1;i<=N;i++)one += (deg[i] == 1);
	cout<<(cnt==1?0:(one+1)>>1);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:109:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  109 |  for(int i = 1;i<=N;i++)cerr<<dsu.root(i)<<' ';cerr<<endl;
      |  ^~~
mergers.cpp:109:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  109 |  for(int i = 1;i<=N;i++)cerr<<dsu.root(i)<<' ';cerr<<endl;
      |                                                ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 35672 KB Output is correct
2 Correct 9 ms 35676 KB Output is correct
3 Correct 9 ms 35676 KB Output is correct
4 Correct 9 ms 35912 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 9 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35672 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
11 Correct 8 ms 35920 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 8 ms 35928 KB Output is correct
15 Correct 8 ms 35676 KB Output is correct
16 Correct 8 ms 35676 KB Output is correct
17 Correct 8 ms 35676 KB Output is correct
18 Correct 8 ms 35676 KB Output is correct
19 Correct 8 ms 35664 KB Output is correct
20 Correct 8 ms 35892 KB Output is correct
21 Correct 9 ms 35900 KB Output is correct
22 Correct 8 ms 35676 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 8 ms 35868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 35672 KB Output is correct
2 Correct 9 ms 35676 KB Output is correct
3 Correct 9 ms 35676 KB Output is correct
4 Correct 9 ms 35912 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 9 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35672 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
11 Correct 8 ms 35920 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 8 ms 35928 KB Output is correct
15 Correct 8 ms 35676 KB Output is correct
16 Correct 8 ms 35676 KB Output is correct
17 Correct 8 ms 35676 KB Output is correct
18 Correct 8 ms 35676 KB Output is correct
19 Correct 8 ms 35664 KB Output is correct
20 Correct 8 ms 35892 KB Output is correct
21 Correct 9 ms 35900 KB Output is correct
22 Correct 8 ms 35676 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 8 ms 35868 KB Output is correct
25 Correct 8 ms 35676 KB Output is correct
26 Correct 16 ms 36032 KB Output is correct
27 Correct 16 ms 35932 KB Output is correct
28 Correct 16 ms 36172 KB Output is correct
29 Correct 16 ms 35928 KB Output is correct
30 Correct 15 ms 35932 KB Output is correct
31 Correct 8 ms 35676 KB Output is correct
32 Correct 17 ms 36188 KB Output is correct
33 Correct 8 ms 35672 KB Output is correct
34 Correct 15 ms 35932 KB Output is correct
35 Correct 17 ms 35932 KB Output is correct
36 Correct 15 ms 35928 KB Output is correct
37 Correct 16 ms 35928 KB Output is correct
38 Correct 8 ms 35676 KB Output is correct
39 Correct 16 ms 35932 KB Output is correct
40 Correct 15 ms 36084 KB Output is correct
41 Correct 16 ms 35928 KB Output is correct
42 Correct 17 ms 36188 KB Output is correct
43 Correct 16 ms 36164 KB Output is correct
44 Correct 9 ms 35860 KB Output is correct
45 Correct 16 ms 36188 KB Output is correct
46 Correct 16 ms 35932 KB Output is correct
47 Correct 8 ms 35672 KB Output is correct
48 Correct 16 ms 35932 KB Output is correct
49 Correct 16 ms 36184 KB Output is correct
50 Correct 16 ms 36188 KB Output is correct
51 Correct 16 ms 35932 KB Output is correct
52 Correct 15 ms 35928 KB Output is correct
53 Correct 16 ms 35928 KB Output is correct
54 Correct 16 ms 35932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 35672 KB Output is correct
2 Correct 9 ms 35676 KB Output is correct
3 Correct 9 ms 35676 KB Output is correct
4 Correct 9 ms 35912 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 9 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35672 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
11 Correct 8 ms 35920 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 8 ms 35928 KB Output is correct
15 Correct 8 ms 35676 KB Output is correct
16 Correct 8 ms 35676 KB Output is correct
17 Correct 8 ms 35676 KB Output is correct
18 Correct 8 ms 35676 KB Output is correct
19 Correct 8 ms 35664 KB Output is correct
20 Correct 8 ms 35892 KB Output is correct
21 Correct 9 ms 35900 KB Output is correct
22 Correct 8 ms 35676 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 8 ms 35868 KB Output is correct
25 Correct 8 ms 35676 KB Output is correct
26 Correct 279 ms 50312 KB Output is correct
27 Correct 293 ms 50360 KB Output is correct
28 Correct 16 ms 36096 KB Output is correct
29 Correct 8 ms 35676 KB Output is correct
30 Correct 8 ms 35676 KB Output is correct
31 Correct 289 ms 49744 KB Output is correct
32 Correct 16 ms 35928 KB Output is correct
33 Correct 313 ms 57088 KB Output is correct
34 Correct 288 ms 49992 KB Output is correct
35 Correct 16 ms 35928 KB Output is correct
36 Correct 294 ms 50652 KB Output is correct
37 Correct 16 ms 35932 KB Output is correct
38 Correct 17 ms 35928 KB Output is correct
39 Correct 272 ms 50256 KB Output is correct
40 Correct 16 ms 36188 KB Output is correct
41 Correct 281 ms 49864 KB Output is correct
42 Correct 318 ms 52304 KB Output is correct
43 Correct 8 ms 35676 KB Output is correct
44 Correct 304 ms 57036 KB Output is correct
45 Correct 306 ms 53588 KB Output is correct
46 Correct 15 ms 35932 KB Output is correct
47 Correct 16 ms 35932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 286 ms 50120 KB Output is correct
2 Correct 298 ms 53204 KB Output is correct
3 Correct 16 ms 35932 KB Output is correct
4 Correct 15 ms 35928 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 8 ms 35856 KB Output is correct
7 Correct 16 ms 35932 KB Output is correct
8 Correct 307 ms 51604 KB Output is correct
9 Correct 16 ms 35928 KB Output is correct
10 Correct 291 ms 50100 KB Output is correct
11 Correct 8 ms 35676 KB Output is correct
12 Correct 299 ms 50376 KB Output is correct
13 Correct 299 ms 51540 KB Output is correct
14 Correct 304 ms 53184 KB Output is correct
15 Correct 271 ms 50248 KB Output is correct
16 Correct 16 ms 35932 KB Output is correct
17 Correct 8 ms 35672 KB Output is correct
18 Correct 292 ms 52940 KB Output is correct
19 Correct 326 ms 56916 KB Output is correct
20 Correct 16 ms 35920 KB Output is correct
21 Correct 8 ms 35676 KB Output is correct
22 Correct 296 ms 51896 KB Output is correct
23 Correct 16 ms 35928 KB Output is correct
24 Correct 303 ms 50704 KB Output is correct
25 Correct 321 ms 56008 KB Output is correct
26 Correct 17 ms 35932 KB Output is correct
27 Correct 16 ms 36188 KB Output is correct
28 Correct 15 ms 36096 KB Output is correct
29 Correct 16 ms 36104 KB Output is correct
30 Correct 15 ms 35920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 35672 KB Output is correct
2 Correct 9 ms 35676 KB Output is correct
3 Correct 9 ms 35676 KB Output is correct
4 Correct 9 ms 35912 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 9 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35672 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
11 Correct 8 ms 35920 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 8 ms 35928 KB Output is correct
15 Correct 8 ms 35676 KB Output is correct
16 Correct 8 ms 35676 KB Output is correct
17 Correct 8 ms 35676 KB Output is correct
18 Correct 8 ms 35676 KB Output is correct
19 Correct 8 ms 35664 KB Output is correct
20 Correct 8 ms 35892 KB Output is correct
21 Correct 9 ms 35900 KB Output is correct
22 Correct 8 ms 35676 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 8 ms 35868 KB Output is correct
25 Correct 8 ms 35676 KB Output is correct
26 Correct 16 ms 36032 KB Output is correct
27 Correct 16 ms 35932 KB Output is correct
28 Correct 16 ms 36172 KB Output is correct
29 Correct 16 ms 35928 KB Output is correct
30 Correct 15 ms 35932 KB Output is correct
31 Correct 8 ms 35676 KB Output is correct
32 Correct 17 ms 36188 KB Output is correct
33 Correct 8 ms 35672 KB Output is correct
34 Correct 15 ms 35932 KB Output is correct
35 Correct 17 ms 35932 KB Output is correct
36 Correct 15 ms 35928 KB Output is correct
37 Correct 16 ms 35928 KB Output is correct
38 Correct 8 ms 35676 KB Output is correct
39 Correct 16 ms 35932 KB Output is correct
40 Correct 15 ms 36084 KB Output is correct
41 Correct 16 ms 35928 KB Output is correct
42 Correct 17 ms 36188 KB Output is correct
43 Correct 16 ms 36164 KB Output is correct
44 Correct 9 ms 35860 KB Output is correct
45 Correct 16 ms 36188 KB Output is correct
46 Correct 16 ms 35932 KB Output is correct
47 Correct 8 ms 35672 KB Output is correct
48 Correct 16 ms 35932 KB Output is correct
49 Correct 16 ms 36184 KB Output is correct
50 Correct 16 ms 36188 KB Output is correct
51 Correct 16 ms 35932 KB Output is correct
52 Correct 15 ms 35928 KB Output is correct
53 Correct 16 ms 35928 KB Output is correct
54 Correct 16 ms 35932 KB Output is correct
55 Correct 8 ms 35676 KB Output is correct
56 Correct 279 ms 50312 KB Output is correct
57 Correct 293 ms 50360 KB Output is correct
58 Correct 16 ms 36096 KB Output is correct
59 Correct 8 ms 35676 KB Output is correct
60 Correct 8 ms 35676 KB Output is correct
61 Correct 289 ms 49744 KB Output is correct
62 Correct 16 ms 35928 KB Output is correct
63 Correct 313 ms 57088 KB Output is correct
64 Correct 288 ms 49992 KB Output is correct
65 Correct 16 ms 35928 KB Output is correct
66 Correct 294 ms 50652 KB Output is correct
67 Correct 16 ms 35932 KB Output is correct
68 Correct 17 ms 35928 KB Output is correct
69 Correct 272 ms 50256 KB Output is correct
70 Correct 16 ms 36188 KB Output is correct
71 Correct 281 ms 49864 KB Output is correct
72 Correct 318 ms 52304 KB Output is correct
73 Correct 8 ms 35676 KB Output is correct
74 Correct 304 ms 57036 KB Output is correct
75 Correct 306 ms 53588 KB Output is correct
76 Correct 15 ms 35932 KB Output is correct
77 Correct 16 ms 35932 KB Output is correct
78 Correct 286 ms 50120 KB Output is correct
79 Correct 298 ms 53204 KB Output is correct
80 Correct 16 ms 35932 KB Output is correct
81 Correct 15 ms 35928 KB Output is correct
82 Correct 8 ms 35676 KB Output is correct
83 Correct 8 ms 35856 KB Output is correct
84 Correct 16 ms 35932 KB Output is correct
85 Correct 307 ms 51604 KB Output is correct
86 Correct 16 ms 35928 KB Output is correct
87 Correct 291 ms 50100 KB Output is correct
88 Correct 8 ms 35676 KB Output is correct
89 Correct 299 ms 50376 KB Output is correct
90 Correct 299 ms 51540 KB Output is correct
91 Correct 304 ms 53184 KB Output is correct
92 Correct 271 ms 50248 KB Output is correct
93 Correct 16 ms 35932 KB Output is correct
94 Correct 8 ms 35672 KB Output is correct
95 Correct 292 ms 52940 KB Output is correct
96 Correct 326 ms 56916 KB Output is correct
97 Correct 16 ms 35920 KB Output is correct
98 Correct 8 ms 35676 KB Output is correct
99 Correct 296 ms 51896 KB Output is correct
100 Correct 16 ms 35928 KB Output is correct
101 Correct 303 ms 50704 KB Output is correct
102 Correct 321 ms 56008 KB Output is correct
103 Correct 17 ms 35932 KB Output is correct
104 Correct 16 ms 36188 KB Output is correct
105 Correct 15 ms 36096 KB Output is correct
106 Correct 16 ms 36104 KB Output is correct
107 Correct 15 ms 35920 KB Output is correct
108 Correct 1649 ms 111680 KB Output is correct
109 Correct 1877 ms 118388 KB Output is correct
110 Correct 1896 ms 129296 KB Output is correct
111 Correct 1898 ms 143448 KB Output is correct
112 Correct 1937 ms 133096 KB Output is correct
113 Correct 1628 ms 119924 KB Output is correct
114 Correct 1591 ms 100416 KB Output is correct
115 Correct 1587 ms 100456 KB Output is correct
116 Correct 1777 ms 108652 KB Output is correct
117 Correct 1785 ms 120300 KB Output is correct
118 Correct 1647 ms 102944 KB Output is correct
119 Correct 1800 ms 120012 KB Output is correct
120 Correct 1902 ms 134012 KB Output is correct
121 Correct 1784 ms 120036 KB Output is correct
122 Correct 1800 ms 113748 KB Output is correct
123 Correct 1621 ms 121836 KB Output is correct
124 Correct 1830 ms 119192 KB Output is correct