답안 #899865

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
899865 2024-01-07T07:47:43 Z PM1 Mergers (JOI19_mergers) C++17
0 / 100
115 ms 50068 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);
		}
	}
	if(act[s[z]].back()==z)num[z]--;
	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++){
			int x=act[i][0];
			for(int j=1;j<act[i].size();j++){
				x=lca(x,act[i][j]);
				num[x]++;
			}
	}
	dfs(1);
	cout<<(ans+1)/2<<endl;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |    for(int j=1;j<act[i].size();j++){
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 47732 KB Output is correct
2 Incorrect 115 ms 50068 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -