-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10596.cpp
89 lines (81 loc) · 1.52 KB
/
10596.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
86
87
88
89
/*
10596 - Morning Walk
*/
#include<bits/stdc++.h>
using namespace std;
bool check_degree(int n,const vector<int>& degree)
{
for(int i=0; i<n; i++)
{
if(degree[i]%2==1)
{
return false;
}
}
return true;
}
void dfs(int n,int i,const vector<vector<bool>>&graph,vector<bool>&visited)
{
visited[i]=true;
const vector<bool>&g = graph[i];
for(int j=0; j<n; j++)
{
if(g[j] && !visited[j])
{
dfs(n,j,graph,visited);
}
}
}
bool check_connection(int n, const vector<int>& degree, const vector<vector<bool>>&graph)
{
vector<bool>visited(n,false);
int start;
for(start=0; start<n; start++)
if(degree[start])
break;
if(start<n)
dfs(n,start,graph,visited);
for(int i=0; i<n; i++)
if(degree[i])
{
if(!visited[i])
return false;
}
return true;
}
int main()
{
int n,r;
while(cin>>n>>r)
{
vector<int>degree(n,0);
vector<vector<bool> >graph(n,vector<bool>(n,false));
for(int i=0; i<r; i++)
{
int a,b;
cin>>a>>b;
degree[a]++;
degree[b]++;
graph[a][b]=graph[b][a]=true;
}
if(r && check_degree(n,degree) && check_connection(n,degree,graph))
{
cout<<"Possible\n";
}
else
{
cout<<"Not Possible\n";
}
}
}
/*
Sample Input
2 2
0 1
1 0
2 1
0 1
Sample Output
Possible
Not Possible
*/