Submission #209058

# Submission time Handle Problem Language Result Execution time Memory
209058 2020-03-13T04:05:10 Z bensonlzl Mergers (JOI19_mergers) C++14
0 / 100
126 ms 55572 KB
#include <bits/stdc++.h>
 
using namespace std;
 
int N, K, X, Y, state[500005], par[500005], sz[500005], anc[500005];
int lca[500005][20], depth[500005];
vector<int> AdjList[500005], AdjList2[500005], st[500005];
 
void dfs_init(int x, int p){
	anc[x] = p;
	lca[x][0] = p;
	for (int i = 1; i < 20; ++i){
		lca[x][i] = lca[lca[x][i-1]][i-1];
	}
	for (auto it : AdjList[x]){
		if (it == p) continue;
		depth[it] = depth[x] + 1;
		dfs_init(it,x);
	}
}
 
int get_lca(int x, int y){
	if (depth[x] > depth[y]) swap(x,y);
	int d = depth[y] - depth[x];
	for (int i = 0; i < 20; ++i){
		if (d & (1 << i)){
			y = lca[y][i];
		}
	}
	if (x == y) return x;
	for (int i = 19; i >= 0; --i){
		if (lca[x][i] != lca[y][i]){
			x = lca[x][i];
			y = lca[y][i];
		}
	}
	return lca[x][0];
}
 
int find_par(int x){
	if (x != par[x]) par[x] = find_par(par[x]);
	return par[x];
}
 
void merg(int x, int y){
	int X = find_par(x), Y = find_par(y);
	if (X == Y) return;
	if (sz[X] < sz[Y]) swap(X,Y);
	par[Y] = X;
	sz[X] += sz[Y];
	if (depth[anc[Y]] < depth[anc[X]]){
		anc[X] = anc[Y];
	}
}
 
void combine(int x, int t){
	int X = find_par(x), T = find_par(t);
	if (anc[X] == anc[T]) return;
	combine(anc[X],T);
	merg(X,anc[X]);
}
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> K;
	for (int i = 1; i < N; ++i){
		cin >> X >> Y;
		AdjList[X].push_back(Y);
		AdjList[Y].push_back(X);
	}
	for (int i = 1; i <= N; ++i){
		par[i] = i;
		sz[i] = 1;
		cin >> state[i];
		st[state[i]].push_back(i);
	}
	dfs_init(1,0);
	merg(1,0);
	for (int i = 1; i <= K; ++i){
		int L = st[i][0];
		for (int j = 1; j < st[i].size(); ++j){
			L = get_lca(L,st[i][j]);
		}
		//cout << i << ' ' << L << '\n';
		for (auto it : st[i]){
			combine(it,L);
		}
	}
	for (int i = 1; i <= N; ++i){
		for (auto it : AdjList[i]){
			if (find_par(i) != find_par(it)){
				AdjList2[find_par(i)].push_back(find_par(it));
			}
		}
	}
	int leaves = 0;
	for (int i = 1; i <= N; ++i){
		if (AdjList2[i].size() == 1) leaves++;
	}
	cout << (leaves+1)/2 << '\n';
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:82:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 1; j < st[i].size(); ++j){
                   ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 28 ms 35704 KB Output is correct
2 Correct 25 ms 35576 KB Output is correct
3 Correct 26 ms 35576 KB Output is correct
4 Correct 25 ms 35576 KB Output is correct
5 Correct 25 ms 35576 KB Output is correct
6 Incorrect 26 ms 35576 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 35704 KB Output is correct
2 Correct 25 ms 35576 KB Output is correct
3 Correct 26 ms 35576 KB Output is correct
4 Correct 25 ms 35576 KB Output is correct
5 Correct 25 ms 35576 KB Output is correct
6 Incorrect 26 ms 35576 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 35704 KB Output is correct
2 Correct 25 ms 35576 KB Output is correct
3 Correct 26 ms 35576 KB Output is correct
4 Correct 25 ms 35576 KB Output is correct
5 Correct 25 ms 35576 KB Output is correct
6 Incorrect 26 ms 35576 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 117 ms 49520 KB Output is correct
2 Correct 126 ms 55572 KB Output is correct
3 Correct 29 ms 36088 KB Output is correct
4 Incorrect 29 ms 35960 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 35704 KB Output is correct
2 Correct 25 ms 35576 KB Output is correct
3 Correct 26 ms 35576 KB Output is correct
4 Correct 25 ms 35576 KB Output is correct
5 Correct 25 ms 35576 KB Output is correct
6 Incorrect 26 ms 35576 KB Output isn't correct
7 Halted 0 ms 0 KB -