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) 如 ...
随机推荐
- 在linux中访问virtualbox的共享文件夹
1.在客户机里需要安装Virtualbox的增强功能. 2.使用virtualbox的图形界面设置好共享文件夹. 3.假设你设置的共享文件夹的名称是 share,使用如下命令在客户机的linux系统中 ...
- Qt之自定义信号和槽函数
自定义信号和槽函数: 1.类的声明和实现分别放在.h和.cpp文件中: 2.类声明包含Q_OBJECT宏: 3.信号只要声明不要设计其的实现函数 4.发射信号用emit关键字 5.自定义槽的实现与普通 ...
- Qt之重写QLabel类
在mylabel.h 文件中#ifndef MYLABEL_H#define MYLABEL_H #include <QLabel>/*重新实现QLabel类,使其支持点击事件*/clas ...
- Head中的标签
Head中的其它一些用法 1.scheme (方案) 说明:scheme can be used when name is used to specify how the value of conte ...
- adpatch options=hotpatch
--no need to shutdown application and no need to enable maintenance mode adpatch options=hotpatch fi ...
- zigbee学习之路(七):定时器3(中断方式)
一.前言 上次我们学习了了用定时器3进行查询方式来进行溢出判断,今天我们来换一种方式,用中断方式来检测和查询定时器3的溢出. 二.原理与分析 要使用定时器3,我们必须先要配置的是T3CTL,来把定时器 ...
- VC++6.0在Win7以上系统上Open或Add to Project files崩溃问题 解决新办法
崩溃原因是和office高版本冲突,比如我64位win7装了64位office2013及visio就遇到了这个问题(我很纳闷,记得重装系统前装的是32位office2013及visio就未曾遇到该问题 ...
- C++字符串与转移字符
先看以下代码: #include<iostream> #include<string> using namespace std; int main() { string str ...
- 没有技术说明文档的开源都是耍流氓:微软Roslyn编译即服务在CIIP中具体应用(上)
前段时间我发布了 github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端,很多园友们都表示支持并与我探讨相关技术问题,上篇中我也承诺会写相关的技术文章,本篇就来介绍一下建模模块中使用 ...
- centos使用yum安装mysql
参考:http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 1.增加yum数据源 (1.1)从http://dev.mysql.com/dow ...