CodeForces - 687A(dfs+二分图的判定)

A. 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 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).

Input

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.

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 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.

Examples
Input
Copy
4 2
1 2
2 3
Output
Copy
1
2
2
1 3
Input
Copy
3 3
1 2
2 3
1 3
Output
Copy
-1
Note

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

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

解题思路:这是一道裸的二分图的判定,利用染色法dfs一下即可,最后统计下相同颜色的点数即可。

染色法判定二分图:把一个点相邻的点染成另一种颜色,能够使得最后的图所有的点与之相邻的都不同色。

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, m, cnt, flag;
int head[100010], color[100010];

struct edge
{
    int to;
    int next;
}e[100010*2];

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void dfs(int k)
{
    for(int i = head[k]; i != -1; i = e[i].next) {
        int en = e[i].to;
        if(!color[en]) {
            if(color[k] == 1) color[en] = 2;
            else color[en] = 1;
            dfs(en);
        }else {
            if(color[en] == color[k])  { //有相邻的点被染成同样的颜色
                flag = 1;
            }
        }
    }
}

int main()
{
    int x, y, a[100010], b[100010], suma, sumb;
    while(~scanf("%d%d", &n, &m)) {
        mem0(color);
        mem1(head);
        cnt = 0, flag = 0;
        suma = sumb = 0;
        for(int i = 0; i < m; i ++) {
            scanf("%d%d", &x, &y);
            add(x, y);
            add(y, x);
        }
        for(int i = 1; i <= n; i ++) {
            if(!color[i]) {
                dfs(i);
            }
        }
        if(!flag) {
            for(int i = 1; i <= n; i ++) { //统计
                if(color[i] == 1) a[suma ++] = i;
                else if(color[i] == 2) b[sumb ++] = i;
            }
            printf("%d\n", suma);
            for(int i = 0; i < suma-1; i ++) {
                printf("%d ", a[i]);
            }
            printf("%d\n",a[suma-1]);
            printf("%d\n", sumb);
            for(int i = 0; i < sumb-1; i ++) {
                printf("%d ", b[i]);
            }
            printf("%d\n", b[sumb-1]);
        } else printf("-1\n"); //不是二分图
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80489599