This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact6.cpp
85 lines (68 loc) · 1.49 KB
/
act6.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
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
/******************************************************************************
CS 325 Activity 6 Babyface & Heels
Sample Test Case
Input:
4
4
1 2
1 3
4 2
4 3
Output:
Possible
Save file as act6.cpp before submitting to Gradescope
*******************************************************************************/
#include <iostream>
#include <fstream>
#include <queue>
int G[100][100]; // if you want to use an adjacency matrix
bool babyfaceHeel(int matrix[100][100], int n) {
int array[n];
for (int i = 0; i < n; i++)
array[i] = -1;
array[0] = 1;
std::queue<int> que;
que.push(0);
while (!que.empty()) {
int u = que.front();
que.pop();
if (G[u][u] == 1)
return false;
for (int i = 0; i < n; i++) {
if (G[u][i] == 1 && array[i] == -1) {
array[i] = 1 - array[u];
que.push(i);
}
else if (G[u][i] == 1 && array[i] == array[u])
return false;
}
}
return true;
}
int main () {
// Create a graph given in the above diagram
int n; // number of wrestlers numbered 1..n
int m; // number of rivalries
int w1, w2;
std::cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
G[i][j]=0;
std::cin >> m;
for (int j = 0; j < m; j++) {
// 1st wrestler
std::cin >> w1;
// 2nd wrestler
std::cin >> w2;
w1--;
w2--;
// Add edges to adjacency matrix optional
G[w1][w2] = 1;
G[w2][w1] = 1;
}
if (babyfaceHeel(G,n))
std::cout << "Possible" << std::endl;
else
std::cout << "Impossible" << std::endl;
return 0;
}