-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCricket Match
78 lines (63 loc) Β· 1.71 KB
/
Cricket Match
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
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
bool dfs(vector<vector<int>>& adj, bool visitedStatus[], bool team[], int v, bool teamToBeAssigned){
//visit the node, mark visitedStatus = true;
//check whether neighbouring visited teams are same. If yes, return false
//Else, dfs & if no contradiction, return true
//Check for disconnected components as well
visitedStatus[v] = true;
team[v] = teamToBeAssigned;
for (int neighbour : adj[v])
{
if (visitedStatus[neighbour] && team[neighbour] == teamToBeAssigned)
return false;
}
for (int neighbour: adj[v]){
/*if (visitedStatus[neighbour]){
if (team[neighbour] == teamToBeAssigned)
return false;
}*/
if (!visitedStatus[neighbour]){
if (!dfs(adj, visitedStatus, team, neighbour, !teamToBeAssigned))
return false;
}
}
return true;
}
int main() {
// your code goes here
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
bool visitedStatus[n] = {false};
bool team[n];
while(m--){
int u, v;
cin >> u >> v;
adj[u-1].push_back(v-1);
adj[v-1].push_back(u-1);
}
if (!dfs(adj, visitedStatus, team, 0, 0))
{
cout << "NO\n";
continue;
}
int i;
for (i = 0; i <n; ++i)
{
if (!visitedStatus[i] && !dfs(adj, visitedStatus, team, i, 0))
{
cout << "NO\n";
break;
}
}
if (i == n)
cout << "YES" << endl;
}
return 0;
}