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 ...
随机推荐
- 懒加载插件- jquery.lazyload.js
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- EXCEL 对比数据是否重复
1.同一列 后一行对比前面所有行 查找是否重复 =IF(COUNTIF(B$2:B2,B2)>1,"重复","") 2.两行两列(多行多列) 两行两列 = ...
- Linux 安装Mono环境 运行ASP.NET(二)
一.安装libgdiplus 前面我们已经安装了apr.apr_util.pcre和httpd apache .现在我们来安装libgdiplus Libgdiplus是一个Mono库,用于对 ...
- Struts中文件上传的一些规则...
1.action中定义规范 如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为 private File xxx; private String ...
- iOS让键盘消失,取消第一响应,取消一级响应
在开发中经常会遇到输入文本内容的时候,输入完毕的时候怎么让键盘消失的问题,有的是更改键盘的按键的方法,有的是点击屏幕的其他地方让键盘消失,个人更倾向于第二种,点击屏幕的其他地方让键盘消失,要实现这种方 ...
- python之路:Day01 --- Python基础1
本节内容 1.Python介绍 2.发展史 3.变量 4.用户输入 5.表达式 if...else语句 6.表达式 for 循环 7.表达式 while 循环 8.模块初识 9.数据类型初识 10.数 ...
- php Your system does not support any of these drivers: gmagick,imagick,gd2
缺少这些库时,安装 : apt-get install php5-gd 就可以.
- 学习EXT.JS5时的重点载图
组件实例化的五种方式,最后一种不建议了 MVVM的图示,及controller的生存周期和MVC的不一样. VIEWCONTROLLER如何得到VIEW的实例呢,注意LOOKUPREFERENCE的使 ...
- Selenium 简单的例子
Selenium是一个web自动化验收测试框架. Selenium Client Driver - Selenium 2.0 Document http://seleniumhq.github.i ...
- JS--该死的&&和||
近段时间搞前端js,零零星星的看了一些框架源码,发现大量存在&&和||,其语法看的我是头冒青烟,也不知道怎么回事,度娘搜吧,现在写下来,以防忘掉. 先来约束一下用词:有一表达式A : ...