zoukankan      html  css  js  c++  java
  • Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题

    C. NP-Hard Problem

    题目连接:

    http://www.codeforces.com/contest/688/problem/C

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

    Sample Input

    4 2
    1 2
    2 3

    Sample Output

    1
    2
    2
    1 3

    Hint

    题意

    给你一个无向图,问你能不能变成二分图。

    题解

    dfs一遍就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    vector<int> E[maxn];
    vector<int> ans[2];
    int n,m,vis[maxn],flag,type[maxn];
    void dfs(int x,int f,int ty){
        ans[ty].push_back(x);
        type[x]=ty;
        vis[x]=1;
        for(int i=0;i<E[x].size();i++){
            if(E[x][i]==f)continue;
            if(vis[E[x][i]]&&type[x]==type[E[x][i]])flag=1;
            if(vis[E[x][i]])continue;
            dfs(E[x][i],x,1-ty);
        }
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            E[a].push_back(b);
            E[b].push_back(a);
        }
        for(int i=1;i<=n;i++){
            if(flag)break;
            if(!vis[i])dfs(i,-1,0);
        }
        if(flag==1){printf("-1
    ");return 0;}
        cout<<ans[0].size()<<endl;
        for(int i=0;i<ans[0].size();i++)
            cout<<ans[0][i]<<" ";
        cout<<endl;
        cout<<ans[1].size()<<endl;
        for(int i=0;i<ans[1].size();i++)
            cout<<ans[1][i]<<" ";
        cout<<endl;
    }
  • 相关阅读:
    matlab后处理保存avi动画
    Python3在Windows安装配置及简单试用
    Matlab,C++存取二进制
    批量修改文件名
    这是我的第一篇博客园博客
    android的平台架构及特性
    Android开发学习
    跟着9张思维导图学习Javascript
    如何使用css和jquery控制文章标题字数?
    分离构造器(2-2)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5631129.html
Copyright © 2011-2022 走看看