题目连接: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. 1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。

    package zuoye; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  2. 移动端touch触屏滑动事件、滑动触屏事件监听!

    一.触摸事件 ontouchstart.ontouchmove.ontouchend.ontouchcancel 目前移动端浏览器均支持这4个触摸事件,包括IE.由于触屏也支持MouseEvent,因 ...

  3. oc 是否允许远程通知

    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSett ...

  4. CodeForces 754C Vladik and chat (DP+暴力)

    题意:给定n个人的m个对话,问能不能找一个方式使得满足,上下楼层人名不同,并且自己不提及自己. 析:首先预处理每一层能有多少个user可选,dp[i][j] 表示第 i 层是不是可以选第 j 个use ...

  5. vbs鼠标方法——模拟鼠标按键

    '*********************************************************************** ' 代码开始 '******************* ...

  6. 详细理解servlet实现的几种方式和生命周期

    现在很多的开发都是用的框架,然后很多同学学习的时候又是直接接触的框架,对于底层的一些开发,完全没有任何的了解.虽然对于业务上面来说,没有什么问题.但是很多时候当你被面试问到,或者是想要了解框架底层原理 ...

  7. JDK1.5与1.6在Override上的区别

    @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Ove ...

  8. hdu_3966_Aragorn's Story(树链剖分裸题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...

  9. OPENWRT make menuconfig错误之一

    1.make menuconfig rm: cannot remove `tmp/.host.mk': Permission denied 退到trunk上级目录sudo chown -R 777 t ...

  10. 开源日志系统比较:scribe、chukwa、kafka、flume

    1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...