답안 #1114250

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114250 2024-11-18T12:36:35 Z Dan4Life Mergers (JOI19_mergers) C++17
100 / 100
919 ms 154496 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ar2 = array<int,2>;
const int N = (int)5e5+10;
int n, k;
vector<int> adj[N];
int p[N], sz[N];
int a[N], tot[N], deg[N];
map<int,int> S[N];
 
int findSet(int i){return i==p[i]?i:p[i]=findSet(p[i]);}
void unionSet(int i, int j){
	int x = findSet(i), y = findSet(j);
	if(x==y) return;
	if(sz[x]<sz[y])swap(x,y);
	p[y] = x, sz[x]+=sz[y];
}
 
void dfs(int s, int p){
	S[s][a[s]]=1;
	if(S[s][a[s]]==tot[a[s]]) 
		S[s].erase(S[s].find(a[s]));
	for(auto u : adj[s]){
		if(u==p) continue;
		dfs(u,s);
		if(sz(S[s]) < sz(S[u])) S[s].swap(S[u]);
		for(auto [v,w] : S[u]){
			S[s][v]+=w;
			if(S[s][v]==tot[v])
				S[s].erase(S[s].find(v));
		}
	}
	if(p and !!sz(S[s])) unionSet(s,p);
}
 
int32_t main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; i++) p[i]=i,sz[i]=1;
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		adj[a].pb(b); adj[b].pb(a);
	}
	for(int i = 1; i <= n; i++)
		cin >> a[i], tot[a[i]]++;
	dfs(1,0);
	for(int i = 1; i <= n; i++)
		for(int j : adj[i])
			if(findSet(i)!=findSet(j)) 
				deg[findSet(i)]++;
	int cnt = 1;
	for(int i = 1; i <= n; i++) cnt+=(deg[i]==1);
	cout << cnt/2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 35952 KB Output is correct
