Submission #379740

# Submission time Handle Problem Language Result Execution time Memory
379740 2021-03-19T07:48:48 Z cgiosy Mergers (JOI19_mergers) C++17
0 / 100
84 ms 12900 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(0);cin.tie(0);
	int N, M, ans=1;
	cin>>N>>M;
	vector<int> C(N), D(N), P(N, -1);
	vector<vector<int>> G(N), H(M);
	for(int i=1; i<N; i++) {
		int x, y;
		cin>>x>>y; --x, --y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for(int i=0; i<N; i++) cin>>C[i], H[--C[i]].push_back(i);
	function<int(int)> root=[&](int i) { return P[i]>=0 ? P[i]=root(P[i]) : i; };
	function<void(int, int)> dep=[&](int i, int p) {
		for(int j:G[i]) if(j!=p) D[j]=D[i]+1, dep(j, i);
	};
	function<int(int, int)> dfs=[&](int i, int p) {
		int cnt=1;
		for(int j:G[i]) if(j!=p) {
			int v=dfs(j, i);
			cnt=~cnt && ~v ? cnt+v : -1;
		}
		p=i;
		for(int q:H[C[i]]) {
			p=root(p), q=root(q);
			if(p==q) continue;
			if(D[p]>D[q]) swap(p, q);
			P[p]+=P[q], P[q]=p;
		}
		H[C[i]].clear();
		if(~cnt && i==p && -P[i]==cnt) ans++, cnt=-1;
		return cnt;
	};
	dep(0, 0);
	dfs(0, 0);
	cout<<ans/2<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 8036 KB Output is correct
2 Incorrect 84 ms 12900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -