-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler.cpp
61 lines (52 loc) · 1 KB
/
euler.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
#include <iostream>
#include <list>
using namespace std;
class Graph {
int V;
list<int>* adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
~Graph() {
delete[] adj;
}
void addEdge(int v, int w);
};
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
adj[w].push_back(v);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
int u[100], v[100], counter[100];
//Input an undirected graph
Graph g(N);
for (int i = 0; i < M; i++) {
cin >> u[i] >> v[i];
g.addEdge(u[i], v[i]);
}
for (int i = 0; i < N; i++) {
counter[i] = 0;
for (int j = 0; j < M; j++) {
if (u[j] == i or v[j] == i)
counter[i]++;
}
}
int result = 0, min = -1, max = -1;
for (int j = 0; j < N; j++) {
if (counter[j] % 2 != 0) {
result++;
if (min == max)
min = j;
else max = j;
}
}
if (result == 0) cout << "CYCLE" << '\n';
else if (result == 2) cout << "PATH " << min << ' ' << max << '\n';
else cout << "IMPOSSIBLE" << '\n';
}