반응형
https://www.acmicpc.net/problem/9938
9938번: 방 청소
처음 6개의 술은 규칙 1에 의해서 1, 3, 5, 7, 9, 2번 서랍에 보관할 수 있다. 7번째 술은 규칙 3을 적용할 수 있다. 1번 서랍에 들어있는 술을 2로, 2번 서랍에 들어있는 술을 3으로, 3번 서랍에 들어있
www.acmicpc.net
Union&Find 알고리즘(dis-joint set)을 이용해서 풀어야 하는 문제다
풀이를 생각해내기 어려운 문제
#include <bits/stdc++.h>
using namespace std;
int n, l, unf[300001];
bool isfull[300001] ;
int Find(int x) {
if (unf[x] < 0) return x;
return unf[x] = Find(unf[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a == b) return;
if (isfull[a]) {
unf[a] = b;
}
else if (isfull[b]) {
unf[b] = a;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(unf, -1, sizeof(unf));
cin >> n >> l;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
if (!isfull[Find(a)]) {
isfull[Find(a)] = 1;
Union(a, b);
cout << "LADICA\n";
}
else if (!isfull[Find(b)]) {
isfull[Find(b)] = 1;
Union(a, b);
cout << "LADICA\n";
}
else {
cout << "SMECE\n";
}
}
return 0;
}

반응형
'Algorithm > problem' 카테고리의 다른 글
백준 15459번 : Haybale Feast - Union&Find dis-joint set C++ 문제풀이 (0) | 2022.03.02 |
---|---|
백준 10775번 : 공항 - dis-joint set , Union&Find C++ (0) | 2022.03.01 |
백준 11085번 : 군사 이동 - Union & Find (0) | 2022.03.01 |
백준 3197번 : 백조의 호수 BFS Union&Find C++ (0) | 2022.03.01 |
백준 14868번 : 문명 - BFS Union&Find C++ (0) | 2022.03.01 |