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 <= ...
随机推荐
- Wireshark 学习笔记 Lebal:Research
学习Wireshark主要是为了契合我最近做的线性激光雷达项目,主要用于抓取数据包 首先是三本书比较值得一看,第一本是清华大学出版社的,侧重教学,第二三两本是人民邮电出版社的,其中第二本是许多课程的指 ...
- 模糊查询基于select遍历json文件自动填充的实现
HTML页面 <tr> <td align="left"><span>案由</span> <input type=" ...
- LeetCode.1018-可被5整除的二进制数(Binary Prefix Divisible By 5)
这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第 ...
- python_面试题_DB相关问题
1.mysql部分 问题 问题1:mysql的存储引擎 问题2:mysql的索引机制 问题3:mysql的事务以及事务隔离级别 问题4:mvcc/GAP lock是做什么的 问题5:mysql的悲观锁 ...
- 小型自动化运维工具pssh和传输工具rsync
一.简单介绍 1.pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括pssh ...
- 1.2.2 OSI参考模型 下
[今天打酱油了,纯抄书.OSI太重要,不敢随便乱写.] 一.开放系统互联参考模型 答:20世纪80年代初,ISO提出来著名的开放系统互联参考模型[Open Systems Interconnectio ...
- 异常1(Exception)
父类 :Throwable(可抛出的) 有两个子类:Error(错误) Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...
- 交换机安全学习笔记 第四章 VLAN
Trunk 口 思科称为:native VLAN 华为称为:PVID 说白了就是Trunk端口本身所属的VLAN,因为,Trunk端口要"透传"多个VLAN的流量,其本 ...
- hive_server2的权限控制
CDH的core-sit开启: 第一个false表示用系统用户来和hive用户的权限绑定,但经测试并没有生效,所以可以改为true 第二项设置成ALL,表示创建者对其创建的表拥有所有的权限,这样也是比 ...
- C语言 --- 高级指针
1. 指针赋值: C语言允许使用赋值运算进行指针的赋值,前提是两个指针具有相同的类型. int i,*p,*q; p = &i; ...