四川第七届 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 ...
随机推荐
- 【转载】如何简单地理解Python中的if __name__ == '__main__'
原帖:https://blog.csdn.net/yjk13703623757/article/details/77918633 通俗的理解__name__ == '__main__':假如你叫小明. ...
- JSP--常用指令
1.JSP中的page指令: jsp中指令格式:<%@ 指令名字 key=value key=value ......%> <%@ page language=& ...
- CentOS防火墙iptables-config的相关配置参数详解
默认/etc/sysoncifg/iptables-config的配置内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- flume-ng源码阅读RollingFileSink(原创)
org.apache.flume.sink.RollingFileSink 这个类比较简单. source的种类有两种:一种是PollableSource:另外一种是EventDrivenSou ...
- eclipse下进行c开发,使用zeromq
使用eclipse开发代码,一段zeromq的代码. #include <string.h> #include <stdio.h> #include <unistd.h& ...
- weinre远程调试
一: 关于weinre weinre是一款依赖于nodejs的远程调试工具,现阶段一般用到手机app上调试非常的强大 二: weinre的安装 1) 安装 nodejs以及npm 2) 安装wein ...
- 有关linux下redis overcommit_memory的问题,有需要的朋友可以参考下。
我在安装redis-4.0.6后,启动时出现一些问题,如下: :M Jan ::! Background save may fail under low memory condition. To fi ...
- Excel 导入到处问题处理!
1.未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 因为没有安装64位的Jet40驱动.可以到 http://www.microsoft.com/downl ...
- doeNET Framework 农历 ChineseLunisolarCalendar
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe test.cs # test.cs using System; using System.Diagnos ...
- 倒置字符串函数reverse
倒置字符串函数reverse:用于倒置字符串s中的各个字符的位置, 如原来字符串中如果初始值为123456,则通过reverse函数可将其倒置为654321, 程序如下: #include<st ...