728x90
반응형
- 풀이 링크:
GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이
알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.
github.com
#include <iostream>
#include <algorithm>
//#include <map> // 중복 x
//#include <string> // getline
//#include <cstring> // memset, strok, strstr
//#include <vector>
//#include <queue>
//#include <set> // 트리, 중복 x
//#include <unordered_set>
//#include <cmath>
using namespace std;
/*
[백준 1976 c++ O] 여행 가자
접근: 유니온 파인드
중복 허용이므로 같은 그래프에 연결만 되어있으면 어느 순서로도 탐색 가능 -> 같은 집합인지만 확인 -> 유니온 파인드
시간복잡도:
풀이1:
//1.입력
//2.부모 배열 초기화
//3.유니온
//4.파인드: 만약 같은 집합이면 YES, 아니면 NO
*/
int n,m,c;
int par[205];
int ad[205][205];
int curFin;
bool ok = 1;
int fin(int node) {
if (par[node] == node) return node;
else return par[node] = fin(par[node]); // 경로 압축: 시간 단축
}
void uni(int a, int b) {
int rootA = fin(a);
int rootB = fin(b);
par[rootA] = rootB;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
//1.입력
cin >> n >> m;
//2.부모 배열 초기화
for (int i = 0; i <= n; i++) {
par[i] = i;
}
//3.유니온
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> ad[i][j];
if (ad[i][j] == 1) {
uni(i, j);
}
}
}
//4.파인드: 만약 같은 집합이면 YES, 아니면 NO
for (int i = 0; i < m; i++) {
cin >> c;
if (curFin == 0) curFin = fin(c);
else {
if (curFin != fin(c)) {
ok = 0;
break;
}
}
}
if (ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
return 0;
}
반응형
'Algorithm_BOJ(백준) > 유니온파인드(UnionFind)' 카테고리의 다른 글
[백준 17352 c++ O] 여러분의 다리가 되어 드리겠습니다! (0) | 2023.01.26 |
---|---|
[백준 1043 c++ O] 거짓말 (0) | 2023.01.26 |
[백준 4195 c++ V] 친구 네트워크 (2) | 2023.01.25 |
[백준 1717 c++ VV] 집합의 표현 (0) | 2023.01.24 |
[백준 1717 c++ V] 집합의 표현 (0) | 2023.01.16 |