Submission #209043

# Submission time Handle Problem Language Result Execution time Memory
209043 2020-03-13T02:30:28 Z bensonlzl Mergers (JOI19_mergers) C++14
0 / 100
312 ms 262148 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];
}

int combine(int x, int t){
	if (x == t) return anc[t];
	merg(x,anc[x]);
	anc[x] = combine(anc[x],t);
	return 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);
	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]){
			anc[it] = 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:78: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 26 ms 35704 KB Output is correct
2 Runtime error 253 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35704 KB Output is correct
2 Runtime error 253 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35704 KB Output is correct
2 Runtime error 253 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 312 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35704 KB Output is correct
2 Runtime error 253 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -