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 k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Examples

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

题意:N点M边,能不能分为两个没有公共点的点集,二者都覆盖所有的边。

思路:因为每条边只有两个端点,所以两端点必须分到不同的集合,所以就说二分图判定,先判定完,然后dfs染色即可。

这里用到了带权的并查集优化了一下。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int fa[maxn],vis[maxn],u[maxn],v[maxn],dis[maxn];
vector<int>G[maxn],a,b;
int find(int x){
if(x!=fa[x]){
int fx=find(fa[x]);
dis[x]^=dis[fa[x]];
//因为这里有压缩路径,所以是x到根距离与fax到根的距离差
fa[x]=fx;
}
return fa[x];
}
int Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy){
if(dis[x]==dis[y]) return false;
return true;
}
fa[fx]=fy; dis[fx]=dis[x]^dis[y]^;
return true;
}
void dfs(int x,int op){
vis[x]=;
if(op==) a.push_back(x); else b.push_back(x);
rep(i,,G[x].size()-) if(!vis[G[x][i]]) dfs(G[x][i],-op);
}
int main()
{
int N,M;
scanf("%d%d",&N,&M);
rep(i,,N) fa[i]=i;
rep(i,,M) {
scanf("%d%d",&u[i],&v[i]);
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
if(!Union(u[i],v[i])) return puts("-1"),;
}
rep(i,,N){ if(!vis[i]&&G[i].size()) dfs(i,);}
printf("%d\n",a.size());
rep(i,,a.size()-) printf("%d ",a[i]); puts("");
printf("%d\n",b.size());
rep(i,,b.size()-) printf("%d ",b[i]); puts("");
return ;
}

CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)的更多相关文章

  1. CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)

    Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...

  2. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  3. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并

    原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...

  4. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  5. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. D. The Door Problem 带权并查集

    http://codeforces.com/contest/776/problem/D 注意到每扇门都有两个东西和它连接着,那么,如果第i扇门的状态是1,也就是已经打开了,那么连接它的两个按钮的状态应 ...

  7. Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...

  8. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  9. UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

随机推荐

  1. spark学习(2)--hadoop安装、配置

    环境: 三台机器 ubuntu14.04 hadoop2.7.5 jdk-8u161-linux-x64.tar.gz (jdk1.8) 架构: machine101 :名称节点.数据节点.Secon ...

  2. 前端之 Ajax(补)

    概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...

  3. blast+学习之search tools

    search tools:blastn, blastp, blastx, tblastx, tblastn, psiblast, rpsblast, and rpstblastn 1.blastn: ...

  4. 根据Django后台的ajax大全

    一.什么是ajax 1.1 什么是JSON? AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语 ...

  5. QT应用程序设置图标

    一.纯Qt 1.下载图标:app.ico 2.新建记事本,输入:IDI_ICON1 ICON DISCARDABLE"app.ico":改变名字为jude.rc 3.将两个文件放在 ...

  6. 关于form表单中button按钮自动提交问题

    坑:点击确认按钮,form表单提交2次,发送后台2次请求 //错误代码: <Button id="btnSubmit" name="btnSubmit" ...

  7. [Android]开源中国源码分析之一---启动界面

    开源中国android端版本号:2.4 启动界面: 在AndroidManifest.xml中找到程序的入口, <activity android:name=".AppStart&qu ...

  8. Openfire部署和配置说明

    一.程序部署 1.1 程序和脚本 将文件拷贝到对应目录下,文件包括:Openfire.tar和setup.sh脚本.Openfire.tar为可执行文件库.配置等的压缩包,setup.sh为解压和部署 ...

  9. 多版本python的使用

    装任一版本的virtualenv都可以 在创建virtualenv时只需指定python的安装位置就可使用该版本的python virtualenv --python=D:\install\pytho ...

  10. 多校hdu5738 寻找

    这道题前面给了两个公式,其实仔细分析一下,就会发现其实是给了你一堆点的坐标,然后让你求这些点有多少种组合可以形成共线的情况当两个点在一个坐标上时这两个点可以看做是不同的两个点,也就是说如果两个点在一个 ...