CodeForces - 688C: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 k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
4 2
1 2
2 3
1
2
2
1 3
3 3
1 2
2 3
1 3
-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 (二分图&带权并查集)的更多相关文章
- 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 ...
- 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 ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- D. The Door Problem 带权并查集
http://codeforces.com/contest/776/problem/D 注意到每扇门都有两个东西和它连接着,那么,如果第i扇门的状态是1,也就是已经打开了,那么连接它的两个按钮的状态应 ...
- Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
随机推荐
- Spring:笔记整理(1)——HelloWorld
Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...
- asp.net IRequiresSessionState
在一般处理程序中,使用context.Session对象,必须先继承IRequiresSessionState接口. System.Web.SessionState.IRequiresSessionS ...
- 使用git从本地上传至git码云远程仓库
从 http://git-scm.com/download 下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git config --global user.name & ...
- Qt核心机制和原理
转:http://blog.csdn.net/light_in_dark/article/details/64125085 ★了解Qt和C++的关系 ★掌握Qt的信号/槽机制的原理和使用方法 ★了解Q ...
- linux du与ls查看文件大小时的区别
du和ls查看文件大小的区别 du == disk usage (磁盘使用量,占用的磁盘空间)du 的基本使用du -s #s参数是可以统计占硬盘空间大小的如 du -skh web-k或-- ...
- 在unity 中,使用http请求,下载文件到可读可写路径
在这里我用了一个线程池,线程池参数接收一个带有object参数的,无返回值的委托 ,下载用到的核心代码,网上拷贝的,他的核心就是发起一个web请求,然后得到请求的响应,读取响应的流 剩下的都是常见的I ...
- 修改redhat默认显示语言为中文
[delmore@localhost Desktop]$ su //切换到最高权限 Password: ...
- 解决Linux系统在设置alias命令重启后失效的问题
在使用linux系统的过程中,大多数情况下都是在字符界面下进行的.有些比较长的命令我们不希望每次都重复输入,这样不仅浪费时间而且还容易出错:我们会使用alias命令来解决 比如: alias ll=' ...
- DESeq2 install --- 如何安装R包("RcppArmadillo")?
安装R包("RcppArmadillo")失败,导致依赖该包的DESeq2 无法使用: 首先对gcc,g++升级至4.7, 但依然报错,还是安装不了RcppArmadillo: 报 ...
- java深入探究13-js,ajax
链接:http://pan.baidu.com/s/1c2D0cAs 密码:uwm6 1.js 1)三种基本类型: var num=100; var str="哈哈"; var f ...