HDOJ 5352 MZL's City 匈牙利匹配

求年份和城市的匹配

MZL‘s City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 539    Accepted Submission(s): 180

Problem Description

MZL is an active girl who has her own country.

Her big country has N cities numbered from 1 to N.She has controled the country for so long and she only remebered that there was a big earthquake M years ago,which made all the roads between the cities destroyed and all the city became broken.She also remebered
that exactly one of the following things happened every recent M years:

1.She rebuild some cities that are connected with X directly and indirectly.Notice that if a city was rebuilt that it will never be broken again.

2.There is a bidirectional road between city X and city Y built.

3.There is a earthquake happened and some roads were destroyed.

She forgot the exactly cities that were rebuilt,but she only knew that no more than K cities were rebuilt in one year.Now she only want to know the maximal number of cities that could be rebuilt.At the same time she want you to tell her the smallest lexicographically
plan under the best answer.Notice that 8 2 1 is smaller than 10 0 1.

Input

The first contains one integer T(T<=50),indicating the number of tests.

For each test,the first line contains three integers N,M,K(N<=200,M<=500,K<=200),indicating the number of MZL’s country ,the years happened a big earthquake and the limit of the rebuild.Next M lines,each line contains a operation,and the format is “1 x” , “2
x y”,or a operation of type 3.

If it’s type 3,first it is a interger p,indicating the number of the destoyed roads,next 2*p numbers,describing the p destoyed roads as (x,y).It’s guaranteed in any time there is no more than 1 road between every two cities and the road destoyed must exist
in that time.

Output

The First line Ans is the maximal number of the city rebuilt,the second line is a array of length of tot describing the plan you give(tot is the number of the operation of type 1).

Sample Input

1
5 6 2
2 1 2
2 1 3
1 1
1 2
3 1 1 2
1 2

Sample Output

3
0 2 1

Hint

 No city was rebuilt in the third year,city 1 and city 3 were rebuilt in the fourth year,and city 2 was rebuilt in the sixth year.

Source

2015 Multi-University Training Contest 5

/* ***********************************************
Author        :CKboss
Created Time  :2015年08月05日 星期三 16时16分29秒
File Name     :HDOJ5352.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=10010;

int n,m,k;
int mark[maxn];
bool g[202][202],used[maxn];

/*************edge*****************/

struct Edge
{
	int to,next;
}edge[maxn];

int Adj[maxn],Size;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	Adj[u]=Size++;
}

void link_edge(int year,int u)
{
	Add_Edge(year,u);
	for(int i=1;i<=n;i++)
	{
		if(g[u][i]==true&&used[i]==false)
		{
			used[i]=true;
			link_edge(year,i);
		}
	}
}

/*************hungary*****************/

