forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.2.cpp
44 lines (41 loc) · 847 Bytes
/
4.2.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
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100;
bool g[maxn][maxn], visited[maxn];
int n;
queue<int> q;
void init(){
memset(g, false, sizeof(g));
memset(visited, false, sizeof(visited));
}
bool route(int src, int dst){
q.push(src);
visited[src] = true;
while(!q.empty()){
int t = q.front();
q.pop();
if(t == dst) return true;
for(int i=0; i<n; ++i)
if(g[t][i] && !visited[i]){
q.push(i);
visited[i] = true;
}
}
return false;
}
int main(){
freopen("4.2.in", "r", stdin);
init();
int m, u, v;
cin>>n>>m;
for(int i=0; i<m; ++i){
cin>>u>>v;
g[u][v] = true;
}
cout<<route(0, 6)<<endl;
fclose(stdin);
return 0;
}