Submission #938555

# Submission time Handle Problem Language Result Execution time Memory
938555 2024-03-05T10:12:49 Z WonderfulWhale Mergers (JOI19_mergers) C++17
0 / 100
83 ms 69416 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];
int start;

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!=start&&res[x]==0) vis[x]=true;
	//cerr << x << " " << vis[x] << "\n";
}

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]]++;
	}
	if(n==2) {
		start = 1;
	} else {
		for(int i=1;i<=n;i++) {
			if(sz(G[i])>1) {
				start = i;
			}
		}
	}
	//cout << "start " << start << "\n";
	dfs(start);
	dfs2(start);
	//cout << dp[start] << "\n";
	if(dp[start]==1) {
		cout << "1\n";
		return 0;
	}
	if(dp[start]==0) {
		cout << "0\n";
		return 0;
	}
	cout << dp[start]-1 << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 62 ms 64852 KB Output is correct
2 Incorrect 83 ms 69416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 47196 KB Output isn't correct
2 Halted 0 ms 0 KB -