题意:
  给你一个无向图,判断是否能够构成一个二分图,如果能的话,输出二分图左边的集合和右边的集合

分析:
    先给每一个顶点的color初始化-1,表示没有被染色,用vector数组v[a],表示元素a所相连的全部元素,然后枚举每一个顶点,发现没有被染色,就对它进行染色,先把顶点染成0,然后

再将染成颜色为0的vector加入当前元素,在枚举与这个元素相连的元素,假设颜色是c的话,相连的就染成c^1,它会把0变成1,1变成0;如果二分图左边是0,右边是1,则表示它已被染色直

接return,发现染色的颜色与该染的颜色不符合,就不能构成二分图了,ok=false.

代码如下:

 #include <iostream>
#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
ok = false;
return;
}
color[k] = c;
v[c].push_back(k); //将已被染色的元素加入数组v中
int len = g[k].size();
for (int i = ; i < len; i++) //枚举与这个元素相连的元素
dfs(g[k][i],c^); // 0 -> 1,, 1 -> 0;
} void print(vector<int> & t)
{
int len = t.size();
printf("%d\n",len);
for (int i = ; i < len; ++i)
{
if(i)
printf(" ");
printf("%d", t[i]);
}
printf("\n");
} int main()
{
int n, m;
while(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)); //初始化-1表示没被染色
for(int i = ; i <= n; i++)
{
if (color[i] == -) //对所有未染色的顶点染色
dfs(i,);
}
if(!ok)
printf("-1\n");
else
for(int i = ; i < ; i++)
print(v[i]);
}
return ;
}

CodeForces 688C-NP-Hard Problem的更多相关文章

  1. CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...

  2. codeforces 688C C. NP-Hard Problem(bfs判断奇数长度环)

    题目链接: C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  4. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  5. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  6. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  7. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  8. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...

  9. [Codeforces 986E] Prince's Problem

    [题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...

  10. 【codeforces 527D】Clique Problem

    [题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...

随机推荐

  1. PHP中fopen,file_get_contents,curl函数的区别

    PHP中fopen,file_get_contents,curl函数的区别 1.fopen/file_get_contents每次请求都做DNS查询,并不对DNS的信息进行缓存,而curl会对DNS的 ...

  2. 在Repeater中嵌套使用Repeater

    在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品.最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加 ...

  3. How to run a (Tomcat)Java application server on a Azure virtual machine

    http://www.windowsazure.com/en-us/documentation/articles/virtual-machines-java-run-tomcat-applicatio ...

  4. Android JPush(极光推送)的使用教程

    首先进入官网https://www.jpush.cn/,先注册一个账号. 注册号以后,创建应用 1.点击右上角进入个人中心 2.点击创建应用 3.在创建应用界面输入自己项目的应用名和应用的包名,输入后 ...

  5. 用xib自定义UITableViewCell

    1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 #import "Mo ...

  6. size()

    jQuery 对象中元素的个数. 当前匹配的元素个数.与length将返回相同的值. 示例 描述: 计算文档中所有图片数量 HTML 代码: <img src="test1.jpg&q ...

  7. Python之控制台输入密码的方法

    一.raw_input()或input(): for python 2.x [root@master test]# /usr/local/python2.7/bin/python test.py Pl ...

  8. canvas 绘图

    <canvas>元素是HTML5中的绘图元素,通过定义一个画布区域,然后使用javascript动态地在这个区域里面绘制图形,对于2D和3D图形都可以绘制,我们将其分成2D上下文和WebG ...

  9. Cheatsheet: 2015 05.01 ~ 05.31

    .NET .NET on Mac for the OS X n00b without Mono via Visual Studio Code Microsoft frameworks deprecat ...

  10. 用excel绘制基因芯片热力图

    1.  首先我们通过一些方法得到了如下的数据,基于篇幅以及为了教学隐去了其他一些信息. 2. 选中表达数据,执行 开始—条件格式—色阶 选择一个合适的色阶: 3. 选择好颜色之后得到了如下结果: