[二分图匹配]bzoj1143 - cool's Blog

[二分图匹配]bzoj1143

cool posted @ 2016年3月13日 18:27 in 图论 , 408 阅读

1143: [CTSC2008]祭祀river

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1870  Solved: 932
[Submit][Status][Discuss]

Description

在遥远的东方,有一个神秘的民族,自称Y族。他们世代居住在水面上,奉龙王为神。每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动。我们可以把Y族居住地水系看成一个由岔口和河道组成的网络。每条河道连接着两个岔口,并且水在河道内按照一个固定的方向流动。显然,水系中不会有环流(下图描述一个环流的例子)。

由于人数众多的原因,Y族的祭祀活动会在多个岔口上同时举行。出于对龙王的尊重,这些祭祀地点的选择必须非常慎重。准确地说,Y族人认为,如果水流可以从一个祭祀点流到另外一个祭祀点,那么祭祀就会失去它神圣的意义。族长希望在保持祭祀神圣性的基础上,选择尽可能多的祭祀的地点。

Input

第一行包含两个用空格隔开的整数N、M,分别表示岔口和河道的数目,岔口从1到N编号。接下来M行,每行包含两个用空格隔开的整数u、v,描述一条连接岔口u和岔口v的河道,水流方向为自u向v。

Output

第一行包含一个整数K,表示最多能选取的祭祀点的个数。

Sample Input

4 4
1 2
3 4
3 2
4 2

Sample Output

2

【样例说明】
在样例给出的水系中,不存在一种方法能够选择三个或者三个以上的祭祀点。包含两个祭祀点的测试点的方案有两种:
选择岔口1与岔口3(如样例输出第二行),选择岔口1与岔口4。
水流可以从任意岔口流至岔口2。如果在岔口2建立祭祀点,那么任意其他岔口都不能建立祭祀点
但是在最优的一种祭祀点的选取方案中我们可以建立两个祭祀点,所以岔口2不能建立祭祀点。对于其他岔口
至少存在一个最优方案选择该岔口为祭祀点,所以输出为2。
 
 

据说这题有两问,不过好像没数据,所以就去掉了(怪不得我都能看懂题解)

话说我完全不会二分图,更不会什么网络流,费用流(所以学习了简短的单纯形)

不过这道题真是道二分图好题(我什么都不懂!应该是我太弱了)

先学习了以下概念:引用自http://www.cnblogs.com/JoeFan/p/4324380.html

在有向无环图中,有如下的一些定义和性质:

链:一条链是一些点的集合,链上任意两个点x, y,满足要么 x 能到达 y ,要么 y 能到达 x 。

反链:一条反链是一些点的集合,链上任意两个点x, y,满足 x 不能到达 y,且 y 也不能到达 x。

那么很显然这道题就是求最长反链长度了。

一个定理:最长反链长度 = 最小链覆盖(用最少的链覆盖所有顶点)

对偶定理:最长链长度 = 最小反链覆盖  

所以这道题就是最小路径覆盖!(我是不会告诉你我还WA了十几发的)

#include <cstdio>
#include <cstring>
#define maxn 201
#define maxm 200001
using namespace std;
int h[maxm],last[maxm],head[maxn],from[maxn],a[maxn][maxn],l,b[maxn],ans;
void add(int x,int y)
{
	h[++l] = y;
	last[l] = head[x];
	head[x] = l;
}

int find(int x)
{
	for (int i = head[x];i;i = last[i])
	if (!b[h[i]])
	{
		b[h[i]] = 1;
		if ((!from[h[i]])||find(from[h[i]]))
		{
			from[h[i]] = x;
			return 1;
		}
	}
	return 0;
}

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i = 1;i <= m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		a[x][y] = 1;
	}
	for (int i = 1;i <= n;i++)
		for (int j = 1;j <= n;j++)
			for (int k = 1;k <= n;k++)
				a[j][k] |= a[j][i]&a[i][k];
	for (int i = 1;i <= n;i++)
		for (int j = 1;j <= n;j++)
			if (i!=j&&a[i][j]) add(i,j+n);
	for (int i = 1;i <= n;i++)
	{
		memset(b,0,sizeof(b));
		if (find(i)) ans--;
    }
    ans += n;
    printf("%d",ans);
    return 0;
}

 

AP SSC Telugu Model 说:
2022年9月14日 17:43

We advised contacting your Telugu teacher to get a chapter-wise practice model question paper for both levels of exams held under the school or board level and follow the link to download All AP SSC 10th Class Telugu Model Question Papers 2023 with Solutions. Every student everyone can download AP 10th Telugu Model Paper 2023 chapter-wise for paper-1, paper-2 exam theory, objective, AP SSC Telugu Model Paper multiple-choice questions (MCQ), Bit Question Bank with practice study material with IMP Question paper pdf with old scheme suggestions for AP 10th Class students 2023 to the Telugu Subject.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee