CodeForces 688C-NP-Hard Problem
题意:
给你一个无向图,判断是否能够构成一个二分图,如果能的话,输出二分图左边的集合和右边的集合
分析:
先给每一个顶点的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的更多相关文章
- CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...
- 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 ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
- codeforces 803G Periodic RMQ Problem
codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...
- [Codeforces 986E] Prince's Problem
[题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...
- 【codeforces 527D】Clique Problem
[题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...
随机推荐
- spring aop 中获取 request
使用aop时需要request 和response 使用方法调用时 HttpServletRequest request = ((ServletRequestAttributes)RequestCon ...
- mongodb备份与恢复
一.备份:mongodump -d mailaccess -c Mail -q ‘{user:”zhaoxy1@szdep.com”}’ -o /data/dump备份mailaccess datab ...
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
为了实现这个功能可折腾了我好久,先上一份代码,经楼主验证是绝对可以用的而且也比较清晰的代码!(ps:还是先剧透下吧,第三方大部分浏览器无法成功.) 点击浏览器中的URL链接,启动特定的App. 首先做 ...
- 动态的计算行高 加载数据源 有多少显示多少 tableView 包含 colloctionView 显示复杂的界面写法
有时候,我们经常碰到这样的需求 先遵守代理 @interface PublishCollectionCell ()<UICollectionViewDataSource, UICollectio ...
- 控制边框颜色:《CSS3 Border-color》
CSS3中有关于Border的属性一共有三个:圆角border-radius,图片边框border-images,边框多颜色border-color,其中圆角border-radius是常用的一个属性 ...
- VS2012下基本类型大小
- Linux 概念架构的理解
摘要 Linux kernel 成功的两个原因: 架构设计支持大量的志愿开发者加入到开发过程中: 每个子系统,尤其是那些需要改进的,都支持很好的扩展性. 正是这两个原因使得 Linux kernel ...
- CSS3的chapter4
段落样式: 行高——line-height p { line-height:25px | 150% | normal;} 段落缩进——text-indent p { text-indent:2em;} ...
- Sqrt(x) - LintCode
examination questions Implement int sqrt(int x). Compute and return the square root of x. Example sq ...
- (转)QML代码与现有Qt UI代码整合
http://blog.csdn.net/henreash/article/details/7934315