URAL 1099 Work Scheduling

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

3
1 2
2 3
1 3

output

2
1 2

這題就典型的一般圖最大匹配,套一個帶花樹算法的模板即可,比較迷的是我上網找了一些板子改了總是不對……,可見網上的代碼水分非常大,我建議如果樣例測了不對就趕緊找下一個模板,這樣節省時間😂。我這個是用並查集的,也簡潔易懂供大家參考:

#include <bits/stdc++.h>
using namespace std;
const int N=250;
deque<int> q;
bool g[N][N],inque[N],inblossom[N],vis[150];
int match[N],pre[N],base[N];
int n,m;
int Find(int u,int v){
    bool inpath[N]={false};
    while(1){
        u=base[u];
        inpath[u]=true;
        if(match[u]==-1)break;
        u=pre[match[u]];
    }
    while(1){
        v=base[v];
        if(inpath[v])return v;
        v=pre[match[v]];
    }
}

void reset(int u,int anc){
    while(u!=anc){
        int v=match[u];
        inblossom[base[u]]=1;
        inblossom[base[v]]=1;
        v=pre[v];
        if(base[v]!=anc)pre[v]=match[u];
        u=v;
    }
}

void contract(int u,int v,int n){
    int anc=Find(u,v);
    memset(inblossom,0,sizeof(inblossom));
    reset(u,anc);reset(v,anc);
    if(base[u]!=anc)pre[u]=v;
    if(base[v]!=anc)pre[v]=u;
    for(int i=1;i<=n;i++)
        if(inblossom[base[i]]){
            base[i]=anc;
            if(!inque[i]){
                q.push_back(i);
                inque[i]=1;
            }
        }
}

bool dfs(int s,int n){
    for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;
    q.clear();q.push_back(s);inque[s]=1;
    while(!q.empty()){
        int u=q.front();q.pop_front();
        for(int v=1;v<=n;v++){
            if(g[u][v]&&base[v]!=base[u]&&match[u]!=v){
                if(v==s||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);
                else if(pre[v]==-1){
                    pre[v]=u;
                    if(match[v]!=-1)q.push_back(match[v]),inque[match[v]]=1;
                    else{
                        u=v;
                        while(u!=-1){
                            v=pre[u];
                            int w=match[v];
                            match[u]=v;
                            match[v]=u;
                            u=w;
                        }
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

void calculate()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n;i++)
        if(match[i]==-1&&dfs(i,n)) ans++;
    printf("%d\n",ans*2);
    for(int i=1;i<=n;i++)
        if(i<match[i]) printf("%d %d\n",i,match[i]);
}

int main()
{
    int a,b;
    scanf("%d",&n);
    memset(g,false,sizeof(g));
     while(~scanf("%d%d",&a,&b)){
        g[a][b]=g[b][a]=true;
    }
    calculate();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章