题目连接:http://codeforces.com/contest/688/problem/C

题意:给你一些边,问你能否构成一个二分图

题解:二分图:二分图又称作二部图,是图论中的一种特殊模型。 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图。

直接上一个DFS就搞定了

 #include<cstdio>
#include<set>
#include<vector>
#define pb push_back
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e5+;
int vis[N],n,m,x,y,ft,col[N],fg;
vector<int>Q[N];
set<int>a,b;
set<int>::iterator it;
void dfs(int pre,int now,int co){
if(vis[now]&&col[now]==co)fg=;
if(fg||vis[now])return;
vis[now]=,col[now]=!co;
if(col[now])a.insert(now);else b.insert(now);
for(int i=;i<Q[now].size();i++){
if(Q[now][i]!=pre){
dfs(now,Q[now][i],col[now]);
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
fg=;
F(i,,n)Q[i].clear();
a.clear(),b.clear();
F(i,,n)vis[i]=;
F(i,,m){
scanf("%d%d",&x,&y);
Q[x].pb(y),Q[y].pb(x);
}
F(i,,n)if(!vis[i])dfs(,i,);
if(fg)puts("-1");
else{
printf("%d\n",a.size()),ft=;
for(it=a.begin();it!=a.end();it++)
if(ft)printf("%d",*it),ft=;else printf(" %d",*it);
printf("\n%d\n",b.size()),ft=;
for(it=b.begin();it!=b.end();it++)
if(ft)printf("%d",*it),ft=;else printf(" %d",*it);
puts("");
}
}
return ;
}

Codeforces Round #361 (Div. 2) C.NP-Hard Problem的更多相关文章

  1. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

  2. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  3. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  4. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  5. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  6. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  7. Codeforces Round #361 (Div. 2) D

    D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...

  8. Codeforces Round #361 (Div. 2) C

    C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...

  9. Codeforces Round #361 (Div. 2) B

    B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...

随机推荐

  1. ubuntu14.04 安装 tensorflow

    如果内容侵权的话,联系我,我会立马删了的-因为参考的太多了,如果一一联系再等回复,战线太长了--蟹蟹给我贡献技术源泉的作者们- 最近准备从理论和实验两个方面学习深度学习,所以,前面装好了Theano环 ...

  2. ListView间隔设置颜色

    在Adapter的getView中 if (position % 2 == 0) holder.shoucangbt.setBackgroundColor( Color.rgb(246, 246, 2 ...

  3. ZOJ 2158 POJ 1789 Truck History

    最小生成树,主要是题目比较难懂. #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  4. C# 网上收集的一些所谓的开源项目

    C#开源 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7- ...

  5. RF常用技巧随笔

    合并xml输出文件: pybot --outputdir d:\test --log log1.html --report report1.html --output output1.xml -T d ...

  6. linux脚本Shell之awk详解

    一.基本介绍1.awk: awk是一个强大的文本分析工具,在对文本文件的处理以及生成报表,awk是无可替代的.awk认为文本文件都是结构化的,它将每一个输入行定义为一个记录,行中的每个字符串定义为一个 ...

  7. thinkphp的model模型的设计经验总结

    关于模型:跟上篇文章thinkphp的目录结构设计经验总结写控制器一个道理:为了尽量避免改动到框架: 首先我们是要有一个BaseModel.class.php作为我们的基础model: 我会在Base ...

  8. 第三十二节,datetime时间模块

    首先要引入import datetime时间模块 datetime.date.today()模块函数 功能:输出系统年月日输出格式 2016-01-26[无参] 使用方法:datetime.date. ...

  9. 【Python爬虫实战--1】深入理解urllib;urllib2;requests

    摘自:http://1oscar.github.io/blog/2015/07/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3urllib;urllib2;reques ...

  10. flash检测网络是否通畅

    air: 要在 Adobe ® Flash ® Professional(CS4 或更高版本)中使用 air.net 包: 选择“文件”>“发布设置”命令. 在 Flash 面板中,单击 Act ...