Codeforces Round #360 (Div. 2) C DFS判断二分图

本文探讨了NP-Hard问题中的最小顶点覆盖问题,并通过一个具体的无向图实例介绍了如何使用深度优先搜索(DFS)算法来判断一个图是否为二分图。




链接:戳这里


C. NP-Hard Problem
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
4 2
1 2
2 3
output
1

2
1 3 
input
3 3
1 2
2 3
1 3
output
-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.


题意:

给定一个无向图(存在环),要求判断是否是二分图

二分图定义:设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图。


思路:

直接用DFS判断是否是二分图,染色就行了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,tot=0;
struct edge{
    int v,next;
}e[400100];
int head[100100],vis[100100];
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int flag=0;
void DFS(int u,int fa){
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        if(vis[v]!=0 && vis[v]==-vis[u]) continue;
        if(vis[v]!=0 && vis[v]==vis[u]){
            flag=1;
            continue;
        }
        if(vis[v]==0) {
            vis[v]=-vis[u];
            DFS(v,u);
        }
    }
}
int main(){
    mst(head,-1);
    mst(vis,0);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    for(int i=1;i<=n;i++){
        if(flag) break;
        if(!vis[i]){
            vis[i]=1;
            DFS(i,0);
        }
    }
    if(flag){
        cout<<-1<<endl;
        return 0;
    }
    int num1=0,num2=0;
    for(int i=1;i<=n;i++){
        if(vis[i]==1) ++num1;
        if(vis[i]==-1) ++num2;
    }
    printf("%d\n",num1);
    for(int i=1;i<=n;i++) {
        if(vis[i]==1) printf("%d ",i);
    }
    printf("\n%d\n",num2);
    for(int i=1;i<=n;i++){
        if(vis[i]==-1) printf("%d ",i);
    }
    printf("\n");
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值