-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosing.cpp
65 lines (59 loc) · 1.41 KB
/
closing.cpp
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
#include <iostream>
#include <vector>
#include <map>
#include <fstream>
using namespace std;
int find(int n, vector<int>& par) {
if (n != par[n]) return par[n] = find(par[n], par);
return n;
}
void unite(int a, int b, vector<int>& par, vector<int>& hei, int& clumps) {
int fa = find(a, par);
int fb = find(b, par);
if (fa != fb) {
clumps--;
if (hei[fa] < hei[fb])
par[fa] = fb;
else
par[fb] = fa;
if (hei[fa] == hei[fb]) {
hei[fa]++;
}
}
}
int main() {
ifstream fin("closing.in");
ofstream fout("closing.out");
int n, m;
fin >> n >> m;
vector<vector<int>> adj(n);
vector<int> ord;
map<int,bool> open;
for (int i=0; i<m; i++) {
int a, b; fin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=0; i<n; i++) {
int a; fin >> a; a--;
ord.push_back(a);
}
vector<int> par(n), hei(n);
for (int i=0; i<n; i++) par[i] = i;
vector<bool> res;
int sz = n;
for (int i=n-1; i>=0; i--) {
for (int node : adj[ord[i]]) {
if (open[node]) {
unite(node, ord[i], par, hei, sz);
}
}
int closed = i;
res.push_back(sz - closed == 1);
open[ord[i]] = 1;
}
for (int i=n-1; i>=0; i--) {
fout << (res[i] ? "YES\n" : "NO\n");
}
}