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 ...
随机推荐
- Python 学习笔记5
Life is like a box of chocolate. 今天继续学习Python数据结构. http://www.pythondoc.com/pythontutorial3/datastru ...
- vb6加载时提示出错,窗体log文件中错误信息为:控件 XX 的类 MSComctlLib.ListView 不是一个已加载的控件类。
解决办法:单击[工程] -- [部件] 添加此Microsoft Windows Common Controls-6.0 (SP6)部件,如果列表中没有,浏览到~\project\包\Support中 ...
- angularJS 系列(七)---指令
----------------------------------------------------------------------------------- 原文:https://www.s ...
- android常用工具类
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkIn ...
- HDU 5778 abs
题意转化一下就是寻找一个数P,要求P质因素分解完后,质因素没有重复,还要保证abs(P*P-x)最小. 暴力,在sqrt(x)附近向下向上分别枚举一下. #pragma comment(linker, ...
- vertor容器
头文件#include<vector> 1.创建vector对象 1.不指定容器大小 vector <int> v; 2.指定容器大小 vector <double&g ...
- 自定义AccessDeniedHandler
在Spring默认的AccessDeniedHandler中只有对页面请求的处理,而没有对Ajax的处理.而在项目开发是Ajax又是我们要常用的技术,所以我们可以通过自定义AccessDeniedHa ...
- php的memcache和memcached扩展区别【转载】
老生长谈的问题了.我这里就整理一下. memcache的文档在:http://pecl.php.net/package/memcache memcached的文档在:http://pecl.php.n ...
- Qt之打包发布(NSIS详解)
来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101fer8.html 发布方式 Qt发布的时候,通常使用两种方式: (1)静态编译 (2)动态编译 ...
- Unable to open connection to supplicant on "/data/misc/wifi/sockets/wlan0"
在调试android wifi UI 的时候,出现了 logcat: Unable to open connection to supplicant on "/data/misc/wifi ...