Codeforces C-np-hard problem

Source: Internet
Author: User

Original question:

Description

Recently, Pari and Arya did some-about np-hard problems and they found the minimum vertex coverproblem very I Nteresting.

Suppose the graph G is given. Subset A of its vertices are called a vertex cover of this graph, if for each edge UV There I  s at least one endpoint of the it in this set, i.e. or (or both).

Pari and Arya has won a great undirected graph as an award in a team contest. Now they has to split it in both parts, but both of them want their parts of the graph to be a vertex cover.

They has agreed to give you their graph and your need to find the disjoint subsets of its vertices A and b, such that both A and B is vertex cover or claim it ' s impossible. Each vertex should is 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 the integers n and m (2≤ n ≤100 000, 1≤ m ≤100-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, vin), 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 is disjoint sets of vertices, such that both sets is vertex cover, print their descriptions. Each description must contain and lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the Seco nd line contains k integers-the indices of vertices. Note that because of m ≥ 1, vertex cover cannot is empty.

Sample Input

Input
4 2
1 2
2 3
Output
1

2
Input
3 3
1 2
2 3
1 3
Output
-1

hint : First, this is a diagram, the diagram is very easy to think of deep search and traverse and extensive search!! ~ ~ Yes, this problem can be traversed with deep search.
Each point here can be connected to multiple points, which is equivalent to multiple directions in a deep search. Search from the first point to traverse all "connected points".
At the time of storage, in order to achieve the above data structure, the vector <int> vertex[] subscript corresponding vertex number is set based on vertex . Stores the points to which they are attached.
2 colors for different people, not 1 that is 2 (with 1^3 2^3 can be converted to each other) or not 0 or 1 (0^1 1^1 can achieve mutual conversion)
PS: This is done by starting at the apex, not knowing if you can do it. (Traverse all edges)

Code:
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;#defineABS (x) ((x) >0? ( x):-(x))#definell Long LongConst intMAXN = 1e5 +Ten;BOOLOK =true;intN, M, U, V, color[maxn];vector<int>v1, v2, G[MAXN];//Color[u] values of 1 and 2 respectively represent two different colors for u coatingvoidDfsintUintc) { for(inti =0; I < g[u].size (); i++) {        intv =G[u][i]; //If the next point is not painted, apply a different color to it .        if(Color[v] = =0) {Color[v]= c ^3; DFS (V, c^3); }        //If the next point is the same color as the current point, the failure returns        if(Color[v] = =c) {OK=false; return; }    }}intMain () {scanf ("%d%d", &n, &m);  for(inti =0; I < m; i++) {scanf ("%d%d", &u, &v);        G[u].push_back (v);    G[v].push_back (U); }    //coloring for each connected component     for(inti =1; I <= N; i++) {        if(Color[i] = =0) {Color[i]=1; DFS (i,1); }    }    //output failed or A and B    if(OK = =false) {puts ("-1"); }    Else {         for(inti =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(inti =0; I < v1.size (); i++) {printf ("%d", V1[i]); } printf ("\n%d\n", V2.size ());  for(inti =0; I < v2.size (); i++) {printf ("%d", V2[i]); } puts (""); }    return 0;}



Codeforces C-np-hard problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.