四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)
Vertex Cover
frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm))(v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm)).
She would like to color some vertices so that each edge has at least one colored vertex.
Find the minimum number of colored vertices.
Input
The input consists of multiple tests. For each test:
The first line contains 22 integers n,mn,m (2≤n≤500,1≤m≤n(n−1)22≤n≤500,1≤m≤n(n−1)2). Each of the following mm lines contains 22 integers ai,biai,bi (1≤ai,bi≤n,ai≠bi,min{ai,bi}≤301≤ai,bi≤n,ai≠bi,min{ai,bi}≤30)
Output
For each test, write 11 integer which denotes the minimum number of colored vertices.
Sample Input
3 2
1 2
1 3
6 5
1 2
1 3
1 4
2 5
2 6
Sample Output
1
2
题意:有n个点m条边,每条边至少有一个顶点染色,至少要染多少个点
思路:二分图最小点覆盖,有一个公式 二分图最大匹配=最小点覆盖,匈牙利算法一代就出来了
#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
#define ll unsigned long long
vector<int>v[];
int match[];
bool used[];
bool dfs(int x)
{
used[x]=;//标记询问过了
for(int i=;i<v[x].size();i++)
{
int y=v[x][i];
int w=match[y];
if(w<||!used[w]&&dfs(w))
{//如果它没有匹配过或者(没有问过而且可以找到其它)
match[x]=y;
match[y]=x;
return ;
}
}
return ;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=;i<=;i++) v[i].clear();
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
int res=;
memset(match,-,sizeof(match));
for(int i=;i<=n;i++)
{
if(match[i]<)//还没匹配
{
memset(used,,sizeof(used));//每次都要初始化
if(dfs(i))
//如果能匹配
res++;
}
}
}
cout<<res<<endl;
}
return ;
}
四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)的更多相关文章
- hdu 2236(二分图最小点覆盖+二分)
无题II Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- POJ2226 Muddy Fields(二分图最小点覆盖集)
题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...
- POJ1325 Machine Schedule(二分图最小点覆盖集)
最小点覆盖集就是在一个有向图中选出最少的点集,使其覆盖所有的边. 二分图最小点覆盖集=二分图最大匹配(二分图最大边独立集) 这题A机器的n种模式作为X部的点,B机器的m种模式作为Y部的点: 每个任务就 ...
- hihoCoder #1127:二分图最小点覆盖和最大独立集
题目大意:求二分图最小点覆盖和最大独立集. 题目分析:如果选中一个点,那么与这个点相连的所有边都被覆盖,使所有边都被覆盖的最小点集称为最小点覆盖,它等于最大匹配:任意两个点之间都没有边相连的最大点集称 ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- 二分图 最小点覆盖 poj 3041
题目链接:Asteroids - POJ 3041 - Virtual Judge https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...
- HihoCoder1127 二分图三·二分图最小点覆盖和最大独立集
二分图三·二分图最小点覆盖和最大独立集 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上次安排完相亲之后又过了挺长时间,大家好像都差不多见过面了.不过相亲这个事不是说 ...
- hihoCoder #1127 : 二分图二·二分图最小点覆盖和最大独立集
#1127 : 二分图二·二分图最小点覆盖和最大独立集 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上次安排完相亲 ...
- Asteroids POJ - 3041 二分图最小点覆盖
Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in ...
随机推荐
- LeetCode——same-tree
Question Given two binary trees, write a function to check if they are equal or not. Two binary tree ...
- nginxif多条件结合判断(实现限速)
参考文章: https://yq.aliyun.com/articles/44957 需求: 要对某一ip下,使用android客户端的用户进行限速 原理 就是用SET变量进行. AND 就用变量叠加 ...
- 字典:dict.c/dict.h
Redis 源码分析(1):字典和哈希表(dict.c 和 dict.h)http://huangz.iteye.com/blog/1455808两个点:字典结构的运作流程哈希表的渐进式 rehash ...
- sorket is closed
今早来公司启动项目时,发现在代码正确的情况下报了一个错. 15:19:14.362 [http-9000-2] ERROR com.nari.osp.servicebus.rmi.LongConnec ...
- The tag handler class for "home.jsp" (org.apache.taglibs.standard.tag.rt.core.ForEachTag) was not found on the Java Build Path
web.xml中 listener,filter,servlet需按顺序. <listener> <listener-class>listener.VisitCountList ...
- WPF/WP/Silverlight/Metro App代码创建动画的思路
在2010年之前,我都是用Blend创建动画,添加触发器实现自动动画,后来写成代码创建的方式.如今Blend已经集成到Visual Studio安装镜像中了,最新的VS2015安装,Blend的操作界 ...
- 51nod 1040 欧拉函数
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 1040 最大公约数之和 题目来源: rihkddd 基准时间限制 ...
- mysql 授予远程连接直接访问
不通过ssh通道,mysql 授予远程连接直接访问 语句 GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '!DSJdg!' WITH GR ...
- Mybatis_总结_05_用_Java API
一.前言 使用 MyBatis 的主要 Java 接口就是 SqlSession.你可以通过这个接口来执行命令,获取映射器和管理事务. 二.主要类 (1)SqlSession 是由 SqlSessio ...
- python中读写LMDB数据库
LMDB的全称是Lightning Memory-Mapped Database(快如闪电的内存映射数据库),它的文件结构简单,包含一个数据文件和一个锁文件: LMDB文件可以同时由多个进程打开,具有 ...