Codeforce 687A. NP-Hard Problem

Posted TOTOTOTOTZZZZZ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Codeforce 687A. NP-Hard Problem的主要内容,如果未能解决你的问题,请参考以下文章

P问题,NP问题,NPC问题,NP-hard问题

P问题NP问题NPC问题和NP-Hard问题,相关概念与题目

P问题NP问题NPC问题和NP-Hard问题,相关概念与题目

CodeForce-807C Success Rate(二分数学)

假的数论gcd,真的记忆化搜索(Codeforce 1070- A. Find a Number)

codeforce 906 SOL (A)