답안 #934354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
934354 2024-02-27T08:07:06 Z penguin133 Mergers (JOI19_mergers) C++17
100 / 100
1104 ms 223436 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, k;
int S[500005], A[500005];
vector <int> adj[500005];
int deg[500005];
vector <int> shit[500005];
map <int, int> mp[500005];

pi dp[500005];

void dfs(int x, int par){
	mp[x][S[x]] = 1;
	int cnt = 0;
	for(auto i : adj[x]){
		if(i == par)continue;
		dfs(i, x);
		cnt += (mp[i].empty());
		if(shit[x].size() < shit[i].size())swap(shit[x], shit[i]);
		for(auto j : shit[i])shit[x].push_back(j);
		dp[x].fi += dp[i].fi;
		dp[x].se += dp[i].se;
		if(dp[x].se >= 3){
			dp[x].fi += dp[x].se / 2, dp[x].se %= 2;
			if(!dp[x].se)dp[x].se = 2, dp[x].fi--;
		}
		if(mp[x].size() < mp[i].size())swap(mp[x], mp[i]);
		for(auto [j, k] : mp[i]){
			mp[x][j] += k;
			if(mp[x][j] == A[j])mp[x].erase(j);
		}
	}
	if(mp[x].find(S[x]) != mp[x].end() && mp[x][S[x]] == A[S[x]])mp[x].erase(S[x]);
	if(mp[x].empty()){
		deg[x] += shit[x].size();
		for(auto i : shit[x])deg[i]++;
		shit[x] = {x};
	}
	/*
	cout << x << '\n';
	for(auto [i, j] : mp[x])cout << i << ' ' << j << '\n';
	cout << '\n';
	*/
}

