Bipartite Graph Match 2

/****************************************************************************************************
          Target: Bipartite Graph Match (According to the problem named "star")  O(nlog(n))
                                                               —— From WZJ
          Author: Xue Zhonghao
          Date: 2014-3-18 19:11:11 
****************************************************************************************************/
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<iostream>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");

#define cin fin

#define MAXA 5000
#define MAXB 5000

int a[MAXA][MAXB];
bool vis[MAXB];
int b[MAXB];

bool find(int k) {
    if(k == 0) return true;
    for(int i = 1; i <= a[k][0]; ++i)
      if(!vis[a[k][i]]) {
        vis[a[k][i]] = true;
        if(find(b[a[k][i]])) {
          vis[a[k][i]] = false;
          b[a[k][i]] = k;
          return true;
        }
      }
    return false;
}

int main(void)
{
    int N, K;
    cin>>N>>K;
    int x, y;
    for(int i = 0; i < K; ++i) {
      cin>>x>>y;
      a[x][++a[x][0]] = y;
    }
    int ans = 0;
    for(int i = 0; i < N; ++i)
      if(find(i)) ans++;
    cout<<ans<<endl;
    system("pause");
    return 0;
}

 

posted on 2014-04-07 16:54  AlanXue  阅读(196)  评论(0编辑  收藏  举报

导航