Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接: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的更多相关文章
- 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 ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- 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 ...
- 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 ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- 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 ...
- Codeforces Round #361 (Div. 2) D
D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...
- Codeforces Round #361 (Div. 2) C
C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...
- Codeforces Round #361 (Div. 2) B
B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...
随机推荐
- 通过Maven配置测试环境和开发环境连接不同的数据库
通过Maven配置测试环境和开发环境连接不同的数据库 作者及来源: 通灵宝玉 - 博客园 收藏到→_→: 此文来自: 马开东博客 网址:http://www.makaidong.com ...
- sql or 与and同时有时要注意
如果是一天sql语句中有or和and同时在 正确:where xxx=xx and (xxx=xx or xxx=xx) 错误:where xxx=xx and xxx=xx or xxx=xx (这 ...
- linux常用服务软件搭建及使用技巧
一.Webmin安装: Webmin 是一个基于浏览器的管理工具,可以应用于Linux 和其他一些平台,提供了可以完成很多管理和操作任务的图形化界面 •安装完成后,root 用户会被自动创建,密码为系 ...
- eclipse新建workspace使用之前workspace的个性配置
为使新建的workspace(称作A)的配置,比如主题等等,和之前的workspace(称作B)的配置一样: . 关闭eclipse . 将A中.metadata/.plugins目录下所有文件.文件 ...
- 《初识PE》导入表
最近听别人讲的我晕晕乎乎的,于是上网上百度下,感觉这篇还不错. 链接:http://www.blogfshare.com/pe-export.html 一.导入表简介 在编程中常常用到"导 ...
- how to add a shared lib in C?
http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html Basically, 2 steps: 1) make the ...
- 把aspx页面输出成xml的方法注意事项
先贴代码 Response.Charset = "gb2312"; Response.ContentType = "text/xml"; Response.Co ...
- CentOS ulimit
Linux 文件系统设置 文件句柄数 ulimit -n 查看 echo "* - nofile 65536" >> /etc/security/limits.conf ...
- Mongoose如何实现统计查询、关联查询
[问题]Mongoose如何实现统计查询.关联查询 发布于 4 年前 作者 a272121742 13025 次浏览 最近业务上提出一个需求,要求能做统计,我们设计的文档集,统计可能跨越的文档会 ...
- c_select 调用参数说明
c_select 调用 1. select系统调用select系统调用是用来让我们的程序监视多个文件描述符的状态变化的.程序会停在select这里等待,直到被监视的文件描述符有某一个或多个发生了状态改 ...