Submission #899866

# Submission time Handle Problem Language Result Execution time Memory
899866 2024-01-07T07:51:54 Z PM1 Mergers (JOI19_mergers) C++17
100 / 100
1107 ms 145228 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];
}
int 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]){
			res+=dfs(x,i);
			num[z]+=num[x];
		}
	}
	if(z==1){
		ans+=(res==1);
	}
	else{
		if(num[z]==0){
			ans+=(res==0);
			res=1;
		}
	}
	return res;
}
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);
	cout<<(ans+1)/2<<endl;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:71:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |    for(int j=0;j<act[i].size();j++){
      |                ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 7 ms 33480 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33420 KB Output is correct
8 Correct 8 ms 33368 KB Output is correct
9 Correct 7 ms 33372 KB Output is correct
10 Correct 7 ms 33372 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 7 ms 33372 KB Output is correct
13 Correct 7 ms 33372 KB Output is correct
14 Correct 7 ms 33372 KB Output is correct
15 Correct 7 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 7 ms 33372 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 33232 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 7 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 6 ms 33468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 7 ms 33480 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33420 KB Output is correct
8 Correct 8 ms 33368 KB Output is correct
9 Correct 7 ms 33372 KB Output is correct
10 Correct 7 ms 33372 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 7 ms 33372 KB Output is correct
13 Correct 7 ms 33372 KB Output is correct
14 Correct 7 ms 33372 KB Output is correct
15 Correct 7 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 7 ms 33372 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 33232 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 7 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 6 ms 33468 KB Output is correct
25 Correct 7 ms 33436 KB Output is correct
26 Correct 9 ms 33724 KB Output is correct
27 Correct 9 ms 33884 KB Output is correct
28 Correct 9 ms 33884 KB Output is correct
29 Correct 9 ms 33836 KB Output is correct
30 Correct 9 ms 33884 KB Output is correct
31 Correct 6 ms 33372 KB Output is correct
32 Correct 9 ms 33880 KB Output is correct
33 Correct 7 ms 33372 KB Output is correct
34 Correct 9 ms 33884 KB Output is correct
35 Correct 9 ms 33884 KB Output is correct
36 Correct 9 ms 33628 KB Output is correct
37 Correct 9 ms 33884 KB Output is correct
38 Correct 7 ms 33372 KB Output is correct
39 Correct 9 ms 33884 KB Output is correct
40 Correct 9 ms 33884 KB Output is correct
41 Correct 13 ms 33884 KB Output is correct
42 Correct 9 ms 33884 KB Output is correct
43 Correct 9 ms 33940 KB Output is correct
44 Correct 7 ms 33372 KB Output is correct
45 Correct 9 ms 33884 KB Output is correct
46 Correct 9 ms 33700 KB Output is correct
47 Correct 7 ms 33372 KB Output is correct
48 Correct 10 ms 33744 KB Output is correct
49 Correct 9 ms 33880 KB Output is correct
50 Correct 9 ms 33884 KB Output is correct
51 Correct 9 ms 33880 KB Output is correct
52 Correct 8 ms 33884 KB Output is correct
53 Correct 9 ms 33884 KB Output is correct
54 Correct 9 ms 33732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 7 ms 33480 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33420 KB Output is correct
8 Correct 8 ms 33368 KB Output is correct
9 Correct 7 ms 33372 KB Output is correct
10 Correct 7 ms 33372 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 7 ms 33372 KB Output is correct
13 Correct 7 ms 33372 KB Output is correct
14 Correct 7 ms 33372 KB Output is correct
15 Correct 7 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 7 ms 33372 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 33232 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 7 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 6 ms 33468 KB Output is correct
25 Correct 7 ms 33412 KB Output is correct
26 Correct 101 ms 49188 KB Output is correct
27 Correct 126 ms 48792 KB Output is correct
28 Correct 9 ms 33884 KB Output is correct
29 Correct 7 ms 33464 KB Output is correct
30 Correct 7 ms 33372 KB Output is correct
31 Correct 111 ms 48516 KB Output is correct
32 Correct 9 ms 33884 KB Output is correct
33 Correct 123 ms 55908 KB Output is correct
34 Correct 125 ms 48752 KB Output is correct
35 Correct 9 ms 33880 KB Output is correct
36 Correct 135 ms 49312 KB Output is correct
37 Correct 9 ms 33884 KB Output is correct
38 Correct 9 ms 33704 KB Output is correct
39 Correct 91 ms 48972 KB Output is correct
40 Correct 9 ms 33880 KB Output is correct
41 Correct 107 ms 48576 KB Output is correct
42 Correct 123 ms 51044 KB Output is correct
43 Correct 6 ms 33372 KB Output is correct
44 Correct 134 ms 56124 KB Output is correct
45 Correct 129 ms 52564 KB Output is correct
46 Correct 9 ms 33884 KB Output is correct
47 Correct 9 ms 33708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 47840 KB Output is correct
2 Correct 111 ms 50120 KB Output is correct
3 Correct 9 ms 33884 KB Output is correct
4 Correct 9 ms 33880 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 9 ms 33664 KB Output is correct
8 Correct 151 ms 50120 KB Output is correct
9 Correct 10 ms 33884 KB Output is correct
10 Correct 117 ms 48720 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 133 ms 48620 KB Output is correct
13 Correct 125 ms 49892 KB Output is correct
14 Correct 142 ms 51852 KB Output is correct
15 Correct 92 ms 48968 KB Output is correct
16 Correct 9 ms 33884 KB Output is correct
17 Correct 7 ms 33372 KB Output is correct
18 Correct 109 ms 51504 KB Output is correct
19 Correct 158 ms 55632 KB Output is correct
20 Correct 9 ms 33884 KB Output is correct
21 Correct 6 ms 33368 KB Output is correct
22 Correct 120 ms 50124 KB Output is correct
23 Correct 9 ms 33884 KB Output is correct
24 Correct 123 ms 49004 KB Output is correct
25 Correct 163 ms 54240 KB Output is correct
26 Correct 9 ms 33892 KB Output is correct
27 Correct 9 ms 33884 KB Output is correct
28 Correct 9 ms 34080 KB Output is correct
29 Correct 9 ms 33728 KB Output is correct
30 Correct 10 ms 33884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 7 ms 33480 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33420 KB Output is correct
8 Correct 8 ms 33368 KB Output is correct
9 Correct 7 ms 33372 KB Output is correct
10 Correct 7 ms 33372 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 7 ms 33372 KB Output is correct
13 Correct 7 ms 33372 KB Output is correct
14 Correct 7 ms 33372 KB Output is correct
15 Correct 7 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 7 ms 33372 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 33232 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 7 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 6 ms 33468 KB Output is correct
25 Correct 7 ms 33436 KB Output is correct
26 Correct 9 ms 33724 KB Output is correct
27 Correct 9 ms 33884 KB Output is correct
28 Correct 9 ms 33884 KB Output is correct
29 Correct 9 ms 33836 KB Output is correct
30 Correct 9 ms 33884 KB Output is correct
31 Correct 6 ms 33372 KB Output is correct
32 Correct 9 ms 33880 KB Output is correct
33 Correct 7 ms 33372 KB Output is correct
34 Correct 9 ms 33884 KB Output is correct
35 Correct 9 ms 33884 KB Output is correct
36 Correct 9 ms 33628 KB Output is correct
37 Correct 9 ms 33884 KB Output is correct
38 Correct 7 ms 33372 KB Output is correct
39 Correct 9 ms 33884 KB Output is correct
40 Correct 9 ms 33884 KB Output is correct
41 Correct 13 ms 33884 KB Output is correct
42 Correct 9 ms 33884 KB Output is correct
43 Correct 9 ms 33940 KB Output is correct
44 Correct 7 ms 33372 KB Output is correct
45 Correct 9 ms 33884 KB Output is correct
46 Correct 9 ms 33700 KB Output is correct
47 Correct 7 ms 33372 KB Output is correct
48 Correct 10 ms 33744 KB Output is correct
49 Correct 9 ms 33880 KB Output is correct
50 Correct 9 ms 33884 KB Output is correct
51 Correct 9 ms 33880 KB Output is correct
52 Correct 8 ms 33884 KB Output is correct
53 Correct 9 ms 33884 KB Output is correct
54 Correct 9 ms 33732 KB Output is correct
55 Correct 7 ms 33412 KB Output is correct
56 Correct 101 ms 49188 KB Output is correct
57 Correct 126 ms 48792 KB Output is correct
58 Correct 9 ms 33884 KB Output is correct
59 Correct 7 ms 33464 KB Output is correct
60 Correct 7 ms 33372 KB Output is correct
61 Correct 111 ms 48516 KB Output is correct
62 Correct 9 ms 33884 KB Output is correct
63 Correct 123 ms 55908 KB Output is correct
64 Correct 125 ms 48752 KB Output is correct
65 Correct 9 ms 33880 KB Output is correct
66 Correct 135 ms 49312 KB Output is correct
67 Correct 9 ms 33884 KB Output is correct
68 Correct 9 ms 33704 KB Output is correct
69 Correct 91 ms 48972 KB Output is correct
70 Correct 9 ms 33880 KB Output is correct
71 Correct 107 ms 48576 KB Output is correct
72 Correct 123 ms 51044 KB Output is correct
73 Correct 6 ms 33372 KB Output is correct
74 Correct 134 ms 56124 KB Output is correct
75 Correct 129 ms 52564 KB Output is correct
76 Correct 9 ms 33884 KB Output is correct
77 Correct 9 ms 33708 KB Output is correct
78 Correct 90 ms 47840 KB Output is correct
79 Correct 111 ms 50120 KB Output is correct
80 Correct 9 ms 33884 KB Output is correct
81 Correct 9 ms 33880 KB Output is correct
82 Correct 6 ms 33372 KB Output is correct
83 Correct 6 ms 33372 KB Output is correct
84 Correct 9 ms 33664 KB Output is correct
85 Correct 151 ms 50120 KB Output is correct
86 Correct 10 ms 33884 KB Output is correct
87 Correct 117 ms 48720 KB Output is correct
88 Correct 6 ms 33372 KB Output is correct
89 Correct 133 ms 48620 KB Output is correct
90 Correct 125 ms 49892 KB Output is correct
91 Correct 142 ms 51852 KB Output is correct
92 Correct 92 ms 48968 KB Output is correct
93 Correct 9 ms 33884 KB Output is correct
94 Correct 7 ms 33372 KB Output is correct
95 Correct 109 ms 51504 KB Output is correct
96 Correct 158 ms 55632 KB Output is correct
97 Correct 9 ms 33884 KB Output is correct
98 Correct 6 ms 33368 KB Output is correct
99 Correct 120 ms 50124 KB Output is correct
100 Correct 9 ms 33884 KB Output is correct
101 Correct 123 ms 49004 KB Output is correct
102 Correct 163 ms 54240 KB Output is correct
103 Correct 9 ms 33892 KB Output is correct
104 Correct 9 ms 33884 KB Output is correct
105 Correct 9 ms 34080 KB Output is correct
106 Correct 9 ms 33728 KB Output is correct
107 Correct 10 ms 33884 KB Output is correct
108 Correct 688 ms 114172 KB Output is correct
109 Correct 950 ms 124124 KB Output is correct
110 Correct 999 ms 135304 KB Output is correct
111 Correct 1096 ms 145228 KB Output is correct
112 Correct 1107 ms 135084 KB Output is correct
113 Correct 623 ms 121916 KB Output is correct
114 Correct 787 ms 106044 KB Output is correct
115 Correct 771 ms 106176 KB Output is correct
116 Correct 874 ms 110156 KB Output is correct
117 Correct 922 ms 122056 KB Output is correct
118 Correct 752 ms 106832 KB Output is correct
119 Correct 839 ms 121940 KB Output is correct
120 Correct 1006 ms 135760 KB Output is correct
121 Correct 843 ms 121924 KB Output is correct
122 Correct 941 ms 115952 KB Output is correct
123 Correct 636 ms 123328 KB Output is correct
124 Correct 986 ms 120588 KB Output is correct