-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_NP_Hard_Problem.cpp
More file actions
81 lines (74 loc) · 1.63 KB
/
A_NP_Hard_Problem.cpp
File metadata and controls
81 lines (74 loc) · 1.63 KB
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<ll>> adj;
vector<bool> vis;
vector<int> cmp;
set<int> st1,st2;
vector<int> colors;// 1 wela 2
bool dfs(ll node,int color) {
colors[node]=color;
for (ll neighbor : adj[node]) {
if(colors[neighbor]==color){
return false;
}
if (!colors[neighbor])
if (!dfs(neighbor, 3 - color)) {
return false;
}
}
return true;
}
void solve() {
ll n, m, y, x;
cin >> n >> m;
adj = vector<vector<ll>>(n);
colors=vector<int>(n,0);
vis=vector<bool>(n);
st1.clear();
st2.clear();
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--, y--;
adj[y].push_back(x);
adj[x].push_back(y);
}
bool ok=true;
int color=1;
for (ll i = 0; i < n; i++) {
color=3-color;
if(!colors[i] && !dfs(i,color)){
ok=false;
break;
}
}
if(!ok){
cout<<-1<<endl;
return;
}
for(int i=0;i<n;i++){
//cout<<i+1<<" : "<<colors[i]<<endl;
if(colors[i]==1){
st1.insert(i+1);
}else if(colors[i]==2){
st2.insert(i+1);
}
}
cout<<st1.size()<<endl;
for(int e:st1){
cout<<e<<" ";
}
cout<<endl;
cout<<st2.size()<<endl;
for(int e:st2){
cout<<e<<" ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}