부록 D - Union-Find #18
Replies: 8 comments 2 replies
-
|
1717. 집합의 표현 #include <iostream>
#include <vector>
using namespace std;
vector<int> p(1000001, -1);
int find(int x) {
//while (p[x] > 0) x = p[x];
if (p[x] < 0) return x;
return p[x] = find(p[x]); //최적화2. 경로 압축
}
bool uni(int u, int v) {
u = find(u); // int u = find(a);
v = find(v);
if (u == v) return false; //이미 같은 그룹
//최적화1. union by rank
if (p[v] < p[u]) swap(u, v); // V의 랭크가 더 큼 = 자식이 더 길어서 U>=V의 랭크로 만듦
if (p[u] == p[v]) p[u]--; // 랭크가 같은 경우 랭크 1 증가(근데 음수라서 빼야 해)
p[v] = u; // v를 u의 자식으로 만든다
return true;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
int q, a, b;
while (m--) {
cin >> q >> a >> b;
if (q == 1) cout << ((find(a) == find(b) ? "YES" : "NO")) << "\n";
else uni(a, b);
}
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
|
14595. 동방 프로젝트 (Large) // 2372 0 561 근데 유니온파인드로 안 풂
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M; cin >> N >> M;
int x, y;
vector<pair<int, int>> vec;
for (int i = 0; i < M; ++i) {
cin >> x >> y;
vec.push_back({ x, y });
}
sort(vec.begin(), vec.end());
int xx = 0, yy = 0;
for (int i = 0; i < M; ++i) {
if (yy < vec[i].first) {
N -= yy - xx;
xx = vec[i].first;
yy = vec[i].second;
}
else {
if (yy < vec[i].second) yy = vec[i].second;
}
}
N -= yy - xx;
cout << N;
return 0;
}// 6120 12 958
vector<int> p; // (1000001, -1);
// 정석적인 유니온파인드 코드
int main() {
cin >> N >> M;
if (M == 0) {
cout << N << "\n";
return 0;
}
p.assign(N + 1, -1);
// 사이즈를 M으로 한정해 줘
vector<pair<int, int>> vec(M);
for (int i = 0; i < M; ++i) cin >> vec[i].first >> vec[i].second;
sort(vec.begin(), vec.end());
int x = vec[0].first, y = vec[0].second;
// i=1부터 시작
for (int i = 1; i < M; ++i) {
int xx = vec[i].first, yy = vec[i].second;
if (xx <= y) y = max(y, yy);
else {
// 구간 끊기기
for (int j = x; j < y; ++j) uni(j, j +1);
x = xx; y = yy;
}
}
// 마지막 구간 union
for (int j = x; j < y; ++j) uni(j, j + 1);
int cnt = 0;
for (int i = 1; i <= N; ++i) if (p[i] < 0) ++cnt;
cout << cnt;
return 0;
}#include <tuple> // tie를 쓰기 위해
int main() {
sort(vec.begin(), vec.end());
// 이렇게 풀어도 돼
int cnt = N;
int x = 0, y = 0, xx, yy;
for (int i = 0; i < M; ++i) {
// auto [xx, yy] = vec[i]; // 17 이상만 돼
tie(xx, yy) = vec[i];
x = max(x, xx);
y = max(y, yy);
for (int i = x; i < y; ++i) {
if (uni(i, i + 1)) --cnt;
}
x = y;
}
cout << cnt;
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
|
1976 | 여행 가자 int main() {
cin >> N >> M;
fill(p, p + MX, -1);
int tmp;
for (int i = 1; i <= N; ++i) { // 범위 주의
for (int j = 1; j <= N; ++j) {
cin >> tmp;
if (tmp) uni(i, j);
}
}
// M개 만큼을 돌면서 그룹이 다르면 break 걸고 "NO" 출력
int grp; cin >> tmp;
grp = find(tmp);
for (int i = 2; i <= M; ++i) {
cin >> tmp;
if (grp != find(tmp)) {
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
|
10775 | 공항 |
Beta Was this translation helpful? Give feedback.
-
|
17619 | 개구리 점프 |
Beta Was this translation helpful? Give feedback.
-
|
18116 | 로봇 조립 |
Beta Was this translation helpful? Give feedback.
-
|
2162 | 선분 그룹 |
Beta Was this translation helpful? Give feedback.
-
어려워!!! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
리드미
Beta Was this translation helpful? Give feedback.
All reactions