From 1c0cc64247a2e73f0442d3945f90cfecf8a95777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9C=A0=ED=99=98?= Date: Thu, 2 Oct 2025 08:43:50 +0900 Subject: [PATCH] =?UTF-8?q?18116=EB=B2=88=20=ED=92=80=EC=9D=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Appendix D/solutions/18116.cpp | 39 ++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Appendix D/solutions/18116.cpp b/Appendix D/solutions/18116.cpp index 6c991660..3b87220b 100644 --- a/Appendix D/solutions/18116.cpp +++ b/Appendix D/solutions/18116.cpp @@ -1,11 +1,42 @@ -// Authored by : BaaaaaaaaaaarkingDog +// Authored by : uhwan0723 // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/23f94068108e4bdba086c2b011dc5a3a #include using namespace std; +const int mx = 1e6+5; +int n, a, b, c, par[mx], sz[mx]; + +int find(int x){ + if(par[x] < 0) return x; + return par[x] = find(par[x]); +} + +void uni(int x, int y){ + int u = find(x), v = find(y); + if(u == v) return; + if(-par[u] < -par[v]) swap(u, v); + if(par[u] == par[v]) --par[u]; + par[v] = u; + sz[u] += sz[v]; +} + int main(void){ ios::sync_with_stdio(0); cin.tie(0); - -} \ No newline at end of file + cin >> n; + fill(par, par+mx, -1); + fill(sz, sz+mx, 1); + while(n--){ + char command; + cin >> command; + if(command == 'I'){ + cin >> a >> b; + uni(a, b); + } + else{ + cin >> c; + cout << sz[find(c)] << '\n'; + } + } +}