-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eulerian_path_checker_class .cpp
72 lines (67 loc) · 1.16 KB
/
Eulerian_path_checker_class .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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class graph
{
int node;
vector< vector<int> >v;
public:
graph(int x)
{
node=x;
v=vector< vector<int> >(x+9);
}
void addpath(int q,int w);
int iscycle();
int isconnected();
void dfs(int x,vector<bool>&visited);
};
void graph::addpath(int q,int w)
{
v[q].push_back(w);
v[w].push_back(q);
}
void graph::dfs(int edge,vector<bool>&visited)
{
visited[edge]=1;
for(int i=0; i<v[edge].size(); i++)
{
if(visited[v[edge][i]]==0)
{
dfs(v[edge][i],visited);
}
}
}
int graph::isconnected()
{
vector<bool>visited(node+9,0);
int i;
for(i=0;i<node;i++)
{
if(v[i].size()!=0)
break;
}
if(i==node)
return 1;
dfs(i,visited);
for(i=0;i<node;i++)
{
if(!visited[i] && v[i].size()>0)
return 0;
}
return 1;
}
int graph::iscycle()
{
if(isconnected()==0)
return 0;
int odd=0;
for(int i=0;i<node;i++)
{
if(v[i].size() & 1)
odd++;
}
if(odd>2)
return 0;
return odd? 1:2;
}