-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01976_Union-Find.cpp
More file actions
64 lines (55 loc) · 1.05 KB
/
01976_Union-Find.cpp
File metadata and controls
64 lines (55 loc) · 1.05 KB
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
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 200;
int n, m;
int route[MAX + 1];
vector<int> plan;
int do_find(int n)
{
if (n == route[n])
return n;
return route[n] = do_find(route[n]);
}
void do_union(int n1, int n2)
{
int s1 = do_find(n1);
int s2 = do_find(n2);
if (s1 > s2)
route[s2] = s1;
else if (s1 < s2)
route[s1] = s2;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++)
route[i] = i;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int t;
cin >> t;
if (t)
do_union(i, j);
}
}
int a;
while (cin >> a)
plan.push_back(a);
int is_route = true;
for (int i = 1; i < plan.size(); i++)
{
if (do_find(plan[i - 1]) != do_find(plan[i]))
is_route = false;
}
if (is_route)
cout << "YES";
else
cout << "NO";
return 0;
}