Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

200题纪念!

Posted by yc5_yc at 2012-11-01 22:36:07 on Problem 1422
先拆点,依照题目要求连边。
匈牙利即可。
贴代码:
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
const int NMax=200;
int N,M,connect[NMax];
bool ok[NMax],state[NMax];
vector<int> G[NMax];
bool DFS(int a) {
    state[a]=1;
    for(int i=0;i<G[a].size();i++) {
        if(!connect[G[a][i]]) {
            ok[i]=1;
            connect[G[a][i]]=a;
            return 1;
        }else if(!state[connect[G[a][i]]]&&DFS(connect[G[a][i]])) {
            ok[i]=1;
            connect[G[a][i]]=a;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int T,x,y;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++) {G[i].clear();ok[i]=0;connect[i]=0;}
        for(int i=1;i<=M;i++) {
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
        }
        int ret=0; 
        for(int i=1;i<=N;i++) {
            for(int j=0;j<G[i].size();j++) if(!connect[G[i][j]]){
                connect[G[i][j]]=i;
                ok[i]=1;
                ret++;
                break;
            }
        }
        for(int i=1;i<=N;i++) {
            memset(state,0,sizeof(state));
            if(!ok[i]) ret+=DFS(i);
        }
        printf("%d\n",N-ret);
    }
    //getchar();getchar();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator