Submission #938549

# Submission time Handle Problem Language Result Execution time Memory
938549 2024-03-05T10:04:14 Z WonderfulWhale Mergers (JOI19_mergers) C++17
0 / 100
86 ms 69420 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;
bool vis[MAXN];
int dp[MAXN];

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) vis[x]=true;
}

void dfs2(int x, int p=0) {
	for(int y:G[x]) {
		if(y==p) continue;
		dfs2(y, x);
		dp[x] += dp[y];
	}
	if(dp[x]==0) dp[x] = vis[x];
}

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);
	dfs2(1);
	if(dp[1]==1) {
		cout << "1\n";
		return 0;
	}
	if(dp[1]==0) {
		cout << "0\n";
		return 0;
	}
	cout << dp[1]-1 << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 49244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 49244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 49244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 75 ms 64916 KB Output is correct
2 Incorrect 86 ms 69420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 49244 KB Output isn't correct
2 Halted 0 ms 0 KB -