zoukankan      html  css  js  c++  java
  • 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;
    }
     
  • 相关阅读:
    tail命令和head命令查询文件指定行数
    php unserialize(): Error at offset 470 of 660 bytes
    屏蔽搜索引擎收录robots.txt文件下载
    php-apc为magento加速
    magento导入csv文件到数据库中乱码
    magento 上传csv表格中实例化对象例子
    magento上新产品,前台不显示不显示属性,后台却有属性问题
    php裁剪图片
    概念:静态static相关知识
    概念 : 重定向概念
  • 原文地址:https://www.cnblogs.com/fenhong/p/5671867.html
Copyright © 2011-2022 走看看