From 673328d73d971e773c56d58929c364a6e2b64577 Mon Sep 17 00:00:00 2001 From: encrypted-def <20028331+encrypted-def@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:11:07 +0900 Subject: [PATCH] Update 7511.cpp --- Appendix D/solutions/7511.cpp | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/Appendix D/solutions/7511.cpp b/Appendix D/solutions/7511.cpp index 6c991660..d9bfabc9 100644 --- a/Appendix D/solutions/7511.cpp +++ b/Appendix D/solutions/7511.cpp @@ -1,11 +1,55 @@ // Authored by : BaaaaaaaaaaarkingDog // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/48d21e5cb37f46978ecca75d63feebb0 #include using namespace std; -int main(void){ +vector p(1000001, -1); + +int find(int x){ + if(p[x] < 0) + return x; + return p[x] = find(p[x]); +} + +bool uni(int u, int v){ + u = find(u); + v = find(v); + if(u == v) + return false; + if(p[v] < p[u]) // v의 랭크가 더 큰 경우 + swap(u, v); // u, v를 swap + // 위의 if문으로 인해 u의 랭크 >= v의 랭크이다 + if(p[u] == p[v]) // 랭크가 같은 경우에 대한 처리 + p[u]--; + p[v] = u; // v를 u의 자식으로 만든다 + return true; +} + +int main(){ ios::sync_with_stdio(0); cin.tie(0); + int t; + cin >> t; -} \ No newline at end of file + for(int tc = 1; tc <= t; tc++){ + cout << "Scenario " << tc << ":\n"; + int n, k; + cin >> n >> k; + fill(p.begin() + 1, p.begin() + n + 1, -1); + while(k--){ + int a, b; + cin >> a >> b; + uni(a, b); + } + + int m; + cin >> m; + while(m--){ + int u, v; + cin >> u >> v; + cout << (find(u) == find(v)) << '\n'; + } + cout << '\n'; + } +}