ural 1099. Work Scheduling 一般图最大匹配
2011-10-11 21:52
344 查看
There is certain amount of night guards that are available to protect the local junkyard from possible junk robberies. These guards need to scheduled in pairs, so that each pair guards at different
night. The junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired). Please note that each guard can be scheduled with only one of his colleagues and no guard
can work alone.
The first line of the input contains one number N ≤ 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs (i, j) follow, each such
pair means that guard #i and guard #j can work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards i.e. helmets, pants, jackets. It is impossible to
put small helmet on a guard with a big head or big shoes on guard with small feet). The input ends with Eof.
You should output one possible optimal assignment. On the first line of the output write the even number C, the amount of scheduled guards. Then output C/2 lines, each containing 2
integers (i, j) that denote that i and j will work together.
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=250;
int n;//点数(1->n)
int head;
int tail;
int Start;
int Finish;
int link
; //表示哪个点匹配了哪个点
int Father
; //这个就是增广路的Father……但是用起来太精髓了
int Base
; //该点属于哪朵花
int Q
;
bool mark
;
bool mat
;//邻接矩阵
bool InBlossom
;
bool in_Queue
;
void BlossomContract(int x,int y){
memset(mark,0,sizeof(mark));
memset(InBlossom,0,sizeof(InBlossom));
#define pre Father[link[i]]
int lca,i;
for (i=x;i;i=pre) {i=Base[i]; mark[i]=true; }
for (i=y;i;i=pre) {i=Base[i]; if (mark[i]) {lca=i; break;} } //寻找lca之旅……一定要注意i=Base[i]
for (i=x;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i]; //对于BFS树中的父边是匹配边的点,Father向后跳
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
for (i=y;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i]; //同理
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
#undef pre
if (Base[x]!=lca) Father[x]=y; //注意不能从lca这个奇环的关键点跳回来
if (Base[y]!=lca) Father[y]=x;
for (i=1;i<=n;i++)
if (InBlossom[Base[i]]){
Base[i]=lca;
if (!in_Queue[i]){
Q[++tail]=i;
in_Queue[i]=true; //要注意如果本来连向BFS树中父结点的边是非匹配边的点,可能是没有入队的
}
}
}
void Change(){
int x,y,z;
z=Finish;
while (z){
y=Father[z];
x=link[y];
link[y]=z;
link[z]=y;
z=x;
}
}
void FindAugmentPath(){
memset(Father,0,sizeof(Father));
memset(in_Queue,0,sizeof(in_Queue));
for (int i=1;i<=n;i++) Base[i]=i;
head=0; tail=1;
Q[1]=Start;
in_Queue[Start]=1;
while (head!=tail){
int x=Q[++head];
for (int y=1;y<=n;y++)
if (mat[x][y] && Base[x]!=Base[y] && link[x]!=y) //无意义的边
if ( Start==y || link[y] && Father[link[y]] ) //精髓地用Father表示该点是否
BlossomContract(x,y);
else if (!Father[y]){
Father[y]=x;
if (link[y]){
Q[++tail]=link[y];
in_Queue[link[y]]=true;
}
else{
Finish=y;
Change();
return;
}
}
}
}
void Edmonds(){
memset(link,0,sizeof(link));
for (Start=1;Start<=n;Start++)
if (link[Start]==0)
FindAugmentPath();
}
void output(){
memset(mark,0,sizeof(mark));
int cnt=0;//一般图最大匹配 最大点数
for (int i=1;i<=n;i++)
if (link[i]) cnt++;
printf("%d\n",cnt);
for (int i=1;i<=n;i++)
if (!mark[i] && link[i]){
mark[i]=true;//i和link[i]匹配
mark[link[i]]=true;
printf("%d %d\n",i,link[i]);
}
}
int main(){
int x,y;
scanf("%d",&n);
memset(mat,0,sizeof(mat));
while (scanf("%d%d",&x,&y)!=EOF)
mat[x][y]=mat[y][x]=1;
Edmonds();
output();
return 0;
}
night. The junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired). Please note that each guard can be scheduled with only one of his colleagues and no guard
can work alone.
Input
The first line of the input contains one number N ≤ 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs (i, j) follow, each suchpair means that guard #i and guard #j can work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards i.e. helmets, pants, jackets. It is impossible to
put small helmet on a guard with a big head or big shoes on guard with small feet). The input ends with Eof.
Output
You should output one possible optimal assignment. On the first line of the output write the even number C, the amount of scheduled guards. Then output C/2 lines, each containing 2integers (i, j) that denote that i and j will work together.
Sample
input | output |
---|---|
3 1 2 2 3 1 3 | 2 1 2 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=250;
int n;//点数(1->n)
int head;
int tail;
int Start;
int Finish;
int link
; //表示哪个点匹配了哪个点
int Father
; //这个就是增广路的Father……但是用起来太精髓了
int Base
; //该点属于哪朵花
int Q
;
bool mark
;
bool mat
;//邻接矩阵
bool InBlossom
;
bool in_Queue
;
void BlossomContract(int x,int y){
memset(mark,0,sizeof(mark));
memset(InBlossom,0,sizeof(InBlossom));
#define pre Father[link[i]]
int lca,i;
for (i=x;i;i=pre) {i=Base[i]; mark[i]=true; }
for (i=y;i;i=pre) {i=Base[i]; if (mark[i]) {lca=i; break;} } //寻找lca之旅……一定要注意i=Base[i]
for (i=x;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i]; //对于BFS树中的父边是匹配边的点,Father向后跳
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
for (i=y;Base[i]!=lca;i=pre){
if (Base[pre]!=lca) Father[pre]=link[i]; //同理
InBlossom[Base[i]]=true;
InBlossom[Base[link[i]]]=true;
}
#undef pre
if (Base[x]!=lca) Father[x]=y; //注意不能从lca这个奇环的关键点跳回来
if (Base[y]!=lca) Father[y]=x;
for (i=1;i<=n;i++)
if (InBlossom[Base[i]]){
Base[i]=lca;
if (!in_Queue[i]){
Q[++tail]=i;
in_Queue[i]=true; //要注意如果本来连向BFS树中父结点的边是非匹配边的点,可能是没有入队的
}
}
}
void Change(){
int x,y,z;
z=Finish;
while (z){
y=Father[z];
x=link[y];
link[y]=z;
link[z]=y;
z=x;
}
}
void FindAugmentPath(){
memset(Father,0,sizeof(Father));
memset(in_Queue,0,sizeof(in_Queue));
for (int i=1;i<=n;i++) Base[i]=i;
head=0; tail=1;
Q[1]=Start;
in_Queue[Start]=1;
while (head!=tail){
int x=Q[++head];
for (int y=1;y<=n;y++)
if (mat[x][y] && Base[x]!=Base[y] && link[x]!=y) //无意义的边
if ( Start==y || link[y] && Father[link[y]] ) //精髓地用Father表示该点是否
BlossomContract(x,y);
else if (!Father[y]){
Father[y]=x;
if (link[y]){
Q[++tail]=link[y];
in_Queue[link[y]]=true;
}
else{
Finish=y;
Change();
return;
}
}
}
}
void Edmonds(){
memset(link,0,sizeof(link));
for (Start=1;Start<=n;Start++)
if (link[Start]==0)
FindAugmentPath();
}
void output(){
memset(mark,0,sizeof(mark));
int cnt=0;//一般图最大匹配 最大点数
for (int i=1;i<=n;i++)
if (link[i]) cnt++;
printf("%d\n",cnt);
for (int i=1;i<=n;i++)
if (!mark[i] && link[i]){
mark[i]=true;//i和link[i]匹配
mark[link[i]]=true;
printf("%d %d\n",i,link[i]);
}
}
int main(){
int x,y;
scanf("%d",&n);
memset(mat,0,sizeof(mat));
while (scanf("%d%d",&x,&y)!=EOF)
mat[x][y]=mat[y][x]=1;
Edmonds();
output();
return 0;
}
相关文章推荐
- Ural 1099 Work Scheduling (一般图的最大匹配:带花树算法)
- URAL 1099 Work scheduling 一般图的最大匹配 带花树算法(模板)
- 【一般图最大匹配】URAL - 1099 Work Scheduling
- Work Scheduling URAL - 1099 一般图的最大匹配(带花树)
- URAL1099-Work Scheduling(一般图最大匹配(带花树))
- 【Learning】带花树——一般图最大匹配
- 带花树算法--一般图最大匹配
- 二分图最大匹配(匈牙利算法) URAL 1721 Two Sides of the Same Coin
- HDU 3551 Hard Problem 一般图的最大匹配(带花树)
- HDOJ 4687 Boke and Tsukkomi 一般图最大匹配带花树+暴力
- uoj79 一般图最大匹配(带花树)
- 完备匹配下的最大权匹配-KM算法的一般模板
- Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
- [一般图最大匹配 带花树] BZOJ 4405
- 【BZOJ4405】挑战NPC 带花树模板 一般图最大匹配
- 一般图最大匹配问题-带花树开花算法
- 【UOJ】#79. 一般图最大匹配
- 一般图最大匹配问题-带花树开花算法
- 【总结】一般图最大匹配
- 一般图带花树匹配(URAL - 1099)模板