hdu 1068 Girls and Boys(水题,二分图匹配)

小记:这题看是10s,其实很水,就是点多,但边少。所以用邻接表会快很多。


思路:直接套二分图匹配的模板就可以过,如果想要速度快,改成邻接表即可,

这里我是使用的前向星,171ms。


代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define REPL(a, b) for(int a = head[b]; a+1; a = edge[a].next)
#define eps 10e-8
#define DEBUG printf("here");

const int MAX_ = 1010;
const int N = 100010;
const int INF = 0x7fffffff;

//int g[MAX_][MAX_];
bool vis[MAX_];
int link[MAX_];
int head[MAX_*2];
int n, M;

struct node{
    int v;
    int cap, next;
}edge[MAX_*2];

void add(int u, int v, int cap)
{
    edge[M].v = v;
    edge[M].cap = cap;
    edge[M].next = head[u];
    head[u] = M++;
}

int dfs(int s)
{
    REPL(j, s){
        int i = edge[j].v;
        if(!vis[i]){
            vis[i] = 1;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = s;
                return 1;
            }
            //vis[i] = 0;
        }
    }
    return 0;
}

int bfs()
{
    mst(link, -1);
    int ans = 0;
    REP(i, 0, n){
        mst(vis, 0);
        if(dfs(i))++ans;
    }
    return ans;
}

int main(){
    int T,  m, s, t;

    while(~scanf("%d", &n)){
        //mst(g, 0);
        mst(head, -1);

        M = 0;

        REP(j, 0, n){
            scanf("%d: (%d)", &s, &m);

            REP(i, 0, m){
                scanf("%d", &t);
                add(s, t, 1);
                //g[s][t] = 1;
            }

        }
        int ans = bfs();

        printf("%d\n", n - ans/2);

    }
    return 0;
}


你可能感兴趣的:(hdu)