Submission #938576

# Submission time Handle Problem Language Result Execution time Memory
938576 2024-03-05T10:24:17 Z WonderfulWhale Mergers (JOI19_mergers) C++17
10 / 100
165 ms 70696 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";
	cout << (dp[start]+1+(dp[start]==1))/2 << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 12 ms 49244 KB Output is correct
3 Correct 13 ms 49424 KB Output is correct
4 Correct 11 ms 49360 KB Output is correct
5 Correct 11 ms 47308 KB Output is correct
6 Correct 13 ms 47372 KB Output is correct
7 Correct 11 ms 45148 KB Output is correct
8 Correct 11 ms 47196 KB Output is correct
9 Correct 12 ms 49244 KB Output is correct
10 Correct 11 ms 49336 KB Output is correct
11 Correct 11 ms 49500 KB Output is correct
12 Correct 13 ms 49360 KB Output is correct
13 Correct 11 ms 49244 KB Output is correct
14 Correct 11 ms 49240 KB Output is correct
15 Correct 13 ms 49292 KB Output is correct
16 Correct 10 ms 47196 KB Output is correct
17 Correct 10 ms 47196 KB Output is correct
18 Correct 11 ms 47196 KB Output is correct
19 Correct 11 ms 47196 KB Output is correct
20 Correct 11 ms 47448 KB Output is correct
21 Correct 11 ms 47196 KB Output is correct
22 Correct 13 ms 47196 KB Output is correct
23 Correct 11 ms 47356 KB Output is correct
24 Correct 11 ms 49244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 12 ms 49244 KB Output is correct
3 Correct 13 ms 49424 KB Output is correct
4 Correct 11 ms 49360 KB Output is correct
5 Correct 11 ms 47308 KB Output is correct
6 Correct 13 ms 47372 KB Output is correct
7 Correct 11 ms 45148 KB Output is correct
8 Correct 11 ms 47196 KB Output is correct
9 Correct 12 ms 49244 KB Output is correct
10 Correct 11 ms 49336 KB Output is correct
11 Correct 11 ms 49500 KB Output is correct
12 Correct 13 ms 49360 KB Output is correct
13 Correct 11 ms 49244 KB Output is correct
14 Correct 11 ms 49240 KB Output is correct
15 Correct 13 ms 49292 KB Output is correct
16 Correct 10 ms 47196 KB Output is correct
17 Correct 10 ms 47196 KB Output is correct
18 Correct 11 ms 47196 KB Output is correct
19 Correct 11 ms 47196 KB Output is correct
20 Correct 11 ms 47448 KB Output is correct
21 Correct 11 ms 47196 KB Output is correct
22 Correct 13 ms 47196 KB Output is correct
23 Correct 11 ms 47356 KB Output is correct
24 Correct 11 ms 49244 KB Output is correct
25 Correct 11 ms 47376 KB Output is correct
26 Correct 13 ms 50012 KB Output is correct
27 Correct 15 ms 49792 KB Output is correct
28 Correct 14 ms 48408 KB Output is correct
29 Correct 14 ms 47964 KB Output is correct
30 Correct 15 ms 49752 KB Output is correct
31 Correct 11 ms 47196 KB Output is correct
32 Correct 14 ms 50272 KB Output is correct
33 Correct 11 ms 49244 KB Output is correct
34 Correct 13 ms 47840 KB Output is correct
35 Correct 15 ms 47964 KB Output is correct
36 Incorrect 13 ms 49848 KB Output isn't correct
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 12 ms 49244 KB Output is correct
3 Correct 13 ms 49424 KB Output is correct
4 Correct 11 ms 49360 KB Output is correct
5 Correct 11 ms 47308 KB Output is correct
6 Correct 13 ms 47372 KB Output is correct
7 Correct 11 ms 45148 KB Output is correct
8 Correct 11 ms 47196 KB Output is correct
9 Correct 12 ms 49244 KB Output is correct
10 Correct 11 ms 49336 KB Output is correct
11 Correct 11 ms 49500 KB Output is correct
12 Correct 13 ms 49360 KB Output is correct
13 Correct 11 ms 49244 KB Output is correct
14 Correct 11 ms 49240 KB Output is correct
15 Correct 13 ms 49292 KB Output is correct
16 Correct 10 ms 47196 KB Output is correct
17 Correct 10 ms 47196 KB Output is correct
18 Correct 11 ms 47196 KB Output is correct
19 Correct 11 ms 47196 KB Output is correct
20 Correct 11 ms 47448 KB Output is correct
21 Correct 11 ms 47196 KB Output is correct
22 Correct 13 ms 47196 KB Output is correct
23 Correct 11 ms 47356 KB Output is correct
24 Correct 11 ms 49244 KB Output is correct
25 Correct 10 ms 47192 KB Output is correct
26 Correct 75 ms 64836 KB Output is correct
27 Correct 101 ms 66904 KB Output is correct
28 Correct 13 ms 49756 KB Output is correct
29 Correct 12 ms 47196 KB Output is correct
30 Correct 11 ms 49244 KB Output is correct
31 Incorrect 93 ms 66752 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 64888 KB Output is correct
2 Correct 106 ms 69404 KB Output is correct
3 Correct 13 ms 50012 KB Output is correct
4 Correct 14 ms 49756 KB Output is correct
5 Correct 13 ms 47376 KB Output is correct
6 Correct 12 ms 49240 KB Output is correct
7 Correct 12 ms 47840 KB Output is correct
8 Correct 165 ms 70696 KB Output is correct
9 Incorrect 14 ms 49756 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47300 KB Output is correct
2 Correct 12 ms 49244 KB Output is correct
3 Correct 13 ms 49424 KB Output is correct
4 Correct 11 ms 49360 KB Output is correct
5 Correct 11 ms 47308 KB Output is correct
6 Correct 13 ms 47372 KB Output is correct
7 Correct 11 ms 45148 KB Output is correct
8 Correct 11 ms 47196 KB Output is correct
9 Correct 12 ms 49244 KB Output is correct
10 Correct 11 ms 49336 KB Output is correct
11 Correct 11 ms 49500 KB Output is correct
12 Correct 13 ms 49360 KB Output is correct
13 Correct 11 ms 49244 KB Output is correct
14 Correct 11 ms 49240 KB Output is correct
15 Correct 13 ms 49292 KB Output is correct
16 Correct 10 ms 47196 KB Output is correct
17 Correct 10 ms 47196 KB Output is correct
18 Correct 11 ms 47196 KB Output is correct
19 Correct 11 ms 47196 KB Output is correct
20 Correct 11 ms 47448 KB Output is correct
21 Correct 11 ms 47196 KB Output is correct
22 Correct 13 ms 47196 KB Output is correct
23 Correct 11 ms 47356 KB Output is correct
24 Correct 11 ms 49244 KB Output is correct
25 Correct 11 ms 47376 KB Output is correct
26 Correct 13 ms 50012 KB Output is correct
27 Correct 15 ms 49792 KB Output is correct
28 Correct 14 ms 48408 KB Output is correct
29 Correct 14 ms 47964 KB Output is correct
30 Correct 15 ms 49752 KB Output is correct
31 Correct 11 ms 47196 KB Output is correct
32 Correct 14 ms 50272 KB Output is correct
33 Correct 11 ms 49244 KB Output is correct
34 Correct 13 ms 47840 KB Output is correct
35 Correct 15 ms 47964 KB Output is correct
36 Incorrect 13 ms 49848 KB Output isn't correct
37 Halted 0 ms 0 KB -