BOJ 1976: 여행가자 작성일 2020-02-23 https://www.acmicpc.net/problem/1976 2월동안 187문제 풀기(지난달 누적 87개) (61/187) 유니온-파인드 1234567891011121314151617181920212223242526272829303132333435363738#include <bits/stdc++.h> using namespace std; int N, M, x, s, parent[205], height[205]; int find(int x) { return parent[x] = (parent[x] == x ? x : find(parent[x])); } void uni(int a, int b) { a = find(a), b = find(b); if(a == b) return; if(height[a] > height[b]) swap(a,b); parent[a] = b; if(height[a] == height[b]) ++height[b]; } int main() { cin >> N >> M; iota(parent, parent+N+1, 0); for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) { cin >> x; if(!x) continue; uni(i,j); } bool pos = true; cin >> s; s = find(s); for(int i = 1; i < M; i++) { cin >> x; if(find(x) != s) { pos = false; break; } } cout << (pos ? "YES" : "NO"); }