-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.cpp
More file actions
59 lines (51 loc) · 1.2 KB
/
5.cpp
File metadata and controls
59 lines (51 loc) · 1.2 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
#include <bits/stdc++.h>
using namespace std;
int n, m, s;
void bfs(vector<vector<int>> &lst, vector<int> &ss){
int cnt = 0;
vector<pair<int, int>> arrivals;
queue<pair<int, int>> q;
q.push({0, 1});
if (find(ss.begin(), ss.end(), 1) != ss.end()){
cout << "Yes";
return;
}
while(!q.empty()){
int limit = q.front().first;
int now = q.front().second;
q.pop();
if (lst[now].size() == 0) arrivals.push_back({limit, now});
else {
for (int i=0; i<lst[now].size(); i++){
int newlimit = limit;
int next = lst[now][i];
if (find(ss.begin(), ss.end(), next) != ss.end()){
newlimit++;
}
q.push({newlimit, next});
}
}
}
sort(arrivals.begin(), arrivals.end());
if(arrivals[0].first == 0) cout << "yes";
else cout << "Yes";
}
int main(void){
ios_base::sync_with_stdio(false); // scanf 사용시 아래 두 문장 삭제
cin.tie(nullptr);
cin >> n >> m;
vector<vector<int>> lst(n + 1, vector<int> ());
for (int i=0; i<m; i++){
int u, v;
cin >> u >> v;
lst[u].push_back(v);
}
cin >> s;
vector<int> ss(s);
for (int i=0; i<s; i++){
int w;
cin >> w;
ss.push_back(w);
}
bfs(lst, ss);
}