forked from mrhagrawal/competitive_programming_resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp file
105 lines (97 loc) · 1.77 KB
/
cpp file
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
vector<ll>v[100005],rv[100005];
stack<int>s ;
queue<ll>q;
ll visited[100005]={0};
ll visited1[100005]={0};
ll recstack[1003]{0};
ll dist[100005];
void transpose(ll n)
{
ll i,j;
for(i=1;i<=n;i++)
{
for(j=0;j<v[i].size();j++)
{
rv[v[i][j]].push_back(i);
}
}
}
void dfs1(ll u)
{
ll i;
visited1[u]=1;
//cout<<u<<" ";
for(i=0;i<rv[u].size();i++)
{
if(visited1[rv[u][i]]==0)
dfs1(rv[u][i]);
}
}
void dfs(ll u) // dfs function
{
ll i;
visited[u]=1;
for(i=0;i<v[u].size();i++)
{
if(visited[v[u][i]]==0)
dfs(v[u][i]);
}
s.push(u);
}
ll solvedist(ll u) // main function
{
ll i;
for(i=0;i<100005;i++)
dist[i]=-1;
visited[u]=1;
dist[u]=1;
q.push(u);
while(!q.empty())
{
ll d=q.front();
q.pop();
for(i=0;i<v[d].size();i++)
{
if(visited[v[d][i]]==0&&dist[v[d][i]]==-1)
{
dist[v[d][i]]=1-dist[d];
q.push(v[d][i]);
visited[v[d][i]]=1;
}
else
{
if(visited[v[d][i]]==0&&dist[v[d][i]]==dist[d])
return 0;
if(visited[v[d][i]]==1&&dist[v[d][i]]==dist[d])
return 0;
}
}
}
return 1;
}
int main()
{
ll n,m,i,j,k,l,u,vx,x,y,c=0,flag=0;
cin>>n>>m;
for(i=0;i<m;i++)
{
cin>>x>>y;
if(x==y)
flag=1;
v[x].push_back(y);
v[y].push_back(x);
}
cin>>u>>vx;
k=solvedist(1);
if(flag==1)
{
cout<<0;
return 0;
}
//cout<<endl;
cout<<k;
return 0;
}