-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1976.cpp
More file actions
68 lines (56 loc) · 903 Bytes
/
1976.cpp
File metadata and controls
68 lines (56 loc) · 903 Bytes
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
64
65
66
67
68
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int U[201];
int find(int a) {
if (U[a] < 0)return a;
return U[a] = find(U[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)return;
if (U[a] < U[b]) {
U[a] += U[b];
U[b] = a;
}
else {
U[b] += U[a];
U[a] = b;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fill(&U[0], &U[201], -1);
int n;
cin >> n;
int m;
cin >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int temp;
cin >> temp;
if (temp == 1) {
merge(i, j);
}
}
}
int prev;
for (int i = 0; i < m; i++) {
int temp;
cin >> temp;
if (i == 0) {
prev = find(temp);
}
else {
if (prev != find(temp)) {
cout << "NO";
return 0;
}
}
}
cout << "YES";
}