Baekjoon #1976

Baekjoon #1976

2개의 풀이 · C++, Python

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
#include<bits/stdc++.h>

using namespace std;

int uf[40404];

int find(int x) {
    if(uf[x] < 0) return x;
    return uf[x] = find(uf[x]);
}

bool merge(int a, int b) {
    a = find(a);
    b = find(b);
    if(a == b)return false;
    uf[b] = a;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, M; cin >> N >> M;
    for(int i=0;i<=N;i++)uf[i] = -1;
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++) {
            int x;cin >> x;
            if(x) merge(i, j);
        }
    }
    int pre; cin >> pre;
    bool answer = true;
    for(int i=1;i<M;i++){
        int cur; cin >> cur;
        if(find(pre) != find(cur)) answer = false;
        pre = cur;
    }
    if(answer) cout << "YES";
    else cout << "NO";

    return 0;
}

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.