답안 #899867

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
899867 2024-01-07T07:56:25 Z PM1 Mergers (JOI19_mergers) C++17
100 / 100
1139 ms 142512 KB
#include <bits/stdc++.h>
using namespace std;
const int mxn=5e5+5;
int n,par[mxn][22],num[mxn],k,s[mxn],st[mxn],fn[mxn],cnt=0,sz[mxn],comp[mxn],ans=0;
bool marky[mxn];
struct yall{
	int u,uu;
}yal[mxn];
vector<int>v[mxn],act[mxn];
void pre(int z){
	st[z]=++cnt;
	act[s[z]].push_back(z);
	for(int i=1;i<=21;i++)
		par[z][i]=par[par[z][i-1]][i-1];
	for(auto i:v[z]){
		int x=yal[i].u^yal[i].uu^z;
		if(par[z][0]!=x){
			par[x][0]=z;
			pre(x);
		}
	}
	fn[z]=cnt;
}
int lca(int x,int y){
	if(st[x]<=st[y] && fn[x]>=fn[y])return x;
	for(int i=21;i>=0;i--){
		if(par[x][i]==0)continue;
		if(st[par[x][i]]>st[y] || fn[par[x][i]]<st[y])x=par[x][i];
	}
	return par[x][0];
}
void dfs(int z,int y=0){
	int res=0;
	for(auto i:v[z]){
		int x=yal[i].u^yal[i].uu^z;
		if(x!=par[z][0]){
			dfs(x,i);
			num[z]+=num[x];
		}
	}
	if(num[z]==0)
		marky[y]=1;
}
void fds(int z){
	comp[z]=cnt;
	for(auto i:v[z]){
		int x=yal[i].u^yal[i].uu^z;
		if(!comp[x] && !marky[i])
			fds(x);
	}
}
int main(){
	cin>>n>>k;
	for(int i=1;i<n;i++){
		cin>>yal[i].u>>yal[i].uu;
		v[yal[i].u].push_back(i);
		v[yal[i].uu].push_back(i);
	}
	for(int i=1;i<=n;i++)
		cin>>s[i];
	pre(1);
	for(int i=1;i<=k;i++){
			for(int j=0;j<act[i].size();j++){
				num[act[i][j]]++;
				num[lca(act[i][j],act[i][(j+1)%act[i].size()])]--;
			}
	}
	dfs(1);
	cnt=1;
	for(int i=1;i<=n;i++){
		if(!comp[i]){
			fds(i);
			cnt++;
		}
	}
	for(int i=1;i<n;i++){
		if(marky[i]){
			sz[comp[yal[i].u]]++;
			sz[comp[yal[i].uu]]++;
		}
	}
	for(int i=1;i<=cnt;i++){
		if(sz[i]==1)ans++;
	}
	if(ans==1 || cnt<=2)
		cout<<0<<'\n';
	else
		cout<<(ans+1)/2<<endl;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:33:6: warning: unused variable 'res' [-Wunused-variable]
   33 |  int res=0;
      |      ^~~
mergers.cpp: In function 'int main()':
mergers.cpp:63:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |    for(int j=0;j<act[i].size();j++){
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35416 KB Output is correct
2 Correct 7 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 7 ms 33372 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33404 KB Output is correct
9 Correct 7 ms 35516 KB Output is correct
10 Correct 6 ms 35416 KB Output is correct
11 Correct 7 ms 35420 KB Output is correct
12 Correct 8 ms 35672 KB Output is correct
13 Correct 6 ms 35420 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 8 ms 35420 KB Output is correct
16 Correct 7 ms 35672 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 7 ms 35420 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 9 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 7 ms 35420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35416 KB Output is correct
2 Correct 7 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 7 ms 33372 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33404 KB Output is correct
9 Correct 7 ms 35516 KB Output is correct
10 Correct 6 ms 35416 KB Output is correct
11 Correct 7 ms 35420 KB Output is correct
12 Correct 8 ms 35672 KB Output is correct
13 Correct 6 ms 35420 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 8 ms 35420 KB Output is correct
16 Correct 7 ms 35672 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 7 ms 35420 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 9 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 7 ms 35420 KB Output is correct
25 Correct 7 ms 35420 KB Output is correct
26 Correct 9 ms 35932 KB Output is correct
27 Correct 9 ms 35784 KB Output is correct
28 Correct 10 ms 35968 KB Output is correct
29 Correct 9 ms 35932 KB Output is correct
30 Correct 9 ms 35672 KB Output is correct
31 Correct 7 ms 35516 KB Output is correct
32 Correct 10 ms 35932 KB Output is correct
33 Correct 7 ms 35416 KB Output is correct
34 Correct 9 ms 33624 KB Output is correct
35 Correct 9 ms 35768 KB Output is correct
36 Correct 9 ms 35796 KB Output is correct
37 Correct 9 ms 35676 KB Output is correct
38 Correct 8 ms 35416 KB Output is correct
39 Correct 10 ms 35676 KB Output is correct
40 Correct 9 ms 33628 KB Output is correct
41 Correct 9 ms 35768 KB Output is correct
42 Correct 10 ms 35932 KB Output is correct
43 Correct 9 ms 34140 KB Output is correct
44 Correct 7 ms 35420 KB Output is correct
45 Correct 10 ms 35932 KB Output is correct
46 Correct 11 ms 35932 KB Output is correct
47 Correct 6 ms 35420 KB Output is correct
48 Correct 9 ms 35676 KB Output is correct
49 Correct 10 ms 35928 KB Output is correct
50 Correct 9 ms 35928 KB Output is correct
51 Correct 9 ms 35672 KB Output is correct
52 Correct 9 ms 33624 KB Output is correct
53 Correct 9 ms 35712 KB Output is correct
54 Correct 13 ms 33776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35416 KB Output is correct
2 Correct 7 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 7 ms 33372 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33404 KB Output is correct
9 Correct 7 ms 35516 KB Output is correct
10 Correct 6 ms 35416 KB Output is correct
11 Correct 7 ms 35420 KB Output is correct
12 Correct 8 ms 35672 KB Output is correct
13 Correct 6 ms 35420 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 8 ms 35420 KB Output is correct
16 Correct 7 ms 35672 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 7 ms 35420 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 9 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 7 ms 35420 KB Output is correct
25 Correct 7 ms 35420 KB Output is correct
26 Correct 90 ms 47508 KB Output is correct
27 Correct 124 ms 47172 KB Output is correct
28 Correct 9 ms 35676 KB Output is correct
29 Correct 7 ms 35420 KB Output is correct
30 Correct 7 ms 35512 KB Output is correct
31 Correct 110 ms 49180 KB Output is correct
32 Correct 9 ms 33628 KB Output is correct
33 Correct 133 ms 56000 KB Output is correct
34 Correct 116 ms 49424 KB Output is correct
35 Correct 9 ms 35676 KB Output is correct
36 Correct 123 ms 50104 KB Output is correct
37 Correct 10 ms 33624 KB Output is correct
38 Correct 10 ms 35672 KB Output is correct
39 Correct 97 ms 47692 KB Output is correct
40 Correct 9 ms 33880 KB Output is correct
41 Correct 107 ms 47392 KB Output is correct
42 Correct 123 ms 52052 KB Output is correct
43 Correct 7 ms 35420 KB Output is correct
44 Correct 131 ms 56124 KB Output is correct
45 Correct 136 ms 51912 KB Output is correct
46 Correct 9 ms 33624 KB Output is correct
47 Correct 9 ms 33628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 47728 KB Output is correct
2 Correct 113 ms 52428 KB Output is correct
3 Correct 9 ms 35932 KB Output is correct
4 Correct 9 ms 35716 KB Output is correct
5 Correct 8 ms 35416 KB Output is correct
6 Correct 7 ms 35420 KB Output is correct
7 Correct 8 ms 33628 KB Output is correct
8 Correct 130 ms 50340 KB Output is correct
9 Correct 9 ms 35676 KB Output is correct
10 Correct 129 ms 49460 KB Output is correct
11 Correct 7 ms 35472 KB Output is correct
12 Correct 113 ms 49248 KB Output is correct
13 Correct 152 ms 50376 KB Output is correct
14 Correct 127 ms 51852 KB Output is correct
15 Correct 89 ms 47656 KB Output is correct
16 Correct 9 ms 35928 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 111 ms 51832 KB Output is correct
19 Correct 150 ms 56544 KB Output is correct
20 Correct 10 ms 36184 KB Output is correct
21 Correct 7 ms 35420 KB Output is correct
22 Correct 104 ms 50724 KB Output is correct
23 Correct 9 ms 35672 KB Output is correct
24 Correct 123 ms 49364 KB Output is correct
25 Correct 147 ms 55120 KB Output is correct
26 Correct 10 ms 35932 KB Output is correct
27 Correct 10 ms 35928 KB Output is correct
28 Correct 9 ms 35676 KB Output is correct
29 Correct 9 ms 33628 KB Output is correct
30 Correct 9 ms 35932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35416 KB Output is correct
2 Correct 7 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 7 ms 33372 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33404 KB Output is correct
9 Correct 7 ms 35516 KB Output is correct
10 Correct 6 ms 35416 KB Output is correct
11 Correct 7 ms 35420 KB Output is correct
12 Correct 8 ms 35672 KB Output is correct
13 Correct 6 ms 35420 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 8 ms 35420 KB Output is correct
16 Correct 7 ms 35672 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 7 ms 35420 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 9 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 7 ms 35420 KB Output is correct
25 Correct 7 ms 35420 KB Output is correct
26 Correct 9 ms 35932 KB Output is correct
27 Correct 9 ms 35784 KB Output is correct
28 Correct 10 ms 35968 KB Output is correct
29 Correct 9 ms 35932 KB Output is correct
30 Correct 9 ms 35672 KB Output is correct
31 Correct 7 ms 35516 KB Output is correct
32 Correct 10 ms 35932 KB Output is correct
33 Correct 7 ms 35416 KB Output is correct
34 Correct 9 ms 33624 KB Output is correct
35 Correct 9 ms 35768 KB Output is correct
36 Correct 9 ms 35796 KB Output is correct
37 Correct 9 ms 35676 KB Output is correct
38 Correct 8 ms 35416 KB Output is correct
39 Correct 10 ms 35676 KB Output is correct
40 Correct 9 ms 33628 KB Output is correct
41 Correct 9 ms 35768 KB Output is correct
42 Correct 10 ms 35932 KB Output is correct
43 Correct 9 ms 34140 KB Output is correct
44 Correct 7 ms 35420 KB Output is correct
45 Correct 10 ms 35932 KB Output is correct
46 Correct 11 ms 35932 KB Output is correct
47 Correct 6 ms 35420 KB Output is correct
48 Correct 9 ms 35676 KB Output is correct
49 Correct 10 ms 35928 KB Output is correct
50 Correct 9 ms 35928 KB Output is correct
51 Correct 9 ms 35672 KB Output is correct
52 Correct 9 ms 33624 KB Output is correct
53 Correct 9 ms 35712 KB Output is correct
54 Correct 13 ms 33776 KB Output is correct
55 Correct 7 ms 35420 KB Output is correct
56 Correct 90 ms 47508 KB Output is correct
57 Correct 124 ms 47172 KB Output is correct
58 Correct 9 ms 35676 KB Output is correct
59 Correct 7 ms 35420 KB Output is correct
60 Correct 7 ms 35512 KB Output is correct
61 Correct 110 ms 49180 KB Output is correct
62 Correct 9 ms 33628 KB Output is correct
63 Correct 133 ms 56000 KB Output is correct
64 Correct 116 ms 49424 KB Output is correct
65 Correct 9 ms 35676 KB Output is correct
66 Correct 123 ms 50104 KB Output is correct
67 Correct 10 ms 33624 KB Output is correct
68 Correct 10 ms 35672 KB Output is correct
69 Correct 97 ms 47692 KB Output is correct
70 Correct 9 ms 33880 KB Output is correct
71 Correct 107 ms 47392 KB Output is correct
72 Correct 123 ms 52052 KB Output is correct
73 Correct 7 ms 35420 KB Output is correct
74 Correct 131 ms 56124 KB Output is correct
75 Correct 136 ms 51912 KB Output is correct
76 Correct 9 ms 33624 KB Output is correct
77 Correct 9 ms 33628 KB Output is correct
78 Correct 97 ms 47728 KB Output is correct
79 Correct 113 ms 52428 KB Output is correct
80 Correct 9 ms 35932 KB Output is correct
81 Correct 9 ms 35716 KB Output is correct
82 Correct 8 ms 35416 KB Output is correct
83 Correct 7 ms 35420 KB Output is correct
84 Correct 8 ms 33628 KB Output is correct
85 Correct 130 ms 50340 KB Output is correct
86 Correct 9 ms 35676 KB Output is correct
87 Correct 129 ms 49460 KB Output is correct
88 Correct 7 ms 35472 KB Output is correct
89 Correct 113 ms 49248 KB Output is correct
90 Correct 152 ms 50376 KB Output is correct
91 Correct 127 ms 51852 KB Output is correct
92 Correct 89 ms 47656 KB Output is correct
93 Correct 9 ms 35928 KB Output is correct
94 Correct 7 ms 35420 KB Output is correct
95 Correct 111 ms 51832 KB Output is correct
96 Correct 150 ms 56544 KB Output is correct
97 Correct 10 ms 36184 KB Output is correct
98 Correct 7 ms 35420 KB Output is correct
99 Correct 104 ms 50724 KB Output is correct
100 Correct 9 ms 35672 KB Output is correct
101 Correct 123 ms 49364 KB Output is correct
102 Correct 147 ms 55120 KB Output is correct
103 Correct 10 ms 35932 KB Output is correct
104 Correct 10 ms 35928 KB Output is correct
105 Correct 9 ms 35676 KB Output is correct
106 Correct 9 ms 33628 KB Output is correct
107 Correct 9 ms 35932 KB Output is correct
108 Correct 697 ms 105324 KB Output is correct
109 Correct 1037 ms 121144 KB Output is correct
110 Correct 1061 ms 134848 KB Output is correct
111 Correct 1094 ms 142512 KB Output is correct
112 Correct 1139 ms 130076 KB Output is correct
113 Correct 805 ms 114280 KB Output is correct
114 Correct 821 ms 99444 KB Output is correct
115 Correct 802 ms 99268 KB Output is correct
116 Correct 961 ms 101560 KB Output is correct
117 Correct 862 ms 114868 KB Output is correct
118 Correct 843 ms 99156 KB Output is correct
119 Correct 854 ms 114772 KB Output is correct
120 Correct 1081 ms 131812 KB Output is correct
121 Correct 899 ms 115024 KB Output is correct
122 Correct 977 ms 106856 KB Output is correct
123 Correct 661 ms 116384 KB Output is correct
124 Correct 1101 ms 113060 KB Output is correct