-
Notifications
You must be signed in to change notification settings - Fork 0
/
C - NP-Hard Problem.cpp
146 lines (126 loc) · 2.59 KB
/
C - 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
RAJAT SINGH
VIT VELLORE
*/
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define ll long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
//inline void get(int *p){register int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}
//inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define sc(a) scanf("%lld",&a)
# define ll long long
#define p(a) printf("%d\n",a)
#define l(i,a) for(int i=0;i<a;i++)
#define re(j,a,b) for( int j=a;j<=b;j++)
#define modu 1000000007
/*ll modular_pow(ll base, ll exponent)
{
ll result = 1;
while (exponent > 0)
{
if (exponent % 2 == 1)
result = (result * base) % MOD;
exponent = exponent >> 1;
base = (base * base) % MOD;
}
return result;
}
*/
int gcd(int a,int b){
if(b==0)
return a;//gcd
else return gcd(b,a%b);
}
bool chek(int x){
return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
return __builtin_popcount(n);//count number od set bits in a number
}
ll maxx(ll x, ll y){
if(x>=y) return x;
else return y;
}
void swap(int *x, int *y){
int t;
t= *x;
*x= *y;
*y=t;
}
/*string s;
void pr(int st, int e){
if(st==e){
cout<<s<<endl;
}
else re(i,st,e){
swap(&s[i],&s[st]);
pr(st+1,e);
swap(&s[i],&s[st]);
}
}*/
vector< int > vis;
int fl=1;
vector< int >seta;
vector< int >setb;
map< int ,set< int > >gr;
map< int , int >col;
//int fl=1;
void dfs(int x, int c){
vis[x]=1;
//col[c].push_back(x);
col[x]= c;
for(set< int >::iterator i= gr[x].begin();i!= gr[x].end();i++){
if(!vis[*i]){
if(col[x]==0) c=1;
else c=0;
dfs(*i, c);
}
else{
if(col[*i]==col[x]) fl=0;
}
}
}
int main(){
// freopen("inp.txt","r", stdin);
int n, m;
cin>>n>>m;
//gr= vector< vector< int > >(n+1);
vis= vector< int >(n+1, 0);
l(i, m){
int s, e;
cin>>s>>e;
gr[s].insert(e);
gr[e].insert(s);
}
int root=0;
for(int u=1;u<=n;u++){
if(gr[u].size()>0){
root= u;break;
}
}
//seta.push_back(root);
//int c=1;
for(int u=1;u<=n;u++){
if(vis[u]==0){
int c=1;
dfs(u,c);}}
if(!fl) puts("-1");
else{
for(map< int , int >::iterator i= col.begin();i!=col.end();i++){
if(i->second==0){
seta.push_back(i->first);
}
else setb.push_back(i->first);
}
cout<< seta.size()<<endl;
l(i, seta.size()) cout<< seta[i]<<" ";
cout<<endl;
cout<< setb.size()<<endl;
l(i, setb.size()) cout<< setb[i]<<" ";
cout<<endl;
}
return 0;}