HDU 2063 Roller Coaster (Bipartite Graph Maximum Matching)

Topic link: http://acm.hdu.edu.cn/viewcode.php?rid=24466075

Problem Description
RPG girls went to the playground with everyone today, and finally got on the roller coaster of their dreams. However, there are only two seats in each row of the roller coaster, and there is an unwritten rule that every girl must find a boy as a partner to sit with her. However, every girl has their own ideas. For example, Rabbit is only willing to partner with XHD or PQK, Grass is only willing to partner with linle or LL, and PrincessSnow is willing to partner with waters prodigal son or pseudoqueer. Considering the issue of funding, Boss Liu decided to let only those who found the partner ride the roller coaster, and the others, hehe, just stand below and watch. Smart Acmer, can you help me figure out how many pairs can get on the roller coaster at most?
 

Input
The first line of the input data is three integers K , M , N, which represent the number of possible combinations, the number of girls, and the number of boys. 0<K<=1000
1<=N and M<=500. In the next K lines, each line has two numbers, which respectively indicate that the girl Ai is willing to be a partner with the boy Bj. The last 0 ends the input.
 

Output
For each set of data, output an integer representing the maximum number of combinations that can be taken on a roller coaster.
 

Sample Input
 
  
6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
 

Sample Output
 
  
3


Idea: Bipartite graph maximum matching naked question

Code:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1005;

int line[N][N]; //Is there a relationship between i and j, that is, it can be matched
int used[N]; //Whether the i-th girl has been used when looking for the augmented path in each round
int Next[N]; //Next[i]=x means that the i-th girl matches the boy of number x
int n,m;

bool Find(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(line[x][i]&&!used[i])
        {
            used[i] = 1; //The ith girl is used
            if(Next[i]==0||Find(Next[i])) //Determine whether the i-th girl has a boyfriend, if not, it can be matched, if there is, transfer the boy number next[i] drop and match
            {
                Next[i] = x;
                return true;
            }
        }
    }
    return false;
}

int maxMatch()
{
    int sum = 0;
    for(int i=1;i<=n;i++)
    {
        memset(used,0,sizeof(used));
        if(Find(i))
            sum++;
    }
    return sum;
}

intmain()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int k,x,y;
    while(cin>>k)
    {
        if(k==0)
            break;
        cin>>n>>m;
        MEM(line,0);
        MEM(Next,0);
        for(int i=1;i<=k;i++)
        {
            cin>>x>>y;
            line[x][y]=1;
        }
        cout<<maxMatch()<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652742&siteId=291194637