-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhas_path.cpp
More file actions
68 lines (68 loc) · 1.54 KB
/
has_path.cpp
File metadata and controls
68 lines (68 loc) · 1.54 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
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
long long n;
string s;
typedef long long ll;
#define nit( i, n) for(i=0;i<n;i++)
int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b);}
bool iseven(int n){return ((n&1)==0)?true:false;}
int pov(int n,int m){ int c=1; while(m>0){ if(!iseven(m)){ c=c*n; } n=n*n; m=m>>1; } return c; }
unsigned int intlen(unsigned int n){return n ? intlen(n/10)+1 : 0;}
bool isprime(int x) { for (int d = 2; d * d <= x; d++) { if (x % d == 0) return false; }return true; }
bool dfs(int** ed,ll st,ll n,ll end,int* visit){
//cout<<st<<" ";
if(st==end){
return true;
}
visit[st]=1;
for(int i=0;i<n;i++){
if(i==st){
continue;
}
else{
if(ed[st][i]==1&&visit[i]!=1){
if(dfs(ed,i,n,end,visit)){
return true;
}
}
}
}
return false;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("ipt.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ll t;
cin>>t;
while(t--){
ll edg,st,e;
cin>>n>>edg;
cin>>st>>e;
int** ed=new int*[n];
for(int i=0;i<n;i++){
ed[i]=new int[n];
for(int j=0;j<n;j++){
ed[i][j]=0;
}
}
for(int i=0;i<edg;i++){
ll x,y;
cin>>x>>y;
ed[x][y]=1;
ed[y][x]=1;
}
int* visit=new int[n];
for(int i=0;i<n;i++){
visit[i]=0;
}
if(dfs(ed,st,n,e,visit)){
cout<<"true"<<endl;
}
else{
cout<<"false"<<endl;
}
}
return 0;
}