ural 1109 Conference (二分图匹配)

Description

On the upcoming conference were sent M representatives of country A and N representatives of country B ( M and N ≤ 1000). The representatives were identified with
1, 2, …, M for country A and 1, 2, …, N for country B. Before the conference K pairs of representatives were chosen. Every such pair consists of one member of delegatio
A and one of delegation B. If there exists a pair in which both member # i of A and member # j of B are included then # i and # j can negotiate. Everyone attending the
conference was included in at least one pair. The CEO of the congress center wants to build direct telephone connections between the rooms of the delegates, so that
 everyone is connected with at least one representative of the other side, and every connection is made between people that can negotiate. The CEO also wants to
minimize the amount of telephone connections. Write a program which given M, N, K and K pairs of representatives, finds the minimum number of needed connections.
 

Input

The first line of the input contains M, N and K. The following K lines contain the choosen pairs in the form of two integers p 1 and p 2, p 1 is member of A and p 2 is member
 of B.

Output

The output should contain the minimum number of needed telephone connections.

Sample Input

input output
3 2 4
1 1
2 1
3 1
3 2
3
 
 *题意:即将到来的会议A国派出M个代表,B国派出N个代表 (M and N <= 1000)
  * A国的代表编号为1, 2, ..., M ;B国的代表编号为1, 2, ..., N .
 *开会前, 要选择K对代表。每一对代表必须一个是A国的,一个是B国的。A国的成员i与B国的成员j就可结对
  * CEO想建立最少的电话联系。做完最大匹配之后问还需要最少多少根电话线才能把分开的点都联通起来.
  * 首行给定M,N,K。以下K行为结对的代表P1,P2。P1是A国的成员,P2是B国的成员。
  * 输出所需的最少电话联系。
  * 解法:最小路径覆盖:顶点数 - 最大匹配数
 
 
#include <iostream>
#include <string.h>
using namespace std;
int M,N,K;
int map[1001][1001],vis[1001],pre[1001];
bool DFS(int x)
{
    for(int i=1;i<=N;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=1;
            if(!pre[i]||DFS(pre[i]))
            {
                pre[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int i,ans=0;
    cin>>M>>N>>K;
    for(i=1;i<=K;i++)
    {
        int a,b;
        cin>>a>>b;
        map[a][b]=1;
    }
    for(i=1;i<=M;i++)
    {
        memset(vis,0,sizeof(vis));
        if(DFS(i))
            ans++;
    }
    cout<<M+N-ans<<endl;
    return 0;
}
 

你可能感兴趣的:(ural 1109 Conference (二分图匹配))