Submission #379737

# Submission time Handle Problem Language Result Execution time Memory
379737 2021-03-19T07:41:20 Z cgiosy Mergers (JOI19_mergers) C++17
0 / 100
88 ms 13668 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<int(int, int)> dfs=[&](int i, int p) {
		int cnt=1;
		for(int j:G[i]) if(j!=p) {
			D[j]=D[i]+1;
			int v=dfs(j, i);
			cnt=~cnt && ~v ? cnt+v : -1;
		}
		p=i;
		for(int j:H[C[i]]) {
			p=root(p), j=root(j);
			if(p==j) continue;
			if(D[p]>D[j]) swap(p, j);
			P[p]+=P[j], P[j]=p;
		}
		H[C[i]].clear();
		if(~cnt && i==p && -P[i]==cnt) ans++, cnt=-1;
		return cnt;
	};
	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 53 ms 8688 KB Output is correct
2 Incorrect 88 ms 13668 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 -