-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1130AlienSecurity.cpp
More file actions
55 lines (54 loc) · 1.11 KB
/
1130AlienSecurity.cpp
File metadata and controls
55 lines (54 loc) · 1.11 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
#include<queue>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int n,e;
vector<vector<int> > g;
vector<vector<int> > rg;
vector<int> dist;
vector<int> vis;
void dfs(int u){
if(vis[u]) return;
vis[u] = 1;
for(int i=0;i<g[u].size();i++)
dfs(g[u][i]);
}
bool check(int i){
//put gurad in room i
vis = vector<int>(n,0);
vis[i] = 1;
dfs(0);
return !vis[e];
}
int bfs(){
dist = vector<int>(n,-1);
dist[e] = 0;
queue<int> q;
q.push(e);
while(!q.empty()){
int h = q.front();
q.pop();
int d = dist[h];
if(d && check(h)) return h;
for(int i=0;i<rg[h].size();i++){
int b = rg[h][i];
if(dist[b]!=-1) continue;
dist[b] = d+1;
q.push(b);
}
}
return -1;
}
int main()
{
cin>>n>>e;
g = vector<vector<int> >(n,vector<int>());
rg = vector<vector<int> >(n,vector<int>());
vis = vector<int>(n,0);
int a,b;
while(cin>>a>>b)
g[a].push_back(b), rg[b].push_back(a);
cout<<"Put guards in room "<<bfs()<<"."<<endl;
return 0;
}