codeforces 360 C - NP-Hard Problem

原题:

Description

Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex coverproblem 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 k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Sample Input

Input

4 21 22 3

Output

12 21 3 

Input

3 31 22 31 3

Output

-1

提示:首先,这是个图,图就很容易想到深搜遍历和广搜遍历啊!!~~   没错,这题可以用深搜遍历。这里的每个点都可以连接多个点,相当于深搜里的多个方向。从第一个点开始搜,遍历所有“相连的点”。储存时,为了达成上述的数据结构,以顶点为基础,建立vector <int> Vertex[ ]    下标对应顶点编号。存储与之相连的点。2种颜色表示不同的人,非1即2 (用 1^3 2^3 可以实现相互转化)或者非0即1(0^1 1^1 可以实现相互转化)ps:这是通过着手顶点来做的,还不知道可不可以着手 边 去做。(遍历所有的边)

代码:
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define ABS(x) ((x)>0?(x):-(x))
#define ll long long

const int maxn = 1e5 + 10;
bool ok = true;
int n, m, u, v, color[maxn];
vector <int> v1, v2, G[maxn];

// color[u]的值为1和2分别代表为u涂上两种不同的颜色
void dfs(int u, int c) {
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        // 如果下一个点没涂过色的话,为其涂上不同的颜色
        if(color[v] == 0) {
            color[v] = c ^ 3;
            dfs(v, c ^ 3);
        }
        // 如果下一个点跟当前点颜色相同的话,失败返回
        if(color[v] == c) {
            ok = false;
            return;
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    // 为每个连通分量涂色
    for(int i = 1; i <= n; i++) {
        if(color[i] == 0) {
            color[i] = 1;
            dfs(i, 1);
        }
    }
    // 输出失败或A和B
    if(ok == false) {
        puts("-1");
    }
    else {
        for(int i = 1; i <= n; i++) {
            if(color[i] == 1) {
                v1.push_back(i);
            }
            if(color[i] == 2) {
                v2.push_back(i);
            }
        }
        printf("%d\n", v1.size());
        for(int i = 0; i < v1.size(); i++) {
            printf("%d ", v1[i]);
        }
        printf("\n%d\n", v2.size());
        for(int i = 0; i < v2.size(); i++) {
            printf("%d ", v2[i]);
        }
        puts("");
    }
    return 0;
}
				
时间: 2024-10-05 22:36:07

codeforces 360 C - NP-Hard Problem的相关文章

Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】

简易字符串匹配,题意不难 1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int main(){ 9 int i, j, k, t, n; 10 int num, flag, ans; 11 char a[300]; 12 sc

codeforces#253 D - Andrey and Problem里的数学知识

这道题是这样的,给主人公一堆事件的成功概率,他只想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才能使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,可是觉得太麻烦. 于是想是不是有什么规律,于是推导了一下,推了一个出来,写成代码提交之后发现是错的. 最后就没办法了,剩下的时间不够写dfs,于是就放弃了. 今天看thnkndblv的代码,代码很短,于是就想肯定是有什么数学规律,于是看了一下, 果然如此. 是这样的,还是枚举,当然是有技巧的,看我娓娓道来. 枚举的话

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Codeforces 776D:The Door Problem(DFS染色)

http://codeforces.com/problemset/problem/776/D 题意:有n个门,m个开关,每个门有一个当前的状态(0表示关闭,1表示打开),每个开关控制k个门,但是每个门确切的受两个开关控制,如果一个开关打开,那么原来关闭的门会打开,打开的门关闭,问是否存在一个情况使得所有的门打开. 思路:类似于01染色,把开关当成点,门当前的状态当成边权建图.初始先假设一个门的状态(初始假设为0和假设为1都是一样的,举几个例子就发现了),然后因为每个门受两个开关控制,所以可以推出

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开始到倒数第二项,当前的a[i]=(a[i-1],a[i],a[i+1])三项排序后的中间项,比如连续3项为 1 0 1,那么中间的就变为1,然后题目让你输出达到稳定状态时所需的最小步数,不能的话输出-1. 无论给你啥数列,都能达到稳态.所以不可能输出-1: 还有一开始就稳定不变,或经过几次变换而稳定

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules