codeforces 360 C
C - NP-Hard Problem
Description
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.
Sample Input
4 2
1 2
2 3
1
2
2
1 3
3 3
1 2
2 3
1 3
-1
题意:给出点的个数和连接这些点的边(给你个无向图),判断是否能构成二分图,如果能就输出左右集合,不能输出-1.
分析:
二分图的定义:有两个顶点集且每条边的两个点分别位于两个集合,每个集合中不能含有一条完整的边。
二分图的判断方法:先对任意一条没有染色的点进行染色,再判断与其相邻的点是否染色,如果没有染色就对其染上与其相邻的 点不同的颜色,如果有染色且颜色与其相邻点的颜色相同就不是二分图,若颜色不同就继续判断(用bfs)。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = + ;
vector<int>g[maxn],v[];
bool ok = true;
int color[maxn];
void dfs(int k,int c){
if(!ok)return;
if (color[k] != -){
if (color[k] != c) ok=false;
return;
}
int len = g[k].size();
if(len!=)
{
color[k] = c;
v[c].push_back(k);
}
for (int i = ; i < len; ++i) dfs(g[k][i],c^);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for (int i = ; i < m; ++i){
int u,w;
scanf("%d%d",&u,&w);
g[u].push_back(w);
g[w].push_back(u);
}
memset(color,-,sizeof color);
for (int i = ; i <= n; ++i){
if (color[i] == -)dfs(i,);
}
if (!ok)printf("-1\n");
else {
for (int i = ; i < ; ++i)
{
int len = v[i].size();
printf("%d\n",len);
for (int j = ; j < len; ++j){
if (j)printf(" ");
printf("%d",v[i][j]);
}
printf("\n");
}
}
return ;
}
codeforces 360 C的更多相关文章
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- codeforces 360 E - The Values You Can Make
E - The Values You Can Make Description Pari wants to buy an expensive chocolate from Arya. She has ...
- codeforces 360 D - Remainders Game
D - Remainders Game Description Today Pari and Arya are playing a game called Remainders. Pari choos ...
- 套题 codeforces 360
A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...
- codeforces 360 C - NP-Hard Problem
原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the ...
- codeforces 360 B
B - Levko and Array 题目大意:给你你个长度为n的数列a,你最多改变k个值,max{ abs ( a[ i + 1] - a[ i ] ) } 的最小值为多少. 思路:这个题很难想到 ...
- Codeforces Round #360 div2
Problem_A(CodeForces 688A): 题意: 有d天, n个人.如果这n个人同时出现, 那么你就赢不了他们所有的人, 除此之外, 你可以赢他们所有到场的人. 到场人数为0也算赢. 现 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 数学
D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...
随机推荐
- RSA非对称性前端加密后端解密
前端加密代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- Sublime Text 2下搭建Python环境常见错误
Sublime Text 2下搭建Python环境时,最容易出的错误就是Python环境配置错误,导致build(Ctrl+B)后没有任何反应. 关于Python编程环境的配置,网上很容易搜索到.先默 ...
- STM32F412应用开发笔记之五:结合W5500实现以太网通讯
因实际使用需求我们测试一下网络通讯,在NUCLEO-F412ZG测试板上没有以太网部分,我们选择外接一个W5500的实验板.W5500支持SPI接口通讯,DC3.3V供源.而NUCLEO-F412ZG ...
- A页面调到B页面,B页面关闭时A页面刷新
// A.html <html> <head> <script type="text/javascript"> alert("refr ...
- RBAC基于角色的访问控制
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成"用 ...
- java写RelativeLayout 的属性
有时项目需要动态的调整一下布局,需要改变一些view的位置属性等等. 直接下代码 RelativeLayout.LayoutParams params=new RelativeLayout.Layou ...
- JavaScript跨域提交数据
1.通过jsonp跨域 场景:假设前台有JS方法"crossJS", 1.1发送请求http://www.xxx.com/?callback=crossJS.(创建一个scr ...
- JS string 截取
subStubstring(a,b); a:开始索引 b:结束索引 subStr(c,d) c:开始索引 d:截取数量.
- xplan.sql(本脚本获取执行计划显示执行顺序)
-- ---------------------------------------------------------------------------------------------- -- ...
- 本人为巨杉数据库(开源NoSQL)写的C#驱动,支持Linq,全部开源,已提交github
一.关于NoSQL的项目需求 这些年在做AgileEAS.NET SOA 中间件平台的推广.技术咨询服务过程之中,特别是针对我们最熟悉的医疗行业应用之中,针对大数据分析,大并发性能的需求,我们也在慢慢 ...