Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or
(or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
4 2 1 2 2 3
1 2 2 1 3
3 3 1 2 2 3 1 3
-1
②:遍历n个点,只要这个点的标记值lor[i]=0就对它进行搜索,并初始让它进入左集合(lor[k]=1)
④:假设搜到了x点,然后p点和x点相邻,那么如果lor[x]=1且lor[p]=0,就让lor[p] = -1并继续搜索p点,如果lor[x]=1且lor[p]=-1那么就说明p点已经搜过,没必要继续搜,但若lor[p]=lor[x]=1,见③,对于lor[x]=-1方法类似
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> v[100005], a, b;
int ok = 1, lor[100005] = {0};
void Sech(int x);
int main(void)
{
int i, n, m, c, d;
scanf("%d%d", &n, &m);
for(i=1;i<=m;i++)
{
scanf("%d%d", &c, &d);
v[c].push_back(d);
v[d].push_back(c);
}
for(i=1;i<=n;i++)
{
if(lor[i]==0)
{
lor[i] = 1;
a.push_back(i);
Sech(i);
}
}
if(ok==0)
printf("-1\n");
else
{
printf("%d\n", a.size());
for(i=0;i<a.size();i++)
{
if(i==0)
printf("%d", a[i]);
else
printf(" %d", a[i]);
}
printf("\n%d\n", b.size());
for(i=0;i<b.size();i++)
{
if(i==0)
printf("%d", b[i]);
else
printf(" %d", b[i]);
}
printf("\n");
}
return 0;
}
void Sech(int x)
{
int i, t;
if(ok==0)
return;
for(i=0;i<v[x].size();i++)
{
t = v[x][i];
if(lor[t]==lor[x])
ok = 0;
else
{
if(lor[x]==1 && lor[t]==0)
{
b.push_back(t), lor[t] = -1;
Sech(t);
}
else if(lor[x]==-1 && lor[t]==0)
{
a.push_back(t), lor[t] = 1;
Sech(t);
}
}
}
}