• 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;
    }
    
  • 相关阅读:
    关于android中透明、半透明、百分比转换
    详解 RestTemplate 操作
    springboot No Identifier specified for entity的解决办法
    Java-Spring-获取Request,Response对象
    java 如何判断操作系统是Linux还是Windows
    Spring中使用Ehcache的方法和注意事项
    Protostuff序列化
    如何使用apache自带的ab压力测试工具
    java 调用 wsdl形式的webservice 示例
    web service 异常
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11363851.html
Copyright © 2020-2023  润新知