0. [c++] 백준


https://www.acmicpc.net/problem/1976


 1. 풀이



 2. 소스코드


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
 
using namespace std;
 
const int INF = 1000000007;
int N, M;
int arr[1000001];
 
int find(const int& x) {
    if (x == arr[x])
        return x;
    else
        return arr[x] = find(arr[x]);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N >> M;
 
    for (int i = 1; i <= N; i++) {
        arr[i] = i;
    }
 
    int temp;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            cin >> temp;
            if (temp == 1) {
                int a = i;
                int b = j;
                a = find(a);
                b = find(b);
                if (a > b)
                    arr[a] = b;
                else
                    arr[b] = a;
            }
        }
    }
 
    bool flag = true;
    cin >> temp;
    int here = find(temp);
    for (int i = 1; i < M; i++) {
        cin >> temp;
        int there = find(temp);
        if (here != there)
            flag = false;
    }
 
    if (flag)
        cout << "YES\n";
    else
        cout << "NO\n";
 
    return 0;
}
cs

 3. 참고




질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


728x90
반응형

'<백준> > 유니온 파인드' 카테고리의 다른 글

[c++] 백준 4195 - 친구 네트워크  (0) 2020.04.19
[c++] 백준 1717 - 집합의 표현  (0) 2020.04.18

+ Recent posts