zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    商户编号前三位代码对应的收单机构大全
    使用邮件模板(freemarker.jar)发送邮件
    Struts2自定义拦截器
    如何在jsp页面显示存储在数据库的图片
    正则表达式 大于0的数字(包含小数)
    Linux的关机命令
    maven的下载和安装
    web.py的安装
    安装第三方插件BeautifulSoup
    myeclipse离线安装pydev插件
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7017083.html
Copyright © 2011-2022 走看看