• Codeforce 687A. NP-Hard Problem


    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.

    带种类的并查集,并完后根据rank值分类 
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include <string>
    #include <sstream>
    #include <map>
    #include <cmath>
    #include <algorithm>
    #include <iomanip>
    #include <stack>
    #include <queue>
    #include <set>
    using namespace std;
    typedef long long LL;
    #define MOD 1000000007
    int n,m;
    int flag = 0;
    int ranks[100005],father[100005];
    int visited[100005] = {0};
    int getF(int x){
        if (x == father[x]) return father[x];
        int fx = getF(father[x]);
        ranks[x] = (ranks[father[x]] + ranks[x]) & 1;
        father[x] = fx;
        return father[x];
    }
    void Union(int a,int b){
        int fa = getF(a),fb = getF(b);
        if (fa == fb){
            if (ranks[a] == ranks[b]){
                flag = 1;
            }
            return;
        }
        father[fa] = fb;
        ranks[fa] = (ranks[a] + ranks[b] + 1) & 1;
    }
    int res[2][100005];
    int main()
    {
        // freopen("test.in","r",stdin);
        ///hhhhh
        cin >> n >> m;
        for (int i=0;i<=n;i++){
            father[i] = i; ranks[i] = 0;
        }
        memset(res,0,sizeof(res));
        for (int i=1;i<=m;i++){
            int u,v;
            cin >> u >> v;
            visited[u] = 1; visited[v] = 1;
            Union(u,v);
        }
        if (flag) cout << "-1";
        else {
            for (int i=1;i<=n;i++){
                if (!visited[i]) continue;
                getF(i);
                res[ranks[i]][0] ++;
                res[ranks[i]][res[ranks[i]][0]] = i;
            }
            cout << res[0][0] << endl;
            for (int i=1;i<=res[0][0];i++){
                cout << res[0][i] << " ";
            }
            cout << endl << res[1][0] << endl;
            for (int i=1;i<=res[1][0];i++){
                cout << res[1][i] << " ";
            }
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    Unity3D游戏制作(四)——Asset Server搭建
    查询开户银行的现代化支付行号
    专业版Unity技巧分享:使用定制资源配置文件
    如何建立一个完整的游戏AI
    实现简易而强大的游戏AI——FSM,有限状态机
    iOS 开发 初级:应用内购买 In-App Purchase
    linux每日命令(14):less命令
    flask上传excel文件,无须存储,直接读取内容
    linux每日命令(13):more命令
    linux每日命令(12):nl命令
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7017083.html
Copyright © 2020-2023  润新知