Submission #938543

# Submission time Handle Problem Language Result Execution time Memory
938543 2024-03-05T09:55:20 Z WonderfulWhale Mergers (JOI19_mergers) C++17
0 / 100
88 ms 64208 KB
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const int MAXN =  500'009;

unordered_map<int, int> M[MAXN];
int goal[MAXN];
int type[MAXN];
vector<int> G[MAXN];
int res[MAXN];
int ans;

void dfs(int x, int p=0) {
	M[x][type[x]] = 1;
	if(goal[type[x]]>1) res[x] = 1;
	for(int y:G[x]) {
		if(y==p) continue;
		dfs(y, x);
		//cerr << "mergin: "<< x << " " << y << "\n";
		if(sz(M[y])>sz(M[x])) {
			//cerr << "swap\n";
			swap(M[y], M[x]);
			swap(res[y], res[x]);
		}
		for(pii z:M[y]) {
			//cerr << "adding: " << z.st << " " << z.nd << "\n";
			//cerr << "res before: "<< res[x] << "\n";
			if(M[x][z.st]==0) res[x]++;
			M[x][z.st] += z.nd;
			if(M[x][z.st]==goal[z.st]) res[x]--;
			//cerr << "res after: " << res[x] << "\n";
		}
		M[y].clear();
	}
	//cerr << "-> " << x << " " << res[x] << "\n";
	if(x>1&&res[x]==0) ans++;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int n, k;
	cin >> n >> k;
	for(int i=0;i<n-1;i++) {
		int a, b;
		cin >> a >> b;
		G[a].pb(b);
		G[b].pb(a);
	}
	for(int i=1;i<=n;i++) {
		cin >> type[i];
		goal[type[i]]++;
	}
	dfs(1);
	if(ans==1) {
		cout << "1\n";
		return 0;
	}
	cout << max(ans-1, (int)0) << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 44888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 44888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 44888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 76 ms 61968 KB Output is correct
2 Incorrect 88 ms 64208 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 44888 KB Output isn't correct
2 Halted 0 ms 0 KB -