URAL - 1099 Work Scheduling一般图最大匹配-带花树

一、内容

There is a certain amount of night guards that are available to protect the local junkyard from possible junk robberies. These guards need to be scheduled in pairs so that each pair guards in a 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.

Input

The first line of the input contains one number N ≤ 222 which is a number 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.

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 2 integers ( i,  j) that denote that i and j will work together.

Example
input output

3
1 2
2 3
1 3

	

2
1 2

二、思路

  • 带花树模板

三、代码

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=505;
const int maxm=maxn*maxn*2;
int n,m,que[maxm],ql,qr,pre[maxn],tim=0;
struct edge {
    int v,nxt;
} e[maxm];
int h[maxn],tot=0;
int match[maxn],f[maxn],tp[maxn],tic[maxn];
bool vis[maxn];
int find(int x) {
    return f[x]==x?f[x]:f[x]=find(f[x]);
}
void add(int u,int v) {
    e[++tot]=(edge){v,h[u]};
    h[u]=tot;
}
int lca(int x,int y) {
    for (++tim;;swap(x,y)) if (x) {
        x=find(x);
        if (tic[x]==tim) return x; else tic[x]=tim,x=pre[match[x]];
    }
}
void shrink(int x,int y,int p) {
    while (find(x)!=p) {
        pre[x]=y,y=match[x];
        if (tp[y]==2) tp[y]=1,que[++qr]=y;
        if (find(x)==x) f[x]=p;
        if (find(y)==y) f[y]=p;
        x=pre[y];
    }
}
bool aug(int s) {
    for (int i=1;i<=n;++i) f[i]=i; //初始化并查集 
    memset(tp,0,sizeof tp),memset(pre,0,sizeof pre); 
    tp[que[ql=qr=1]=s]=1; // 1: type A ; 2: type B 起点类型是1 
    int t=0;
    while (ql<=qr) {
        int x=que[ql++]; 
        for (int i=h[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) {
            if (find(v)==find(x) || tp[v]==2) continue; //在同一朵话里面 或者 遇到B类点 
            if (!tp[v]) { //未访问过的点 
                tp[v]=2,pre[v]=x;
                if (!match[v]) {
                    for (int now=v,last,tmp;now;now=last) {
                        last=match[tmp=pre[now]];
                        match[now]=tmp,match[tmp]=now;
                    }
                    return true;
                } 
                tp[match[v]]=1,que[++qr]=match[v];
            } else if (tp[v]==1) {
                int l=lca(x,v);
                shrink(x,v,l);
                shrink(v,x,l);
            }
        }
    }   
    return false;
}
int main() {
    scanf("%d", &n);
    int x, y;
	while (~scanf("%d%d", &x, &y)) add(x,y),add(y,x);
    int ans=0;
    for (int i=1;i<=n;++i) if (!match[i]) aug(i); //匹配每一个点 
    for (int i = 1; i <= n; i++) if(match[i] > 0) ans++;
	printf("%d\n",ans);
    for (int i=1;i<=n;++i) if (i < match[i])printf("%d %d\n", i, match[i]);
    puts("");
    return 0;
}

发布了482 篇原创文章 · 获赞 492 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104564191