Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 시뮬레이션
- 정렬
- 스택
- BFS
- ue5
- 재귀
- 유니온 파인드
- 알고리즘
- XR Interaction Toolkit
- Team Fortress 2
- 유니티
- 백준
- 그리디 알고리즘
- 브루트포스
- 투 포인터
- 백트래킹
- DFS
- 다이나믹 프로그래밍
- 문자열
- 자료구조
- 구현
- 트리
- VR
- 다익스트라
- 그래프
- 수학
- 누적 합
- 우선순위 큐
- c++
- Unreal Engine 5
Archives
- Today
- Total
1일1알
백준 1976번 여행 가자 C++ 본문
연결되어있는 경로를 유니온 파인드를 이용해서 저장하고 여행 계획에 속한 도시들이 전부 이어져있으면 YES, 아니면 NO를 출력했다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m;
vector<int> parent;
vector<int> height;
int GetParent(int n) {
if (n == parent[n])
return n;
return parent[n] = GetParent(parent[n]);
}
void Merge(int u, int v) {
u = parent[u];
v = parent[v];
if (GetParent(u) == GetParent(v))
return;
if (height[u] > height[v])
::swap(u, v);
parent[u] = v;
if (height[u] == height[v])
height[v]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
parent = vector<int>(n + 1);
height = vector<int>(n + 1, 1);
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int input;
cin >> input;
if (input == 0) continue;
if (GetParent(i) == GetParent(j)) continue;
Merge(i, j);
}
}
set<int> s;
for (int i = 0; i < m; i++) {
int input;
cin >> input;
int parent = GetParent(input);
s.insert(parent);
}
if (s.size() == 1) cout << "YES";
else cout << "NO";
}
'알고리즘' 카테고리의 다른 글
백준 1922번 네트워크 연결 C++ (0) | 2022.08.09 |
---|---|
백준 1707번 이분 그래프 C++ (0) | 2022.08.08 |
백준 1039번 교환 C++ (0) | 2022.08.06 |
백준 21924번 도시 건설 C++ (0) | 2022.08.05 |
백준 10710번 실크로드 C++ (0) | 2022.08.03 |