Skip to content

Commit b1b641f

Browse files
authored
[20250429] BOJ / P2 / 민혁이의 게임 파티 / 권혁준
1 parent 924c6bc commit b1b641f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
```cpp
2+
#include <iostream>
3+
#include <numeric>
4+
#include <set>
5+
using namespace std;
6+
7+
int N, M, Q;
8+
multiset<int> S[100001]{};
9+
int A[100001]{}, C[100001]{}, r[100001]{}, ans[100001]{};
10+
11+
int f(int x) { return x == r[x] ? x : r[x] = f(r[x]); }
12+
13+
int main() {
14+
cin.tie(0)->sync_with_stdio(0);
15+
16+
cin >> N >> M >> Q;
17+
for (int i = 1; i <= N; i++) {
18+
cin >> A[i];
19+
S[i].insert(A[i]);
20+
C[A[i]]++;
21+
}
22+
for (int i = 1; i <= M; i++) ans[i] = C[i] == 1 ? 0 : -1;
23+
24+
iota(r, r + N + 1, 0);
25+
26+
for (int a, b, t = 1; Q--; t++) {
27+
cin >> a >> b;
28+
int x = f(a), y = f(b);
29+
if (x == y) continue;
30+
if (S[x].size() > S[y].size()) swap(x, y);
31+
for (int j : S[x]) {
32+
S[y].insert(j);
33+
if (C[j] == S[y].count(j) && ans[j] < 0) ans[j] = t;
34+
}
35+
S[x] = multiset<int>();
36+
r[x] = y;
37+
}
38+
39+
for (int i = 1; i <= M; i++) cout << ans[i] << '\n';
40+
41+
}
42+
```

0 commit comments

Comments
 (0)