답안 #417430

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
417430 2021-06-03T17:29:36 Z shivensinha4 Mergers (JOI19_mergers) C++17
0 / 100
71 ms 16292 KB
#include <bits/stdc++.h>
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'

int main() {
#ifdef mlocal
	freopen("test.in", "r", stdin);
#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n, k; cin >> n >> k;
	vector<vi> adj(n);
	for_(i, 0, n-1) {
		int a, b; cin >> a >> b;
		a -= 1; b -= 1;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	vi st(n), ct(n);
	for_(i, 0, n) {
		cin >> st[i];
		st[i] -= 1;
		ct[st[i]]++;
	}
	
	vector<map<int, int>> mp(n);
	vector<bool> lone(n);
	
	int ans = 0;
	function<void(int,int)> dfs = [&](int p, int parent) {
		int badchild = 0;
		if (ct[st[p]] != 1) mp[p][st[p]] = 1;
		
		for (auto i: adj[p]) if (i != parent) {
			dfs(i, p);
			lone[p] = lone[p] or lone[i];
			if (mp[i].size() == 0 and !lone[i]) badchild++;
			
			if (mp[i].size() > mp[p].size()) swap(mp[p], mp[i]);
			vi rem;
			for (auto &v: mp[i]) {
				mp[p][v.first] += v.second;
				if (mp[p][v.first] == ct[v.first]) rem.push_back(v.first);
			}
			
			for (auto v: rem) mp[p].erase(v);
		}
		
//		cout << p << ": " << mp[p] << endl;
		ans += (badchild+1)/2;
		lone[p] = lone[p] or (badchild%2);
	};
	
	bool done = false;
	for_(i, 0, n) if (adj[i].size() != 1) {
		dfs(i, i);
		done = true;
		break;
	}
	if (!done) dfs(0, 0);
	cout << ans << endl;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 16292 KB Output is correct
2 Correct 55 ms 11556 KB Output is correct
3 Incorrect 2 ms 588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -