• codeforces 360 C


                         C - NP-Hard Problem

    Description

    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 kintegers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

    Sample Input

    Input
    4 2
    1 2
    2 3
    Output
    1
    2
    2
    1 3
    Input
    3 3
    1 2
    2 3
    1 3
    Output
    -1
    题意:给出点的个数和连接这些点的边(给你个无向图),判断是否能构成二分图,如果能就输出左右集合,不能输出-1.
    分析:
    二分图的定义:有两个顶点集且每条边的两个点分别位于两个集合,每个集合中不能含有一条完整的边。
    二分图的判断方法:先对任意一条没有染色的点进行染色,再判断与其相邻的点是否染色,如果没有染色就对其染上与其相邻的 点不同的颜色,如果有染色且颜色与其相邻点的颜色相同就不是二分图,若颜色不同就继续判断(用bfs)。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn = 100000 + 10;
    vector<int>g[maxn],v[2];
    bool ok = true;
    int color[maxn];
    void dfs(int k,int c){
        if(!ok)return;
        if (color[k] != -1){
            if (color[k] != c)  ok=false;
            return;
        }
        int len = g[k].size();
        if(len!=0)
        {
            color[k] = c;
        v[c].push_back(k);
        }
        for (int i = 0; i < len; ++i)  dfs(g[k][i],c^1);
    }
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i = 0; i < m; ++i){
            int u,w;
            scanf("%d%d",&u,&w);
            g[u].push_back(w);
            g[w].push_back(u);
        }
        memset(color,-1,sizeof color);
        for (int i = 1; i <= n; ++i){
            if (color[i] == -1)dfs(i,0);
        }
        if (!ok)printf("-1
    ");
        else {
            for (int i = 0; i < 2; ++i)
            {
                 int len = v[i].size();
             printf("%d
    ",len);
        for (int j = 0; j < len; ++j){
            if (j)printf(" ");
            printf("%d",v[i][j]);
        }
        printf("
    ");
            }
        }
        return 0;
    }
     
  • 相关阅读:
    3号团队-团队任务4:每日例会(2018-11-28)
    10号团队-团队任务5:项目总结
    课后作业4-个人总结
    团队任务4-每日例会(2018-12-6)
    团队任务4-每日例会(2018-12-5)
    团队任务4-每日例会(2018-12-4)
    团队任务4-每日例会(2018-12-3)
    团队任务4:每日例会(2018-11-30)
    团队任务4:每日例会(2018-11-28)
    团队任务4:每日例会(2018-11-29)
  • 原文地址:https://www.cnblogs.com/fenhong/p/5671867.html
Copyright © 2020-2023  润新知