forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsBARPARTITE.cpp
60 lines (58 loc) · 1.16 KB
/
IsBARPARTITE.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define dopamine ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
int maxi = INT_MAX, mini = INT_MIN;
bool visited[100005] = {false};
int col[100005];
vector<int>arr[100005];
void initialise(int a)
{
for (int i = 0; i < a + 1; i++)
{ col[i] = 0;
visited[i] = false;
}
}
bool isbipartite(int a, int b)
{
visited[a] = true;
col[a] = b;
for (int child : arr[a])
{
if (!visited[child])
{
visited[child] = true;
if (isbipartite(child, b ^ 1) == false)
return false;
}
else
{
if (col[a] == col[child])
return false;
}
}
return true;
}
int32_t main()
{
//serotonin();
dopamine
int t = 1;//cin >> t;
while (t--)
{
int a, b; cin >> a >> b;
for (int i = 0; i < b; i++)
{
int q, w;
cin >> q >> w;
arr[q].push_back(w);
}
initialise(a);
if (isbipartite(1, 0))
cout << 1;
else
cout << 0;
}
return 0;
}