Submission #899870

# Submission time Handle Problem Language Result Execution time Memory
899870 2024-01-07T08:00:46 Z PM1 Mergers (JOI19_mergers) C++17
100 / 100
1218 ms 142420 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++){
			int x=act[i][0];
			num[x]--;
			for(int j=1;j<act[i].size();j++){
				num[act[i][j]]--;
				x=lca(x,act[i][j]);
				num[x]++;
			}
			num[x]++;
	}
	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:65:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |    for(int j=1;j<act[i].size();j++){
      |                ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 35420 KB Output is correct
2 Correct 8 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 7 ms 35420 KB Output is correct
5 Correct 7 ms 33372 KB Output is correct
6 Correct 7 ms 33416 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 7 ms 35420 KB Output is correct
10 Correct 6 ms 35420 KB Output is correct
11 Correct 7 ms 35416 KB Output is correct
12 Correct 7 ms 35420 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 7 ms 35420 KB Output is correct
15 Correct 7 ms 35420 KB Output is correct
16 Correct 7 ms 35420 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 7 ms 33372 KB Output is correct
19 Correct 7 ms 35488 KB Output is correct
20 Correct 6 ms 35464 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 6 ms 33372 KB Output is correct
23 Correct 8 ms 33372 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 35420 KB Output is correct
2 Correct 8 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 7 ms 35420 KB Output is correct
5 Correct 7 ms 33372 KB Output is correct
6 Correct 7 ms 33416 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 7 ms 35420 KB Output is correct
10 Correct 6 ms 35420 KB Output is correct
11 Correct 7 ms 35416 KB Output is correct
12 Correct 7 ms 35420 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 7 ms 35420 KB Output is correct
15 Correct 7 ms 35420 KB Output is correct
16 Correct 7 ms 35420 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 7 ms 33372 KB Output is correct
19 Correct 7 ms 35488 KB Output is correct
20 Correct 6 ms 35464 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 6 ms 33372 KB Output is correct
23 Correct 8 ms 33372 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 9 ms 35416 KB Output is correct
26 Correct 9 ms 35932 KB Output is correct
27 Correct 11 ms 35676 KB Output is correct
28 Correct 10 ms 35932 KB Output is correct
29 Correct 10 ms 35932 KB Output is correct
30 Correct 11 ms 35676 KB Output is correct
31 Correct 7 ms 35420 KB Output is correct
32 Correct 10 ms 35932 KB Output is correct
33 Correct 6 ms 35420 KB Output is correct
34 Correct 9 ms 33628 KB Output is correct
35 Correct 9 ms 35932 KB Output is correct
36 Correct 9 ms 35856 KB Output is correct
37 Correct 9 ms 35676 KB Output is correct
38 Correct 7 ms 35420 KB Output is correct
39 Correct 9 ms 35764 KB Output is correct
40 Correct 9 ms 33852 KB Output is correct
41 Correct 9 ms 35672 KB Output is correct
42 Correct 9 ms 35932 KB Output is correct
43 Correct 9 ms 34044 KB Output is correct
44 Correct 7 ms 35668 KB Output is correct
45 Correct 10 ms 35932 KB Output is correct
46 Correct 10 ms 35676 KB Output is correct
47 Correct 8 ms 35420 KB Output is correct
48 Correct 9 ms 35724 KB Output is correct
49 Correct 9 ms 35928 KB Output is correct
50 Correct 9 ms 35928 KB Output is correct
51 Correct 9 ms 35676 KB Output is correct
52 Correct 9 ms 33628 KB Output is correct
53 Correct 9 ms 35932 KB Output is correct
54 Correct 9 ms 33844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 35420 KB Output is correct
2 Correct 8 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 7 ms 35420 KB Output is correct
5 Correct 7 ms 33372 KB Output is correct
6 Correct 7 ms 33416 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 7 ms 35420 KB Output is correct
10 Correct 6 ms 35420 KB Output is correct
11 Correct 7 ms 35416 KB Output is correct
12 Correct 7 ms 35420 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 7 ms 35420 KB Output is correct
15 Correct 7 ms 35420 KB Output is correct
16 Correct 7 ms 35420 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 7 ms 33372 KB Output is correct
19 Correct 7 ms 35488 KB Output is correct
20 Correct 6 ms 35464 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 6 ms 33372 KB Output is correct
23 Correct 8 ms 33372 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 7 ms 35420 KB Output is correct
26 Correct 91 ms 47712 KB Output is correct
27 Correct 112 ms 47184 KB Output is correct
28 Correct 11 ms 35672 KB Output is correct
29 Correct 7 ms 35420 KB Output is correct
30 Correct 7 ms 35420 KB Output is correct
31 Correct 111 ms 49296 KB Output is correct
32 Correct 9 ms 33628 KB Output is correct
33 Correct 159 ms 55940 KB Output is correct
34 Correct 108 ms 49236 KB Output is correct
35 Correct 9 ms 35676 KB Output is correct
36 Correct 124 ms 50260 KB Output is correct
37 Correct 9 ms 33624 KB Output is correct
38 Correct 11 ms 35676 KB Output is correct
39 Correct 93 ms 47664 KB Output is correct
40 Correct 9 ms 33884 KB Output is correct
41 Correct 106 ms 47300 KB Output is correct
42 Correct 147 ms 52052 KB Output is correct
43 Correct 7 ms 35416 KB Output is correct
44 Correct 132 ms 56320 KB Output is correct
45 Correct 128 ms 51792 KB Output is correct
46 Correct 9 ms 33628 KB Output is correct
47 Correct 9 ms 33624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 47704 KB Output is correct
2 Correct 113 ms 52152 KB Output is correct
3 Correct 10 ms 35928 KB Output is correct
4 Correct 9 ms 35676 KB Output is correct
5 Correct 8 ms 35420 KB Output is correct
6 Correct 7 ms 35420 KB Output is correct
7 Correct 9 ms 33628 KB Output is correct
8 Correct 150 ms 50452 KB Output is correct
9 Correct 10 ms 35676 KB Output is correct
10 Correct 111 ms 49212 KB Output is correct
11 Correct 7 ms 35416 KB Output is correct
12 Correct 128 ms 49260 KB Output is correct
13 Correct 129 ms 50304 KB Output is correct
14 Correct 136 ms 52028 KB Output is correct
15 Correct 102 ms 47800 KB Output is correct
16 Correct 10 ms 35928 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 118 ms 51812 KB Output is correct
19 Correct 185 ms 56764 KB Output is correct
20 Correct 12 ms 35672 KB Output is correct
21 Correct 7 ms 35476 KB Output is correct
22 Correct 109 ms 50632 KB Output is correct
23 Correct 9 ms 35672 KB Output is correct
24 Correct 133 ms 49416 KB Output is correct
25 Correct 156 ms 55128 KB Output is correct
26 Correct 9 ms 35928 KB Output is correct
27 Correct 11 ms 35932 KB Output is correct
28 Correct 9 ms 35676 KB Output is correct
29 Correct 9 ms 33712 KB Output is correct
30 Correct 9 ms 35932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 35420 KB Output is correct
2 Correct 8 ms 35420 KB Output is correct
3 Correct 7 ms 35420 KB Output is correct
4 Correct 7 ms 35420 KB Output is correct
5 Correct 7 ms 33372 KB Output is correct
6 Correct 7 ms 33416 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 7 ms 35420 KB Output is correct
10 Correct 6 ms 35420 KB Output is correct
11 Correct 7 ms 35416 KB Output is correct
12 Correct 7 ms 35420 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 7 ms 35420 KB Output is correct
15 Correct 7 ms 35420 KB Output is correct
16 Correct 7 ms 35420 KB Output is correct
17 Correct 7 ms 35420 KB Output is correct
18 Correct 7 ms 33372 KB Output is correct
19 Correct 7 ms 35488 KB Output is correct
20 Correct 6 ms 35464 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 6 ms 33372 KB Output is correct
23 Correct 8 ms 33372 KB Output is correct
24 Correct 7 ms 35416 KB Output is correct
25 Correct 9 ms 35416 KB Output is correct
26 Correct 9 ms 35932 KB Output is correct
27 Correct 11 ms 35676 KB Output is correct
28 Correct 10 ms 35932 KB Output is correct
29 Correct 10 ms 35932 KB Output is correct
30 Correct 11 ms 35676 KB Output is correct
31 Correct 7 ms 35420 KB Output is correct
32 Correct 10 ms 35932 KB Output is correct
33 Correct 6 ms 35420 KB Output is correct
34 Correct 9 ms 33628 KB Output is correct
35 Correct 9 ms 35932 KB Output is correct
36 Correct 9 ms 35856 KB Output is correct
37 Correct 9 ms 35676 KB Output is correct
38 Correct 7 ms 35420 KB Output is correct
39 Correct 9 ms 35764 KB Output is correct
40 Correct 9 ms 33852 KB Output is correct
41 Correct 9 ms 35672 KB Output is correct
42 Correct 9 ms 35932 KB Output is correct
43 Correct 9 ms 34044 KB Output is correct
44 Correct 7 ms 35668 KB Output is correct
45 Correct 10 ms 35932 KB Output is correct
46 Correct 10 ms 35676 KB Output is correct
47 Correct 8 ms 35420 KB Output is correct
48 Correct 9 ms 35724 KB Output is correct
49 Correct 9 ms 35928 KB Output is correct
50 Correct 9 ms 35928 KB Output is correct
51 Correct 9 ms 35676 KB Output is correct
52 Correct 9 ms 33628 KB Output is correct
53 Correct 9 ms 35932 KB Output is correct
54 Correct 9 ms 33844 KB Output is correct
55 Correct 7 ms 35420 KB Output is correct
56 Correct 91 ms 47712 KB Output is correct
57 Correct 112 ms 47184 KB Output is correct
58 Correct 11 ms 35672 KB Output is correct
59 Correct 7 ms 35420 KB Output is correct
60 Correct 7 ms 35420 KB Output is correct
61 Correct 111 ms 49296 KB Output is correct
62 Correct 9 ms 33628 KB Output is correct
63 Correct 159 ms 55940 KB Output is correct
64 Correct 108 ms 49236 KB Output is correct
65 Correct 9 ms 35676 KB Output is correct
66 Correct 124 ms 50260 KB Output is correct
67 Correct 9 ms 33624 KB Output is correct
68 Correct 11 ms 35676 KB Output is correct
69 Correct 93 ms 47664 KB Output is correct
70 Correct 9 ms 33884 KB Output is correct
71 Correct 106 ms 47300 KB Output is correct
72 Correct 147 ms 52052 KB Output is correct
73 Correct 7 ms 35416 KB Output is correct
74 Correct 132 ms 56320 KB Output is correct
75 Correct 128 ms 51792 KB Output is correct
76 Correct 9 ms 33628 KB Output is correct
77 Correct 9 ms 33624 KB Output is correct
78 Correct 99 ms 47704 KB Output is correct
79 Correct 113 ms 52152 KB Output is correct
80 Correct 10 ms 35928 KB Output is correct
81 Correct 9 ms 35676 KB Output is correct
82 Correct 8 ms 35420 KB Output is correct
83 Correct 7 ms 35420 KB Output is correct
84 Correct 9 ms 33628 KB Output is correct
85 Correct 150 ms 50452 KB Output is correct
86 Correct 10 ms 35676 KB Output is correct
87 Correct 111 ms 49212 KB Output is correct
88 Correct 7 ms 35416 KB Output is correct
89 Correct 128 ms 49260 KB Output is correct
90 Correct 129 ms 50304 KB Output is correct
91 Correct 136 ms 52028 KB Output is correct
92 Correct 102 ms 47800 KB Output is correct
93 Correct 10 ms 35928 KB Output is correct
94 Correct 7 ms 35420 KB Output is correct
95 Correct 118 ms 51812 KB Output is correct
96 Correct 185 ms 56764 KB Output is correct
97 Correct 12 ms 35672 KB Output is correct
98 Correct 7 ms 35476 KB Output is correct
99 Correct 109 ms 50632 KB Output is correct
100 Correct 9 ms 35672 KB Output is correct
101 Correct 133 ms 49416 KB Output is correct
102 Correct 156 ms 55128 KB Output is correct
103 Correct 9 ms 35928 KB Output is correct
104 Correct 11 ms 35932 KB Output is correct
105 Correct 9 ms 35676 KB Output is correct
106 Correct 9 ms 33712 KB Output is correct
107 Correct 9 ms 35932 KB Output is correct
108 Correct 702 ms 105476 KB Output is correct
109 Correct 1218 ms 121144 KB Output is correct
110 Correct 1191 ms 134848 KB Output is correct
111 Correct 1139 ms 142420 KB Output is correct
112 Correct 1205 ms 130124 KB Output is correct
113 Correct 688 ms 114292 KB Output is correct
114 Correct 895 ms 99460 KB Output is correct
115 Correct 879 ms 99276 KB Output is correct
116 Correct 1087 ms 101512 KB Output is correct
117 Correct 1041 ms 114900 KB Output is correct
118 Correct 876 ms 99356 KB Output is correct
119 Correct 920 ms 114812 KB Output is correct
120 Correct 1106 ms 131676 KB Output is correct
121 Correct 962 ms 115024 KB Output is correct
122 Correct 1000 ms 107148 KB Output is correct
123 Correct 723 ms 116356 KB Output is correct
124 Correct 1116 ms 113060 KB Output is correct