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) 如 ...
随机推荐
- Qt之键盘讲解
QWSInputMethod详解 注意:这个类不仅仅在嵌入式环境下有效 一个输入法包括了过滤器和可选的图形界面,用来过滤服务器和客户端应用程序之间 的输入事件. 创建自定义的输入法,需要得自QWSIn ...
- 关于Navigation的BarButtonItem的
(ios6.1)有两个controller在navigation stack里,A和B.A是B的previous view controller,现在top-level controller是B.要自 ...
- openDatabase() chrome vivaldi Stylish
located at /Users/ruili/Library/Application Support/Vivaldi/Default/databases/ Databases.db contains ...
- System.IO.File.Create 不会自动释放,一定要Dispose
这样会导致W3P进程一直占用这个文件 System.IO.File.Create(HttpContext.Current.Server.MapPath(strName)) 最好加上Dispose Sy ...
- Python之检查URL
# -*- coding: utf-8 -*- import os,sys import time import sys import pycurl #URL="http://www.bai ...
- flexigrid随手记
最近要用到flexigrid做表格,随手记一些知识点. 引入了两个jquery库(jquery.js和jquery-1.7.1.min.js),发生冲突,只保留一个 $("#gridTabl ...
- Android网络框架源码分析一---Volley
转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...
- prototype与原型链
1.今天翻看 阮一峰老师的博客看到了,一篇讲javascript为什么要设计出prototype,跳转 大意就是new 的方式有缺陷,没有共同的属性,一下明白了很多. 在来一张原型链的图:
- 手机号码归属地查询api接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
- 怎样在excel中添加下拉列表框
用excel2013打开要编辑的工作表,例子是一个班级名单,可以看到政治面貌目前还没有填写 接着我们找一个空白处,依次写入政治面貌的可能选项: 群众.共青团员 然后选中“政治面貌”这一列,点击 ...