답안 #120624

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120624 2019-06-25T06:09:38 Z 임유진(#2961) Mergers (JOI19_mergers) C++14
10 / 100
162 ms 55920 KB
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

#define MAXN 500005

int A[MAXN], B[MAXN], S[MAXN];
vector<int> e[MAXN];
int par[20][MAXN], dep[MAXN];
vector<int> ss[MAXN];
bool chkn[MAXN], chks[MAXN];
queue<int> q;
int gs[MAXN];
vector<int> ge[MAXN];

void dfs(int x){
	for(auto a:e[x]) if(a!=par[0][x]){
		par[0][a]=x;
		dep[a]=dep[x]+1;
		dfs(a);
	}
}

int anc(int x, int d){
	if(d>dep[x]) return 0;
	for(int i=19; i>=0; i--) if((dep[x]-d)&(1<<i)) x=par[i][x];
	return x;
}

int lca(int x, int y){
	if(dep[x]>dep[y]) x=anc(x, dep[y]);
	else y=anc(y, dep[x]);
	if(x==y) return x;
	for(int i=19; i>=0; i--) if(par[i][x]!=par[i][y]){
		x=par[i][x];
		y=par[i][y];
	}
	return par[0][x];
}

int main(){
	int N, K;
	int ans=0;

	scanf("%d%d", &N, &K);
	for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
	for(int i=1; i<=N; i++) scanf("%d", S+i);

	for(int i=0; i<N-1; i++){
		e[A[i]].push_back(B[i]);
		e[B[i]].push_back(A[i]);
	}
	par[0][1]=1;
	dfs(1);
	for(int i=1; i<20; i++) for(int j=1; j<=N; j++) par[i][j]=par[i-1][par[i-1][j]];
	//for(int i=1; i<=N; i++) printf("%d %d %d\n", par[0][i], par[1][i], par[2][i]);
	for(int i=1; i<=N; i++) ss[S[i]].push_back(i);
	//for(int i=1; i<=K; i++) printf("%d\n", ss[i].size());
	
	for(int i=1; i<=K; i++) if(!chks[i]){
		chks[i]=true;
		q.push(i);
		//printf("[%d]\n", i);
		while(!q.empty()){
			int t=q.front();
			q.pop();
			gs[t]=i;
			int l=ss[t][0];
			for(auto a:ss[t]) l=lca(l, a);
			//printf("lca=%d\n", l);
			for(auto a:ss[t]) for(int j=a; dep[j]>=dep[l]&&!chkn[j]; j=par[0][j]){
				chkn[j]=true;
				if(!chks[S[j]]){
					chks[S[j]]=true;
					q.push(S[j]);
				}
			}
		}
	}

	//for(int i=1; i<=K; i++) printf("%d ", gs[i]);

	for(int i=0; i<N-1; i++) if(gs[S[A[i]]]!=gs[S[B[i]]]){
		ge[gs[S[A[i]]]].push_back(gs[S[B[i]]]);
		ge[gs[S[B[i]]]].push_back(gs[S[A[i]]]);
		//printf("(%d %d)\n", S[A[i]], S[B[i]]);
	}
	for(int i=1; i<=K; i++){
		sort(ge[i].begin(), ge[i].end());
		unique(ge[i].begin(), ge[i].end());
		if(ge[i].size()==1) ans++;
	}

	//printf("\n%d\n", ans);
	printf("%d", (ans+1)/2);
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:49:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
                           ~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:50:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=N; i++) scanf("%d", S+i);
                          ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35704 KB Output is correct
2 Correct 36 ms 35712 KB Output is correct
3 Correct 34 ms 35832 KB Output is correct
4 Correct 33 ms 35704 KB Output is correct
5 Correct 35 ms 35712 KB Output is correct
6 Correct 33 ms 35744 KB Output is correct
7 Correct 34 ms 35712 KB Output is correct
8 Correct 60 ms 35704 KB Output is correct
9 Correct 34 ms 35684 KB Output is correct
10 Correct 34 ms 35840 KB Output is correct
11 Correct 33 ms 35696 KB Output is correct
12 Correct 34 ms 35840 KB Output is correct
13 Correct 32 ms 35712 KB Output is correct
14 Correct 33 ms 35704 KB Output is correct
15 Correct 33 ms 35812 KB Output is correct
16 Correct 33 ms 35712 KB Output is correct
17 Correct 34 ms 35712 KB Output is correct
18 Correct 34 ms 35704 KB Output is correct
19 Correct 42 ms 35704 KB Output is correct
20 Correct 51 ms 35832 KB Output is correct
21 Correct 33 ms 35724 KB Output is correct
22 Correct 33 ms 35712 KB Output is correct
23 Correct 34 ms 35704 KB Output is correct
24 Correct 32 ms 35704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35704 KB Output is correct
2 Correct 36 ms 35712 KB Output is correct
3 Correct 34 ms 35832 KB Output is correct
4 Correct 33 ms 35704 KB Output is correct
5 Correct 35 ms 35712 KB Output is correct
6 Correct 33 ms 35744 KB Output is correct
7 Correct 34 ms 35712 KB Output is correct
8 Correct 60 ms 35704 KB Output is correct
9 Correct 34 ms 35684 KB Output is correct
10 Correct 34 ms 35840 KB Output is correct
11 Correct 33 ms 35696 KB Output is correct
12 Correct 34 ms 35840 KB Output is correct
13 Correct 32 ms 35712 KB Output is correct
14 Correct 33 ms 35704 KB Output is correct
15 Correct 33 ms 35812 KB Output is correct
16 Correct 33 ms 35712 KB Output is correct
17 Correct 34 ms 35712 KB Output is correct
18 Correct 34 ms 35704 KB Output is correct
19 Correct 42 ms 35704 KB Output is correct
20 Correct 51 ms 35832 KB Output is correct
21 Correct 33 ms 35724 KB Output is correct
22 Correct 33 ms 35712 KB Output is correct
23 Correct 34 ms 35704 KB Output is correct
24 Correct 32 ms 35704 KB Output is correct
25 Correct 32 ms 35712 KB Output is correct
26 Incorrect 34 ms 36216 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35704 KB Output is correct
2 Correct 36 ms 35712 KB Output is correct
3 Correct 34 ms 35832 KB Output is correct
4 Correct 33 ms 35704 KB Output is correct
5 Correct 35 ms 35712 KB Output is correct
6 Correct 33 ms 35744 KB Output is correct
7 Correct 34 ms 35712 KB Output is correct
8 Correct 60 ms 35704 KB Output is correct
9 Correct 34 ms 35684 KB Output is correct
10 Correct 34 ms 35840 KB Output is correct
11 Correct 33 ms 35696 KB Output is correct
12 Correct 34 ms 35840 KB Output is correct
13 Correct 32 ms 35712 KB Output is correct
14 Correct 33 ms 35704 KB Output is correct
15 Correct 33 ms 35812 KB Output is correct
16 Correct 33 ms 35712 KB Output is correct
17 Correct 34 ms 35712 KB Output is correct
18 Correct 34 ms 35704 KB Output is correct
19 Correct 42 ms 35704 KB Output is correct
20 Correct 51 ms 35832 KB Output is correct
21 Correct 33 ms 35724 KB Output is correct
22 Correct 33 ms 35712 KB Output is correct
23 Correct 34 ms 35704 KB Output is correct
24 Correct 32 ms 35704 KB Output is correct
25 Correct 31 ms 35712 KB Output is correct
26 Correct 95 ms 50324 KB Output is correct
27 Correct 109 ms 48892 KB Output is correct
28 Correct 33 ms 36096 KB Output is correct
29 Correct 32 ms 35704 KB Output is correct
30 Correct 32 ms 35840 KB Output is correct
31 Correct 110 ms 48888 KB Output is correct
32 Correct 34 ms 36344 KB Output is correct
33 Correct 149 ms 53240 KB Output is correct
34 Correct 112 ms 49016 KB Output is correct
35 Correct 34 ms 36216 KB Output is correct
36 Correct 122 ms 49272 KB Output is correct
37 Correct 33 ms 36088 KB Output is correct
38 Correct 34 ms 36224 KB Output is correct
39 Correct 95 ms 50036 KB Output is correct
40 Correct 34 ms 36216 KB Output is correct
41 Correct 115 ms 49116 KB Output is correct
42 Correct 162 ms 50296 KB Output is correct
43 Incorrect 34 ms 35832 KB Output isn't correct
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 50392 KB Output is correct
2 Correct 149 ms 55920 KB Output is correct
3 Incorrect 37 ms 36352 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35704 KB Output is correct
2 Correct 36 ms 35712 KB Output is correct
3 Correct 34 ms 35832 KB Output is correct
4 Correct 33 ms 35704 KB Output is correct
5 Correct 35 ms 35712 KB Output is correct
6 Correct 33 ms 35744 KB Output is correct
7 Correct 34 ms 35712 KB Output is correct
8 Correct 60 ms 35704 KB Output is correct
9 Correct 34 ms 35684 KB Output is correct
10 Correct 34 ms 35840 KB Output is correct
11 Correct 33 ms 35696 KB Output is correct
12 Correct 34 ms 35840 KB Output is correct
13 Correct 32 ms 35712 KB Output is correct
14 Correct 33 ms 35704 KB Output is correct
15 Correct 33 ms 35812 KB Output is correct
16 Correct 33 ms 35712 KB Output is correct
17 Correct 34 ms 35712 KB Output is correct
18 Correct 34 ms 35704 KB Output is correct
19 Correct 42 ms 35704 KB Output is correct
20 Correct 51 ms 35832 KB Output is correct
21 Correct 33 ms 35724 KB Output is correct
22 Correct 33 ms 35712 KB Output is correct
23 Correct 34 ms 35704 KB Output is correct
24 Correct 32 ms 35704 KB Output is correct
25 Correct 32 ms 35712 KB Output is correct
26 Incorrect 34 ms 36216 KB Output isn't correct
27 Halted 0 ms 0 KB -