2 Correct 16 ms 35920 KB Output is correct
3 Correct 27 ms 35676 KB Output is correct
4 Correct 30 ms 35676 KB Output is correct
5 Correct 24 ms 35676 KB Output is correct
6 Correct 33 ms 35656 KB Output is correct
7 Correct 30 ms 35676 KB Output is correct
8 Correct 17 ms 35664 KB Output is correct
9 Correct 26 ms 35540 KB Output is correct
10 Correct 26 ms 35664 KB Output is correct
11 Correct 31 ms 35664 KB Output is correct
12 Correct 19 ms 35920 KB Output is correct
13 Correct 30 ms 35656 KB Output is correct
14 Correct 22 ms 35664 KB Output is correct
15 Correct 24 ms 35716 KB Output is correct
16 Correct 31 ms 35664 KB Output is correct
17 Correct 25 ms 35664 KB Output is correct
18 Correct 23 ms 35664 KB Output is correct
19 Correct 32 ms 35704 KB Output is correct
20 Correct 30 ms 35664 KB Output is correct
21 Correct 25 ms 35548 KB Output is correct
22 Correct 24 ms 35920 KB Output is correct
23 Correct 21 ms 35920 KB Output is correct
24 Correct 29 ms 35664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 35952 KB Output is correct
2 Correct 16 ms 35920 KB Output is correct
3 Correct 27 ms 35676 KB Output is correct
4 Correct 30 ms 35676 KB Output is correct
5 Correct 24 ms 35676 KB Output is correct
6 Correct 33 ms 35656 KB Output is correct
7 Correct 30 ms 35676 KB Output is correct
8 Correct 17 ms 35664 KB Output is correct
9 Correct 26 ms 35540 KB Output is correct
10 Correct 26 ms 35664 KB Output is correct
11 Correct 31 ms 35664 KB Output is correct
12 Correct 19 ms 35920 KB Output is correct
13 Correct 30 ms 35656 KB Output is correct
14 Correct 22 ms 35664 KB Output is correct
15 Correct 24 ms 35716 KB Output is correct
16 Correct 31 ms 35664 KB Output is correct
17 Correct 25 ms 35664 KB Output is correct
18 Correct 23 ms 35664 KB Output is correct
19 Correct 32 ms 35704 KB Output is correct
20 Correct 30 ms 35664 KB Output is correct
21 Correct 25 ms 35548 KB Output is correct
22 Correct 24 ms 35920 KB Output is correct
23 Correct 21 ms 35920 KB Output is correct
24 Correct 29 ms 35664 KB Output is correct
25 Correct 30 ms 35656 KB Output is correct
26 Correct 24 ms 35920 KB Output is correct
27 Correct 24 ms 35824 KB Output is correct
28 Correct 25 ms 35932 KB Output is correct
29 Correct 24 ms 35664 KB Output is correct
30 Correct 30 ms 36176 KB Output is correct
31 Correct 24 ms 35664 KB Output is correct
32 Correct 24 ms 36176 KB Output is correct
33 Correct 23 ms 35672 KB Output is correct
34 Correct 21 ms 41296 KB Output is correct
35 Correct 22 ms 35920 KB Output is correct
36 Correct 29 ms 35920 KB Output is correct
37 Correct 25 ms 35920 KB Output is correct
38 Correct 23 ms 35664 KB Output is correct
39 Correct 28 ms 36524 KB Output is correct
40 Correct 26 ms 36176 KB Output is correct
41 Correct 24 ms 35920 KB Output is correct
42 Correct 24 ms 35920 KB Output is correct
43 Correct 24 ms 36168 KB Output is correct
44 Correct 16 ms 35920 KB Output is correct
45 Correct 24 ms 35996 KB Output is correct
46 Correct 25 ms 35920 KB Output is correct
47 Correct 23 ms 35664 KB Output is correct
48 Correct 25 ms 36068 KB Output is correct
49 Correct 21 ms 36004 KB Output is correct
50 Correct 19 ms 36344 KB Output is correct
51 Correct 24 ms 35964 KB Output is correct
52 Correct 24 ms 36016 KB Output is correct
53 Correct 28 ms 35920 KB Output is correct
54 Correct 28 ms 36168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 35952 KB Output is correct
2 Correct 16 ms 35920 KB Output is correct
3 Correct 27 ms 35676 KB Output is correct
4 Correct 30 ms 35676 KB Output is correct
5 Correct 24 ms 35676 KB Output is correct
6 Correct 33 ms 35656 KB Output is correct
7 Correct 30 ms 35676 KB Output is correct
8 Correct 17 ms 35664 KB Output is correct
9 Correct 26 ms 35540 KB Output is correct
10 Correct 26 ms 35664 KB Output is correct
11 Correct 31 ms 35664 KB Output is correct
12 Correct 19 ms 35920 KB Output is correct
13 Correct 30 ms 35656 KB Output is correct
14 Correct 22 ms 35664 KB Output is correct
15 Correct 24 ms 35716 KB Output is correct
16 Correct 31 ms 35664 KB Output is correct
17 Correct 25 ms 35664 KB Output is correct
18 Correct 23 ms 35664 KB Output is correct
19 Correct 32 ms 35704 KB Output is correct
20 Correct 30 ms 35664 KB Output is correct
21 Correct 25 ms 35548 KB Output is correct
22 Correct 24 ms 35920 KB Output is correct
23 Correct 21 ms 35920 KB Output is correct
24 Correct 29 ms 35664 KB Output is correct
25 Correct 16 ms 35664 KB Output is correct
26 Correct 93 ms 45248 KB Output is correct
27 Correct 134 ms 51784 KB Output is correct
28 Correct 16 ms 36288 KB Output is correct
29 Correct 14 ms 35824 KB Output is correct
30 Correct 16 ms 35920 KB Output is correct
31 Correct 90 ms 45140 KB Output is correct
32 Correct 22 ms 36176 KB Output is correct
33 Correct 120 ms 57964 KB Output is correct
34 Correct 133 ms 51048 KB Output is correct
35 Correct 17 ms 36176 KB Output is correct
36 Correct 107 ms 47528 KB Output is correct
37 Correct 17 ms 36176 KB Output is correct
38 Correct 34 ms 35904 KB Output is correct
39 Correct 78 ms 44864 KB Output is correct
40 Correct 33 ms 36212 KB Output is correct
41 Correct 100 ms 45848 KB Output is correct
42 Correct 118 ms 48968 KB Output is correct
43 Correct 25 ms 35664 KB Output is correct
44 Correct 123 ms 58120 KB Output is correct
45 Correct 109 ms 52732 KB Output is correct
46 Correct 18 ms 36272 KB Output is correct
47 Correct 20 ms 36432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 45000 KB Output is correct
2 Correct 73 ms 41144 KB Output is correct
3 Correct 28 ms 36176 KB Output is correct
4 Correct 27 ms 35920 KB Output is correct
5 Correct 25 ms 35652 KB Output is correct
6 Correct 13 ms 35920 KB Output is correct
7 Correct 16 ms 36176 KB Output is correct
8 Correct 123 ms 47688 KB Output is correct
9 Correct 27 ms 35920 KB Output is correct
10 Correct 101 ms 44988 KB Output is correct
11 Correct 24 ms 35664 KB Output is correct
12 Correct 98 ms 44984 KB Output is correct
13 Correct 104 ms 44600 KB Output is correct
14 Correct 65 ms 41032 KB Output is correct
15 Correct 83 ms 45116 KB Output is correct
16 Correct 19 ms 36176 KB Output is correct
17 Correct 19 ms 35920 KB Output is correct
18 Correct 68 ms 42380 KB Output is correct
19 Correct 83 ms 48172 KB Output is correct
20 Correct 20 ms 36176 KB Output is correct
21 Correct 22 ms 35656 KB Output is correct
22 Correct 99 ms 45760 KB Output is correct
23 Correct 28 ms 36052 KB Output is correct
24 Correct 79 ms 45896 KB Output is correct
25 Correct 89 ms 50148 KB Output is correct
26 Correct 28 ms 35920 KB Output is correct
27 Correct 28 ms 36124 KB Output is correct
28 Correct 23 ms 36168 KB Output is correct
29 Correct 20 ms 45796 KB Output is correct
30 Correct 26 ms 35920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 35952 KB Output is correct
2 Correct 16 ms 35920 KB Output is correct
3 Correct 27 ms 35676 KB Output is correct
4 Correct 30 ms 35676 KB Output is correct
5 Correct 24 ms 35676 KB Output is correct
6 Correct 33 ms 35656 KB Output is correct
7 Correct 30 ms 35676 KB Output is correct
8 Correct 17 ms 35664 KB Output is correct
9 Correct 26 ms 35540 KB Output is correct
10 Correct 26 ms 35664 KB Output is correct
11 Correct 31 ms 35664 KB Output is correct
12 Correct 19 ms 35920 KB Output is correct
13 Correct 30 ms 35656 KB Output is correct
14 Correct 22 ms 35664 KB Output is correct
15 Correct 24 ms 35716 KB Output is correct
16 Correct 31 ms 35664 KB Output is correct
17 Correct 25 ms 35664 KB Output is correct
18 Correct 23 ms 35664 KB Output is correct
19 Correct 32 ms 35704 KB Output is correct
20 Correct 30 ms 35664 KB Output is correct
21 Correct 25 ms 35548 KB Output is correct
22 Correct 24 ms 35920 KB Output is correct
23 Correct 21 ms 35920 KB Output is correct
24 Correct 29 ms 35664 KB Output is correct
25 Correct 30 ms 35656 KB Output is correct
26 Correct 24 ms 35920 KB Output is correct
27 Correct 24 ms 35824 KB Output is correct
28 Correct 25 ms 35932 KB Output is correct
29 Correct 24 ms 35664 KB Output is correct
30 Correct 30 ms 36176 KB Output is correct
31 Correct 24 ms 35664 KB Output is correct
32 Correct 24 ms 36176 KB Output is correct
33 Correct 23 ms 35672 KB Output is correct
34 Correct 21 ms 41296 KB Output is correct
35 Correct 22 ms 35920 KB Output is correct
36 Correct 29 ms 35920 KB Output is correct
37 Correct 25 ms 35920 KB Output is correct
38 Correct 23 ms 35664 KB Output is correct
39 Correct 28 ms 36524 KB Output is correct
40 Correct 26 ms 36176 KB Output is correct
41 Correct 24 ms 35920 KB Output is correct
42 Correct 24 ms 35920 KB Output is correct
43 Correct 24 ms 36168 KB Output is correct
44 Correct 16 ms 35920 KB Output is correct
45 Correct 24 ms 35996 KB Output is correct
46 Correct 25 ms 35920 KB Output is correct
47 Correct 23 ms 35664 KB Output is correct
48 Correct 25 ms 36068 KB Output is correct
49 Correct 21 ms 36004 KB Output is correct
50 Correct 19 ms 36344 KB Output is correct
51 Correct 24 ms 35964 KB Output is correct
52 Correct 24 ms 36016 KB Output is correct
53 Correct 28 ms 35920 KB Output is correct
54 Correct 28 ms 36168 KB Output is correct
55 Correct 16 ms 35664 KB Output is correct
56 Correct 93 ms 45248 KB Output is correct
57 Correct 134 ms 51784 KB Output is correct
58 Correct 16 ms 36288 KB Output is correct
59 Correct 14 ms 35824 KB Output is correct
60 Correct 16 ms 35920 KB Output is correct
61 Correct 90 ms 45140 KB Output is correct
62 Correct 22 ms 36176 KB Output is correct
63 Correct 120 ms 57964 KB Output is correct
64 Correct 133 ms 51048 KB Output is correct
65 Correct 17 ms 36176 KB Output is correct
66 Correct 107 ms 47528 KB Output is correct
67 Correct 17 ms 36176 KB Output is correct
68 Correct 34 ms 35904 KB Output is correct
69 Correct 78 ms 44864 KB Output is correct
70 Correct 33 ms 36212 KB Output is correct
71 Correct 100 ms 45848 KB Output is correct
72 Correct 118 ms 48968 KB Output is correct
73 Correct 25 ms 35664 KB Output is correct
74 Correct 123 ms 58120 KB Output is correct
75 Correct 109 ms 52732 KB Output is correct
76 Correct 18 ms 36272 KB Output is correct
77 Correct 20 ms 36432 KB Output is correct
78 Correct 95 ms 45000 KB Output is correct
79 Correct 73 ms 41144 KB Output is correct
80 Correct 28 ms 36176 KB Output is correct
81 Correct 27 ms 35920 KB Output is correct
82 Correct 25 ms 35652 KB Output is correct
83 Correct 13 ms 35920 KB Output is correct
84 Correct 16 ms 36176 KB Output is correct
85 Correct 123 ms 47688 KB Output is correct
86 Correct 27 ms 35920 KB Output is correct
87 Correct 101 ms 44988 KB Output is correct
88 Correct 24 ms 35664 KB Output is correct
89 Correct 98 ms 44984 KB Output is correct
90 Correct 104 ms 44600 KB Output is correct
91 Correct 65 ms 41032 KB Output is correct
92 Correct 83 ms 45116 KB Output is correct
93 Correct 19 ms 36176 KB Output is correct
94 Correct 19 ms 35920 KB Output is correct
95 Correct 68 ms 42380 KB Output is correct
96 Correct 83 ms 48172 KB Output is correct
97 Correct 20 ms 36176 KB Output is correct
98 Correct 22 ms 35656 KB Output is correct
99 Correct 99 ms 45760 KB Output is correct
100 Correct 28 ms 36052 KB Output is correct
101 Correct 79 ms 45896 KB Output is correct
102 Correct 89 ms 50148 KB Output is correct
103 Correct 28 ms 35920 KB Output is correct
104 Correct 28 ms 36124 KB Output is correct
105 Correct 23 ms 36168 KB Output is correct
106 Correct 20 ms 45796 KB Output is correct
107 Correct 26 ms 35920 KB Output is correct
108 Correct 654 ms 88168 KB Output is correct
109 Correct 622 ms 118092 KB Output is correct
110 Correct 732 ms 137924 KB Output is correct
111 Correct 623 ms 108616 KB Output is correct
112 Correct 580 ms 90952 KB Output is correct
113 Correct 445 ms 72760 KB Output is correct
114 Correct 547 ms 87052 KB Output is correct
115 Correct 513 ms 85320 KB Output is correct
116 Correct 893 ms 140616 KB Output is correct
117 Correct 472 ms 70464 KB Output is correct
118 Correct 548 ms 81868 KB Output is correct
119 Correct 449 ms 69300 KB Output is correct
120 Correct 561 ms 92108 KB Output is correct
121 Correct 515 ms 69188 KB Output is correct
122 Correct 919 ms 154496 KB Output is correct
123 Correct 330 ms 68280 KB Output is correct
124 Correct 505 ms 66448 KB Output is correct