int linker[maxn];
bool dfs(int u)
{
	for(int i=Adj[u];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!used[v])
		{
			used[v]=true;
			if(linker[v]==-1||dfs(linker[v]))
			{
				linker[v]=u;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d%d",&n,&m,&k);

		init();
		memset(g,0,sizeof(g));
		memset(mark,0,sizeof(mark));

		for(int i=0,k,x,y;i<m;i++)
		{
			scanf("%d",&k);
			if(k==1)
			{
				scanf("%d",&x);
				memset(used,false,sizeof(used));
				used[x]=true;
				link_edge(i,x);
				mark[i]=true;
			}
			else if(k==2)
			{
				scanf("%d%d",&x,&y);
				g[x][y]=g[y][x]=1;
			}
			else if(k==3)
			{
				int p;
				scanf("%d",&p);
				while(p--)
				{
					scanf("%d%d",&x,&y);
					g[x][y]=g[y][x]=0;
				}
			}
		}

		int au=0;
		vector<int> vi;
		memset(linker,-1,sizeof(linker));

		for(int i=m-1;i>=0;i--)
		{
			if(mark[i]==true)
			{
				int tu=0;
				for(int j=0;j<k;j++)
				{
					memset(used,0,sizeof(used));
					if(dfs(i)==true) tu++;
				}
				au+=tu;
				vi.push_back(tu);
			}
		}

		printf("%d\n",au);
		for(int sz=vi.size(),i=sz-1;i>=0;i--)
			printf("%d%c",vi[i],(i==0)?'\n':' ');
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 5352 MZL's City 匈牙利匹配

时间: 2024-10-17 19:42:21

HDOJ 5352 MZL's City 匈牙利匹配的相关文章

Hdu 5352 MZL&#39;s City (多重匹配)

题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一条双向路(x,y) 3:又发生了地震,p条路被毁. 问最后最多有多少个节点被重建,输出重建节点的最小字典序. 解题思路: 这几天正好在学匹配,但是昨天下午还是没有看出来这个是匹配题目.看了题解扪心自问了自己三次,是不是傻.就是把每个需要重建的节点x拆成k个点,然后对每个拆分后的点和与拆点在同一连通块

HDU 5352——MZL&#39;s City——————【二分图多重匹配、拆点】

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 710    Accepted Submission(s): 245 Problem Description MZL is an active girl who has her own country. Her big country has N cities numb

hdu 5352 MZL&#39;s City 【二分图】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 题意: 给你n,m,k 表示n个建筑 m次操作,修复操作每次最多修复k个建筑. 有三种操作 1.修复x点周围建筑(<=k) 2.x,y建筑间建边 3.x,y间删边 修复建筑时候拆点建图.反着求最大匹配,保证字典序最小. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <l

HDU 5352 MZL&#39;s City

最小费用最大流,因为要控制字典序,网络流控制不好了...一直WA,所以用了费用流,时间早的费用大,时间晚的费用少. 构图: 建立一个超级源点和超级汇点.超级源点连向1操作,容量为K,费用为COST,然后COST-1,因为下一次遇到1操作,费用为减少1,即COST-1: 每个1操作连向当前能建造的城市,容量为1,费用为0: 每个城市连向汇点,容量为1,费用为0: #include<cstdio> #include<cstring> #include<cmath> #inc

HDU 5352 MZL&#39;s City(2015 多校 第5场,最小费用最大流)

  MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 94 Problem Description MZL is an active girl who has her own country. Her big country has N cities nu

hdu 5352 MZL&#39;s City 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 题意:M年前有一个国家发生了地震.所有城市和城市之间的路都被摧毁了.现在要重建城市. 给出一个N表示共有N个城市,M表示大地震发生在M年前,K表示每次最多只能重建K个城市. 现在从大地震发生的那年开始,每年可以进行一个操作,也就是总共有M个操作. 1 x表示可以重建和x联通(直接或间接)的城市(包括x本身),每次最多只能重建K个城市 2 x y 表示修建了一条城市x到城市y的路. 3操作给出一

MZL&#39;s City (hdu 5352 最小费用流 ||二分图匹配)

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 719    Accepted Submission(s): 251 Problem Description MZL is an active girl who has her own country. Her big country has N cities num

hdu5352 MZL&#39;s City(最小费用最大流问题)

题意描述: 一个国家有N个城市,标号1~N,初始时城市和道路都被破坏,下面进行以下三个操作: 第一种操作:1 X 修建城市,和X(包括X自己也可以被修建)直接相连或间接相连的城市可以被一次性修建(但一次性最多修建k个城市) 第二种操作:2 X Y 在X 与 Y之间建立一条道路 第三种操作:3 p X1 Y1 X2 Y2 ··· 表示破坏X1与Y1.X2与Y2之间的道路 经过M次操作后,对于这M次操作中的操作一,我们在每次操作一时修建多少城市修建那些城市才能使最终城市数量最多? 并输出最终修建城市

HDOJ 5343 MZL&#39;s Circle Zhou 后缀自动机

对第二个串建SAM求出第二个串的以每个字符开头的不同子串的数目.. 再对第一个串建SAM,遍历自动机如果某个节点后面没有某个字符则答案加上这个节点的出现次数乘上以这个字符为开头的在第二个串中的不同子串的数目.. MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 244    Accepted Sub