URAL-1099Work Scheduling

1099. Work Scheduling

Time limit: 0.5 second
Memory limit: 64 MB
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.

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

Sample

input output
3
1 2
2 3
1 3
2
1 2
Problem Author: Jivko Ganev


题意:给出n个士兵,再给出多组士兵之间两两可以匹配的关系。已知某个士兵最多只能与一个士兵匹配。求最多能够有多少对匹配,并输出这些匹配。

解题思路:最大匹配问题,对于二分图来说用的是匈牙利算法,求一般图最大匹配用的是带花树开花算法。


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
#include <bitset>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

const int MAXN=250;
int n;//点的个数,点的编号从1到N
bool g[MAXN][MAXN];
int match[MAXN];
bool inqueue[MAXN],inpath[MAXN],inblossom[MAXN];
int head,tail;
int Queue[MAXN];
int start,finish;
int newBase;
int Father[MAXN],base[MAXN];
int Count;//匹配数,匹配对数是Count/2

void Push(int u)
{
    Queue[tail]=u;
    tail++;
    inqueue[u]=true;
}

int FindCommonAncestor(int u,int v)
{
    memset(inpath,false,sizeof(inpath));
    while(true)
    {
        u=base[u];
        inpath[u]=true;
        if(u==start) break;
        u=Father[match[u]];
    }
    while(true)
    {
        v=base[v];
        if(inpath[v]) break;
        v=Father[match[v]];
    }
    return v;
}

void ResetTrace(int u)
{
    int v;
    while(base[u]!=newBase)
    {
        v=match[u];
        inblossom[base[u]]=inblossom[base[v]]=true;
        u=Father[v];
        if(base[u]!=newBase) Father[u]=v;
    }
}

void BloosomContract(int u,int v)
{
    newBase=FindCommonAncestor(u,v);
    memset(inblossom,false,sizeof(inblossom));
    ResetTrace(u);
    ResetTrace(v);
    if(base[u]!=newBase) Father[u]=v;
    if(base[v]!=newBase) Father[v]=u;
    for(int tu=1;tu<=n;tu++)
    {
        if(inblossom[base[tu]])
        {
            base[tu]=newBase;
            if(!inqueue[tu]) Push(tu);
        }
    }
}

void FindAugmentingPath()
{
    memset(inqueue,false,sizeof(inqueue));
    memset(Father,0,sizeof(Father));
    for(int i=1;i<=n;i++) base[i]=i;
    head=tail=1;
    Push(start);
    finish=0;
    while(head<tail)
    {
        int u=Queue[head];
        head++;
        for(int v=1;v<=n;v++)
        {
            if(g[u][v]&&(base[u]!=base[v])&&(match[u]!=v))
            {
                if((v==start)||((match[v]>0)&&Father[match[v]]>0))
                    BloosomContract(u,v);
                else if(Father[v]==0)
                {
                    Father[v]=u;
                    if(match[v]>0) Push(match[v]);
                    else
                    {
                        finish=v;
                        return;
                    }
                }
            }
        }
    }
}

void AugmentPath()
{
    int u,v,w;
    u=finish;
    while(u>0)
    {
        v=Father[u];
        w=match[v];
        match[v]=u;
        match[u]=v;
        u=w;
    }
}

void Edmonds()
{
    memset(match,0,sizeof(match));
    for(int u=1;u<=n;u++)
    {
        if(match[u]==0)
        {
            start=u;
            FindAugmentingPath();
            if(finish>0) AugmentPath();
        }
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        int u,v;
        memset(g,false,sizeof(g));
        while(~scanf("%d%d",&u,&v))
            g[u][v]=g[v][u]=true;
        Edmonds();//进行匹配
        Count=0;
        for(int i=1;i<=n;i++)
            if(match[i]>0) Count++;
        printf("%d\n",Count);
        for(int i=1;i<=n;i++)
            if(i<match[i]) printf("%d %d\n",i,match[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值