-
Notifications
You must be signed in to change notification settings - Fork 10
/
Codeforces-A - NP-Hard Problem.cpp
95 lines (80 loc) · 1.76 KB
/
Codeforces-A - NP-Hard Problem.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
90
91
92
93
94
95
// Bicolouring problem , slightly modified
#include<bits/stdc++.h>
using namespace std;
const int mx=1e5+1;
vector<int>zero;
vector<int>one;
int color[mx];
vector<int>nodes[mx];
bool isPosiible(int vertex)
{
queue<int>Q;
Q.push(vertex);
color[vertex]=0;
zero.push_back(vertex);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0;i<nodes[u].size();i++)
{
int v=nodes[u][i];
if(color[v]==-1)
{
if(color[u]==0){
color[v]=1;
one.push_back(v);
}
else{
color[v]=0;
zero.push_back(v);
}
Q.push(v);
}
if(color[u]==color[v])
{
return false;
}
}
}
}
int main()
{
int n,m,a,b;
cin>>n>>m;
int ver=INT_MAX;
while(m--)
{
cin>>a>>b;
ver=min(a,min(b,ver));
nodes[a].push_back(b);
nodes[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
color[i]=-1;
}
for(int i=1;i<=n;i++){
if(color[i]==-1){
if(nodes[i].size()==0){ // This node has no connection with other nodes
continue;}
if(!isPosiible(i))
{
cout<<"-1"<<endl;
return 0;
}
}
}
cout<<(int)zero.size()<<endl;
for(int j=0;j<zero.size();j++)
{
cout<<zero[j]<<" ";
}
cout<<endl;
cout<<(int)one.size()<<endl;
for(int j=0;j<one.size();j++)
{
cout<<one[j]<<" ";
}
cout<<endl;
}