CodeForces 688C-NP-Hard Problem(dfs)

来源:互联网 发布:惠普hp m1136的端口 编辑:程序博客网 时间:2024/05/16 04:52
C. NP-Hard Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Pari and Arya did some research about NP-Hard problems and they found theminimum 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 edgeuv 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 andB, such that both A andB 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).

Input

The first line of the input contains two integers n andm (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 integersui andvi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui andvi. It's guaranteed the graph won't contain any self-loops or multiple edges.

Output

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 integerk denoting the number of vertices in that vertex cover, and the second line containsk integers — the indices of vertices. Note that because ofm ≥ 1, vertex cover cannot be empty.

Examples
Input
4 21 22 3
Output
12 21 3 
Input
3 31 22 31 3
Output
-1
Note

In the first sample, you can give the vertex number 2 to Arya and vertices numbered1 and 3 to Pari and keep vertex number4 for yourself (or give it someone, if you wish).

In the second sample, there is no way to satisfy both Pari and Arya.


题意:给出一个无向图,*有可能分成好几块,求是否能将这个无向图分成两部分,使得两部分的点不重复并且每部分都能够覆盖所有边(含有边的其中一端)。

思路:用dfs分配,假设父结点分配给第一部分,那么它的儿子结点只能分配给另一部分。如果有一个结点被两部分都需要,就会引起矛盾,输出-1。

#include <bits/stdc++.h>using namespace std;#define maxn 100010struct node{    int to, next;}edge[maxn*2];int head[maxn], vet[maxn], tot, n, m;bool vis[maxn];void add(int u, int v){    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}bool flag;void dfs(int u){    if(!flag) return;    vis[u] = 1;    //printf("fa:%d\n", u);    for(int i = head[u];i != -1;i = edge[i].next){        int son = edge[i].to;        if(vis[son]){            if(vet[u] == vet[son]){                flag = false;                return;            }            continue;        }        vet[son] = (vet[u]==2?1:2);        dfs(son);    }}int main(){    int i, u, v;    scanf("%d %d", &n, &m);    tot = 1;    flag = 1;    memset(head, -1, sizeof head);    for(i = 0;i < m;i++){        scanf("%d %d", &u, &v);        add(u, v);        add(v, u);    }    for(i = 1;i <= n;i++)        if(!vis[i]){            vet[i] = 1;            dfs(i);        }    if(!flag) printf("-1\n");    else{        int len1, len2;        len1 = len2 = 0;        for(i = 1;i <= n;i++){            if(vet[i] == 1) len1++;            else if(vet[i] == 2) len2++;        }        printf("%d\n", len1);        for(i = 1;i <= n;i++){            if(vet[i] == 1){                len1--;                printf("%d%c", i, len1==0?'\n':' ');            }        }        printf("%d\n", len2);        for(i = 1;i <= n;i++){            if(vet[i] == 2){                len2--;                printf("%d%c", i, len2==0?'\n':' ');            }        }    }}

0 0
原创粉丝点击