1109. Conference

http://acm.timus.ru/problem.aspx?space=1&num=1109

匈牙利算法简单题  好久没写匈牙利算法了 

先让A组 和 B组 尽量多的配对  剩余人数每个人连一根

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<vector>

#include<map>

#include<cmath>

#include<queue>

#include<stack>

#include<algorithm>



using namespace std;

const int INF=0x3f3f3f3f;

const int N=1005;

vector<int>negotiate[N];

int f[N];

bool visited[N];

bool dfs(int x)

{//cout<<x<<endl;

    for(unsigned int i=0;i<negotiate[x].size();++i)

    {

        if(visited[ negotiate[x][i] ])

        continue;

        visited[ negotiate[x][i] ]=true;

        if(f[ negotiate[x][i] ]==-1||(dfs( f[ negotiate[x][i] ] )))

        {

            f[negotiate[x][i]]=x;

            return true;

        }

    }

    return false;

}

int main()

{

    //freopen("data.txt","r",stdin);

    int n,m,k;

    while(scanf("%d %d %d",&n,&m,&k)!=EOF)

    {

        for(int i=1;i<=n;++i)

        negotiate[i].clear();

        while(k--)

        {

            int l1,l2;

            scanf("%d %d",&l1,&l2);

            negotiate[l1].push_back(l2);

        }

        memset(f,-1,sizeof(f));

        int tmp=0;

        for(int i=1;i<=n;++i)

        {

            memset(visited,false,sizeof(visited));

            if(dfs(i))

            ++tmp;

        }

        printf("%d\n",n+m-tmp*2+tmp);

    }

    return 0;

}

 

你可能感兴趣的:(conf)