码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5352 MZL's City 匈牙利匹配

时间:2015-08-05 22:22:49      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:


求年份和城市的匹配

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
 


/* ***********************************************
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 匈牙利匹配

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47303411

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!