From 17a1ca18b005b1db02b84bb6db2a6f63788f8cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9C=A0=ED=99=98?= Date: Fri, 3 Oct 2025 17:46:29 +0900 Subject: [PATCH] =?UTF-8?q?14595=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/14595.cpp | 45 +++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/Appendix D/solutions/14595.cpp b/Appendix D/solutions/14595.cpp index 6c991660..a71351ea 100644 --- a/Appendix D/solutions/14595.cpp +++ b/Appendix D/solutions/14595.cpp @@ -1,11 +1,48 @@ -// Authored by : BaaaaaaaaaaarkingDog +// Authored by : uhwan0723 // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/6baaf9ffbfe746fd88488265801717b4 #include using namespace std; +const int mn = 1e6+5; +int n, m, par[mn], nxt[mn]; +/* +nxt[i]: i번 방이 속해있는 집합에 속하지 않으면서, 그 집합의 오른쪽에 위치하고, 그 중에서 번호가 가장 낮은 방 +예를 들어 i, i+1이 한 집합이면 nxt[i] = nxt[i+1] = i+2 +초기에 각 방은 자기 자신만 있는 집합에 속하기 때문에, nxt[i]의 초기값은 i+1 +*/ + +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; +} + int main(void){ ios::sync_with_stdio(0); cin.tie(0); - -} \ No newline at end of file + cin >> n >> m; + fill(par, par+n+5, -1); + fill(nxt, nxt+n+5, mn); + for(int i = 1; i <= n; ++i) + nxt[i] = i+1; + while(m--){ + int x, y; + cin >> x >> y; + for(int i = nxt[find(x)]; i <= y; i = nxt[find(i)]){ + nxt[find(x)] = nxt[find(i)]; + uni(x, i); + } + } + int res = 0; + for(int i = 1; i <= n; ++i) + res += par[i] < 0; + cout << res; +}