From f306ba87c60180d938305534d45e25c3bcde7ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9C=A0=ED=99=98?= Date: Wed, 1 Oct 2025 09:08:15 +0900 Subject: [PATCH] =?UTF-8?q?20040=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/20040.cpp | 39 +++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/Appendix D/solutions/20040.cpp b/Appendix D/solutions/20040.cpp index 6c991660..1da8b618 100644 --- a/Appendix D/solutions/20040.cpp +++ b/Appendix D/solutions/20040.cpp @@ -1,11 +1,40 @@ -// Authored by : BaaaaaaaaaaarkingDog +// Authored by : uhwan0723 // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/f654dc349b074299a388938b0c5b35a8 #include using namespace std; -int main(void){ +const int mn = 500005; +int res, n, m, par[mn]; + +int find(int x){ + if(par[x] < 0) return x; + return par[x] = find(par[x]); +} + +bool uni(int x, int y){ + int u = find(x), v = find(y); + if(u == v) return false; + if(-par[u] < -par[v]) std::swap(u, v); + if(par[u] == par[v]) --par[u]; + par[v] = u; + return true; +} + +int main(){ ios::sync_with_stdio(0); cin.tie(0); - -} \ No newline at end of file + + cin >> n >> m; + fill(par, par+n+1, -1); + while(m--){ + ++res; + int u, v; + cin >> u >> v; + if(!uni(u, v)){ + cout << res; + return 0; + } + } + cout << 0; +}