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 <= ...
随机推荐
- shell脚本判断端口是否打开
[root@www zabbix_scripts]# cat check_httpd.sh #!/bin/bash a=`lsof -i: | wc -l` " ];then " ...
- 关机报 at-spi-bus-launcher
查看是否有autostart文件夹,有则不建立 mkdir -v ~/.config/autostart 可直接修改/etc/xdg/autostart/at-spi-dbus-bus.desktop ...
- java:JQuery(声明,JQ和JS对象的区别,prop,attr,addClass,offset,trigger,dblclick和change事件,hide,show,toggle,slideUp,slideDown,slideToggle,三种选择器,标签的获取,三张图片的放大与缩小)
1.JQuery: jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计 的宗旨是“ ...
- iOS 开发】解决使用 CocoaPods 执行 pod install 时出现 - Use the `$(inherited)` flag ... 警告
公司项目在执行 pod install 的时候总是出现很多黄色的警告,因为是警告并不会影响项目的正常编译,一直没有在意,但是总是有很多警告看起来很不舒服,于是就花了点时间解决掉了,下面将解决方法记录下 ...
- Java编程思想—八皇后问题(数组法、堆栈法)
Java编程思想-八皇后问题(数组法.堆栈法) 实验题目:回溯法实验(八皇后问题) 实验目的: 实验要求: 实验内容: (1)问题描述 (2)实验步骤: 数组法: 堆栈法: 算法伪代码: 实验结果: ...
- 【HANA系列】SAP HANA跟我学HANA系列之创建分析视图一
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA跟我学HANA系 ...
- DSP28335 eCAP 测频
F28335共有6组eCAP模块,每个eCAP不但具有捕获功能,而且还可用作PWM输出功能.F28335捕获模块的主要特征如下: 1. 150MHz系统时钟的情况下,32位时基的时间分辨率为6.67n ...
- Hibernate——简单的增、删、改、查操作
思路: 1.导入hibernate jar包 2.导入mysql jar包 3.创建数据库 4.创建java实体类 5.编写hibernate.cfg.xml配置文件 6.编写dao类 目录: 数据表 ...
- Akka系列(八):Akka persistence设计理念之CQRS
前言........ 这一篇文章主要是讲解Akka persistence的核心设计理念,也是CQRS(Command Query Responsibility Segregation)架构设计的典型 ...
- React基础篇学习
到今天为止, 使用react已经一年了, 现在整理一下入门时的一些重要知识, 帮助想要学习react的同学们理解某些内容. React 元素 React 元素,它是 React 中最小基本单位,我们可 ...