void solve(){
	cin >> n >> k;
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	for(int i = 1; i <= n; i++)cin >> S[i], A[S[i]]++;
	dfs(1, -1);
	//cout << dp[1].fi + dp[1].se;
	//cout << dp[1].fi + (dp[1].se + 1) / 2;
	int ans = 0;
	for(int i = 1; i <= n; i++)ans += (deg[i] == 1);
	cout << (ans + 1) / 2;
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

mergers.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   72 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 53592 KB Output is correct
2 Correct 13 ms 53592 KB Output is correct
3 Correct 13 ms 53592 KB Output is correct
4 Correct 12 ms 53720 KB Output is correct
5 Correct 12 ms 53592 KB Output is correct
6 Correct 13 ms 53596 KB Output is correct
7 Correct 12 ms 53684 KB Output is correct
8 Correct 12 ms 53596 KB Output is correct
9 Correct 12 ms 53820 KB Output is correct
10 Correct 12 ms 53820 KB Output is correct
11 Correct 18 ms 53596 KB Output is correct
12 Correct 12 ms 53596 KB Output is correct
13 Correct 12 ms 53720 KB Output is correct
14 Correct 13 ms 53848 KB Output is correct
15 Correct 12 ms 53720 KB Output is correct
16 Correct 12 ms 53596 KB Output is correct
17 Correct 12 ms 53728 KB Output is correct
18 Correct 12 ms 53596 KB Output is correct
19 Correct 13 ms 53596 KB Output is correct
20 Correct 13 ms 53728 KB Output is correct
21 Correct 12 ms 53592 KB Output is correct
22 Correct 14 ms 53592 KB Output is correct
23 Correct 12 ms 53596 KB Output is correct
24 Correct 12 ms 53596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 53592 KB Output is correct
2 Correct 13 ms 53592 KB Output is correct
3 Correct 13 ms 53592 KB Output is correct
4 Correct 12 ms 53720 KB Output is correct
5 Correct 12 ms 53592 KB Output is correct
6 Correct 13 ms 53596 KB Output is correct
7 Correct 12 ms 53684 KB Output is correct
8 Correct 12 ms 53596 KB Output is correct
9 Correct 12 ms 53820 KB Output is correct
10 Correct 12 ms 53820 KB Output is correct
11 Correct 18 ms 53596 KB Output is correct
12 Correct 12 ms 53596 KB Output is correct
13 Correct 12 ms 53720 KB Output is correct
14 Correct 13 ms 53848 KB Output is correct
15 Correct 12 ms 53720 KB Output is correct
16 Correct 12 ms 53596 KB Output is correct
17 Correct 12 ms 53728 KB Output is correct
18 Correct 12 ms 53596 KB Output is correct
19 Correct 13 ms 53596 KB Output is correct
20 Correct 13 ms 53728 KB Output is correct
21 Correct 12 ms 53592 KB Output is correct
22 Correct 14 ms 53592 KB Output is correct
23 Correct 12 ms 53596 KB Output is correct
24 Correct 12 ms 53596 KB Output is correct
25 Correct 13 ms 53596 KB Output is correct
26 Correct 14 ms 54108 KB Output is correct
27 Correct 15 ms 54228 KB Output is correct
28 Correct 14 ms 54360 KB Output is correct
29 Correct 15 ms 53848 KB Output is correct
30 Correct 14 ms 54364 KB Output is correct
31 Correct 13 ms 53820 KB Output is correct
32 Correct 14 ms 54364 KB Output is correct
33 Correct 12 ms 53740 KB Output is correct
34 Correct 13 ms 54108 KB Output is correct
35 Correct 16 ms 53852 KB Output is correct
36 Correct 14 ms 54108 KB Output is correct
37 Correct 15 ms 54408 KB Output is correct
38 Correct 16 ms 53852 KB Output is correct
39 Correct 16 ms 54620 KB Output is correct
40 Correct 15 ms 54108 KB Output is correct
41 Correct 14 ms 53964 KB Output is correct
42 Correct 15 ms 54108 KB Output is correct
43 Correct 15 ms 54616 KB Output is correct
44 Correct 12 ms 53596 KB Output is correct
45 Correct 14 ms 54104 KB Output is correct
46 Correct 15 ms 54108 KB Output is correct
47 Correct 13 ms 53592 KB Output is correct
48 Correct 14 ms 54108 KB Output is correct
49 Correct 14 ms 54108 KB Output is correct
50 Correct 14 ms 54108 KB Output is correct
51 Correct 14 ms 54108 KB Output is correct
52 Correct 14 ms 54108 KB Output is correct
53 Correct 14 ms 54168 KB Output is correct
54 Correct 14 ms 54364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 53592 KB Output is correct
2 Correct 13 ms 53592 KB Output is correct
3 Correct 13 ms 53592 KB Output is correct
4 Correct 12 ms 53720 KB Output is correct
5 Correct 12 ms 53592 KB Output is correct
6 Correct 13 ms 53596 KB Output is correct
7 Correct 12 ms 53684 KB Output is correct
8 Correct 12 ms 53596 KB Output is correct
9 Correct 12 ms 53820 KB Output is correct
10 Correct 12 ms 53820 KB Output is correct
11 Correct 18 ms 53596 KB Output is correct
12 Correct 12 ms 53596 KB Output is correct
13 Correct 12 ms 53720 KB Output is correct
14 Correct 13 ms 53848 KB Output is correct
15 Correct 12 ms 53720 KB Output is correct
16 Correct 12 ms 53596 KB Output is correct
17 Correct 12 ms 53728 KB Output is correct
18 Correct 12 ms 53596 KB Output is correct
19 Correct 13 ms 53596 KB Output is correct
20 Correct 13 ms 53728 KB Output is correct
21 Correct 12 ms 53592 KB Output is correct
22 Correct 14 ms 53592 KB Output is correct
23 Correct 12 ms 53596 KB Output is correct
24 Correct 12 ms 53596 KB Output is correct
25 Correct 14 ms 53704 KB Output is correct
26 Correct 61 ms 65288 KB Output is correct
27 Correct 112 ms 75676 KB Output is correct
28 Correct 14 ms 54108 KB Output is correct
29 Correct 13 ms 53596 KB Output is correct
30 Correct 14 ms 53596 KB Output is correct
31 Correct 74 ms 68628 KB Output is correct
32 Correct 14 ms 54008 KB Output is correct
33 Correct 109 ms 83540 KB Output is correct
34 Correct 99 ms 76568 KB Output is correct
35 Correct 14 ms 54104 KB Output is correct
36 Correct 97 ms 70076 KB Output is correct
37 Correct 14 ms 54108 KB Output is correct
38 Correct 15 ms 54104 KB Output is correct
39 Correct 66 ms 65272 KB Output is correct
40 Correct 14 ms 54616 KB Output is correct
41 Correct 69 ms 66492 KB Output is correct
42 Correct 91 ms 73928 KB Output is correct
43 Correct 14 ms 53596 KB Output is correct
44 Correct 89 ms 84048 KB Output is correct
45 Correct 102 ms 77908 KB Output is correct
46 Correct 14 ms 54108 KB Output is correct
47 Correct 15 ms 54364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 65316 KB Output is correct
2 Correct 67 ms 65312 KB Output is correct
3 Correct 15 ms 54104 KB Output is correct
4 Correct 14 ms 54104 KB Output is correct
5 Correct 12 ms 53596 KB Output is correct
6 Correct 13 ms 53596 KB Output is correct
7 Correct 14 ms 54108 KB Output is correct
8 Correct 101 ms 73292 KB Output is correct
9 Correct 13 ms 54104 KB Output is correct
10 Correct 76 ms 68692 KB Output is correct
11 Correct 12 ms 53592 KB Output is correct
12 Correct 96 ms 68800 KB Output is correct
13 Correct 83 ms 68688 KB Output is correct
14 Correct 75 ms 64596 KB Output is correct
15 Correct 65 ms 65372 KB Output is correct
16 Correct 14 ms 54108 KB Output is correct
17 Correct 12 ms 53596 KB Output is correct
18 Correct 90 ms 66352 KB Output is correct
19 Correct 96 ms 75172 KB Output is correct
20 Correct 15 ms 54108 KB Output is correct
21 Correct 13 ms 53596 KB Output is correct
22 Correct 94 ms 67056 KB Output is correct
23 Correct 14 ms 54092 KB Output is correct
24 Correct 82 ms 68180 KB Output is correct
25 Correct 93 ms 72272 KB Output is correct
26 Correct 14 ms 54108 KB Output is correct
27 Correct 14 ms 54268 KB Output is correct
28 Correct 16 ms 54108 KB Output is correct
29 Correct 15 ms 53992 KB Output is correct
30 Correct 15 ms 54304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 53592 KB Output is correct
2 Correct 13 ms 53592 KB Output is correct
3 Correct 13 ms 53592 KB Output is correct
4 Correct 12 ms 53720 KB Output is correct
5 Correct 12 ms 53592 KB Output is correct
6 Correct 13 ms 53596 KB Output is correct
7 Correct 12 ms 53684 KB Output is correct
8 Correct 12 ms 53596 KB Output is correct
9 Correct 12 ms 53820 KB Output is correct
10 Correct 12 ms 53820 KB Output is correct
11 Correct 18 ms 53596 KB Output is correct
12 Correct 12 ms 53596 KB Output is correct
13 Correct 12 ms 53720 KB Output is correct
14 Correct 13 ms 53848 KB Output is correct
15 Correct 12 ms 53720 KB Output is correct
16 Correct 12 ms 53596 KB Output is correct
17 Correct 12 ms 53728 KB Output is correct
18 Correct 12 ms 53596 KB Output is correct
19 Correct 13 ms 53596 KB Output is correct
20 Correct 13 ms 53728 KB Output is correct
21 Correct 12 ms 53592 KB Output is correct
22 Correct 14 ms 53592 KB Output is correct
23 Correct 12 ms 53596 KB Output is correct
24 Correct 12 ms 53596 KB Output is correct
25 Correct 13 ms 53596 KB Output is correct
26 Correct 14 ms 54108 KB Output is correct
27 Correct 15 ms 54228 KB Output is correct
28 Correct 14 ms 54360 KB Output is correct
29 Correct 15 ms 53848 KB Output is correct
30 Correct 14 ms 54364 KB Output is correct
31 Correct 13 ms 53820 KB Output is correct
32 Correct 14 ms 54364 KB Output is correct
33 Correct 12 ms 53740 KB Output is correct
34 Correct 13 ms 54108 KB Output is correct
35 Correct 16 ms 53852 KB Output is correct
36 Correct 14 ms 54108 KB Output is correct
37 Correct 15 ms 54408 KB Output is correct
38 Correct 16 ms 53852 KB Output is correct
39 Correct 16 ms 54620 KB Output is correct
40 Correct 15 ms 54108 KB Output is correct
41 Correct 14 ms 53964 KB Output is correct
42 Correct 15 ms 54108 KB Output is correct
43 Correct 15 ms 54616 KB Output is correct
44 Correct 12 ms 53596 KB Output is correct
45 Correct 14 ms 54104 KB Output is correct
46 Correct 15 ms 54108 KB Output is correct
47 Correct 13 ms 53592 KB Output is correct
48 Correct 14 ms 54108 KB Output is correct
49 Correct 14 ms 54108 KB Output is correct
50 Correct 14 ms 54108 KB Output is correct
51 Correct 14 ms 54108 KB Output is correct
52 Correct 14 ms 54108 KB Output is correct
53 Correct 14 ms 54168 KB Output is correct
54 Correct 14 ms 54364 KB Output is correct
55 Correct 14 ms 53704 KB Output is correct
56 Correct 61 ms 65288 KB Output is correct
57 Correct 112 ms 75676 KB Output is correct
58 Correct 14 ms 54108 KB Output is correct
59 Correct 13 ms 53596 KB Output is correct
60 Correct 14 ms 53596 KB Output is correct
61 Correct 74 ms 68628 KB Output is correct
62 Correct 14 ms 54008 KB Output is correct
63 Correct 109 ms 83540 KB Output is correct
64 Correct 99 ms 76568 KB Output is correct
65 Correct 14 ms 54104 KB Output is correct
66 Correct 97 ms 70076 KB Output is correct
67 Correct 14 ms 54108 KB Output is correct
68 Correct 15 ms 54104 KB Output is correct
69 Correct 66 ms 65272 KB Output is correct
70 Correct 14 ms 54616 KB Output is correct
71 Correct 69 ms 66492 KB Output is correct
72 Correct 91 ms 73928 KB Output is correct
73 Correct 14 ms 53596 KB Output is correct
74 Correct 89 ms 84048 KB Output is correct
75 Correct 102 ms 77908 KB Output is correct
76 Correct 14 ms 54108 KB Output is correct
77 Correct 15 ms 54364 KB Output is correct
78 Correct 59 ms 65316 KB Output is correct
79 Correct 67 ms 65312 KB Output is correct
80 Correct 15 ms 54104 KB Output is correct
81 Correct 14 ms 54104 KB Output is correct
82 Correct 12 ms 53596 KB Output is correct
83 Correct 13 ms 53596 KB Output is correct
84 Correct 14 ms 54108 KB Output is correct
85 Correct 101 ms 73292 KB Output is correct
86 Correct 13 ms 54104 KB Output is correct
87 Correct 76 ms 68692 KB Output is correct
88 Correct 12 ms 53592 KB Output is correct
89 Correct 96 ms 68800 KB Output is correct
90 Correct 83 ms 68688 KB Output is correct
91 Correct 75 ms 64596 KB Output is correct
92 Correct 65 ms 65372 KB Output is correct
93 Correct 14 ms 54108 KB Output is correct
94 Correct 12 ms 53596 KB Output is correct
95 Correct 90 ms 66352 KB Output is correct
96 Correct 96 ms 75172 KB Output is correct
97 Correct 15 ms 54108 KB Output is correct
98 Correct 13 ms 53596 KB Output is correct
99 Correct 94 ms 67056 KB Output is correct
100 Correct 14 ms 54092 KB Output is correct
101 Correct 82 ms 68180 KB Output is correct
102 Correct 93 ms 72272 KB Output is correct
103 Correct 14 ms 54108 KB Output is correct
104 Correct 14 ms 54268 KB Output is correct
105 Correct 16 ms 54108 KB Output is correct
106 Correct 15 ms 53992 KB Output is correct
107 Correct 15 ms 54304 KB Output is correct
108 Correct 709 ms 120376 KB Output is correct
109 Correct 625 ms 163920 KB Output is correct
110 Correct 677 ms 188904 KB Output is correct
111 Correct 717 ms 168076 KB Output is correct
112 Correct 854 ms 154612 KB Output is correct
113 Correct 543 ms 112236 KB Output is correct
114 Correct 548 ms 120656 KB Output is correct
115 Correct 570 ms 120280 KB Output is correct
116 Correct 855 ms 200576 KB Output is correct
117 Correct 544 ms 104532 KB Output is correct
118 Correct 524 ms 122160 KB Output is correct
119 Correct 537 ms 104732 KB Output is correct
120 Correct 658 ms 145852 KB Output is correct
121 Correct 560 ms 105156 KB Output is correct
122 Correct 1104 ms 223436 KB Output is correct
123 Correct 451 ms 109900 KB Output is correct
124 Correct 620 ms 103760 KB Output is correct