CodeForces-687A(DFS,染色)
链接:
https://vjudge.net/problem/CodeForces-687A
题意:
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
思路:
第一眼以为最大匹配.其实就是dfs染色判断.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
vector<int> G[MAXN];
int Color[MAXN];
int n, m;
bool Dfs(int u, int v, int c)
{
Color[v] = c;
for (int i = 0;i < G[v].size();i++)
{
int node = G[v][i];
if (node == u)
continue;
if (Color[node] == Color[v])
return false;
if (Color[node] != -1)
continue;
if (!Dfs(v, node, c^1))
return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
memset(Color, -1, sizeof(Color));
cin >> n >> m;
int u, v;
for (int i = 1;i <= m;i++)
{
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
bool flag = true;
for (int i = 1;i <= n;i++)
{
if (Color[i] == -1 && !Dfs(0, i, 0))
flag = false;
}
if (!flag)
cout << -1 << endl;
else
{
int cnt = 0;
for (int i = 1;i <= n;i++)
if (Color[i] == 0)
cnt++;
cout << cnt << endl;
for (int i = 1;i <= n;i++)
if (Color[i] == 0)
cout << i << ' ' ;
cout << endl;
cnt = 0;
for (int i = 1;i <= n;i++)
if (Color[i] == 1)
cnt++;
cout << cnt << endl;
for (int i = 1;i <= n;i++)
if (Color[i] == 1)
cout << i << ' ' ;
cout << endl;
}
return 0;
}
CodeForces-687A(DFS,染色)的更多相关文章
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Gym100502A:Amanda Lounges(DFS染色)
http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...
- Codeforces 781A:Andryusha and Colored Balloons(DFS染色)
http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...
- Codeforces 1144F Graph Without Long Directed Paths DFS染色
题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...
- cf804C(dfs染色)
题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more c ...
- 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)
这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
随机推荐
- jmeter响应数据Unicode编码转换为汉字
2018-07-09 10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...
- string中getline,cin的方法getline(),get总结
一.string中的getline不是string的成员函数,属于全局函数,使用需要include<string>,有两个重载版本: 函数原型参见:http://www.cplusplus ...
- Django 中 ModelForm 的使用
定义 ModelForm 定制 ModelForm Meta 在 Form 中另外定义 Field 值得一提的一些 Field 转化 AutoField BooleanField ForeignKey ...
- Tomcat启动报错:“通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明“
从报错信息就可以明显察觉到是xml配置文件出现的问题 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- 【Ruby on Rails 学习四】简单的代码快和错误处理
第一个例子: 1 ... 5000的加法运算 1 sum = 0 2 i = 1 3 while true 4 sum += i 5 i += 1 6 break if i == 5001 7 end ...
- 【VS开发】使用WinPcap编程(4)——把网络数据包存储到一个文件中
这里用到的数据结构是pcap_dumper_t,这也是一个相当于文件描述符的东西,我们在用的时候先指定pcap_dumper_t *dumpfp; 使用两个函数来存储网络数据,一个是pcap_dump ...
- js --- ffmpeg rtsp 推流,websocket通信,cnavas 渲染
首先选择正确的ffmpeg包 百度搜索ffmpeg官网,点击进入下图所示. 点击download,进入下载页面,选择window,window下面只有一个builds版本,如图红线部分圈出来的 点击b ...
- 异常1(Exception)
父类 :Throwable(可抛出的) 有两个子类:Error(错误) Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...
- python nonlocal 的具体原理
很多文章都大概列了下nonlocal的具体用法,比如看到几篇文章写的 “nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量” 看完以后我感觉自己是懂了,但光从这句话来说还没完全理解它 ...
- python3爬取动态网站图片
思路: 1.图片放在<image>XXX</image>标签中 2.利用fiddler抓包获取存放图片信息的js文件url 3.利用requests库获取html内容,然后获取 ...