CodeForces-687A(DFS,染色)

链接:

https://vjudge.net/problem/CodeForces-687A

题意:

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

思路:

第一眼以为最大匹配.其实就是dfs染色判断.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 1e5+10;

vector<int> G[MAXN];
int Color[MAXN];
int n, m;

bool Dfs(int u, int v, int c)
{
    Color[v] = c;
    for (int i = 0;i < G[v].size();i++)
    {
        int node = G[v][i];
        if (node == u)
            continue;
        if (Color[node] == Color[v])
            return false;
        if (Color[node] != -1)
            continue;
        if (!Dfs(v, node, c^1))
            return false;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(Color, -1, sizeof(Color));
    cin >> n >> m;
    int u, v;
    for (int i = 1;i <= m;i++)
    {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);

    }
    bool flag = true;
    for (int i = 1;i <= n;i++)
    {
        if (Color[i] == -1 && !Dfs(0, i, 0))
            flag = false;
    }
    if (!flag)
        cout << -1 << endl;
    else
    {
        int cnt = 0;
        for (int i = 1;i <= n;i++)
            if (Color[i] == 0)
                cnt++;
        cout << cnt << endl;
        for (int i = 1;i <= n;i++)
            if (Color[i] == 0)
                cout << i << ' ' ;
        cout << endl;
        cnt = 0;
        for (int i = 1;i <= n;i++)
            if (Color[i] == 1)
                cnt++;
        cout << cnt << endl;
        for (int i = 1;i <= n;i++)
            if (Color[i] == 1)
                cout << i << ' ' ;
        cout << endl;
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11363851.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值