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 <= ...
随机推荐
- java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)
1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- plsql连接本地oracle数据库,而远程主机却无法连接,出现无监听程序的解决方法(转)
原文转自:plsql连接本地oracle数据库,而远程主机却无法连接,出现无监听程序的解决方法 最近在使用plsql连接本地oracle数据库的时候,在同一网络环境中,出现了可以连接本地oracle, ...
- 通过NGINX location实现一个域名访问多个项目
location ~ \.php$ { root /home/webroot; //此目录下有多个项目 project1 ,project2... fastcgi_pass $php_upstr ...
- python基础之字符串索引与切片
字符串索引与切片:切片后组成新字符串与原字符串无关系增:str1+str2查:str1[index] str1[start_index:end_index]1,索引从0开始2,根据索引获取元素:索引超 ...
- c语言l博客作业07
一.本周教学内容&目标 第3章 分支结构 3.3 使学生熟悉多分支结构switch语句的使用. 二.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://e ...
- C# StreamReader与StreamWriter
原文:https://www.cnblogs.com/kissdodog/archive/2013/01/27/2878667.html StreamReader实现了抽象基类TextReader类, ...
- 开发完成的springboot项目扩展 swagger
第一步:pom.xml 引入 swagger 配置 <swagger.version>2.9.2</swagger.version> <!--swagger start- ...
- 2017.10.21 C组比赛总结
今天考得不太好,只拿了100+0+0+30=130分... [GDKOI训练]音乐节拍 考场AC了! 其实就是大水一道! 思路:二分查找 每次输入后,输出该时刻所在的区间的编号就好了. 总体难度:★★ ...
- Python自学笔记之计算机基础
osi七层协议应用层-表示层-会话层-传输层-网络层-数据链路层-物理层 无线网协议 ethernet 物理层:网线,光纤 数据链路层:arp协议 mac地址,广播 在广播域内传播 网络层:ip地址标 ...
- Android热修复、插件化、组件化
模块化:项目按照独立的模块进行划分 组件化:将项目按照单一的组件来进行划分结构 项目组件化的重要环节在于,将项目按照模块来进行拆分,拆分成一个个业务module和其他支撑module(lib),各个业 ...