您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 5 hdu 5352 MZL's City

2015-08-07 17:04 489 查看

MZL's City

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 743 Accepted Submission(s): 260


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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).

[align=left]Sample Input[/align]

1

5 6 2

2 1 2
2 1 3

1 1

1 2

3 1 1 2

1 2

[align=left]Sample Output[/align]

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.

[align=left]Source[/align]
2015 Multi-University Training Contest 5 1010-J

解题:拆点做最大匹配,倒着匈牙利即可以字典序最小

#include <bits/stdc++.h>
using namespace std;
const int maxn = 502;
int N,M,K;
struct arc {
int to,next;
arc(int x = 0,int y = -1) {
to = x;
next = y;
}
} e[2000100];
int head[100010],Link[maxn],ans[maxn],tot;
bool used[maxn],mp[maxn][maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
bool match(int u) {
for(int i = head[u]; ~i; i = e[i].next) {
if(!used[e[i].to]) {
used[e[i].to] = true;
if(Link[e[i].to] == -1 || match(Link[e[i].to])) {
Link[e[i].to] = u;
return true;
}
}
}
return false;
}
vector<int>con;
bool vis[maxn];
void dfs(int u) {
vis[u] = true;
con.push_back(u);
for(int i = 1; i <= N; ++i)
if(mp[u][i] && !vis[i])
dfs(i);
}
int hungary(int tot){
int ret = 0;
memset(ans,0,sizeof ans);
for(int i = tot-1; i >= 0; --i){
for(int j = 0; j < K; ++j){
memset(used,false,sizeof used);
if(match(i*K + j)){
ret++;
ans[i]++;
}
}
}
return ret;
}
int main(int c) {
int kase,op,u,v,tot;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&N,&M,&K);
memset(head,-1,sizeof head);
memset(mp,false,sizeof mp);
memset(Link,-1,sizeof Link);
for(int i = tot = ::tot = 0; i < M; ++i) {
scanf("%d",&op);
switch(op) {
case 1:
memset(vis,false,sizeof vis);
scanf("%d",&u);
con.clear();
dfs(u);
for(int j = 0; j < K; ++j) {
for(int k = con.size()-1; k >= 0; --k)
add(tot*K + j,con[k]);
}
++tot;
break;
case 2:
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = true;
break;
case 3:
scanf("%d",&op);
while(op--) {
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = false;
}
break;
default:
break;
}
}
printf("%d\n",hungary(tot));
for(int i = 0; i < tot; ++i)
printf("%d%c",ans[i],i+1==tot?'\n':' ');
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: