hdu 1068 Girls and Boys(水題,二分圖匹配)
阿新 • • 發佈:2019-01-01
小記:這題看是10s,其實很水,就是點多,但邊少。所以用鄰接表會快很多。
思路:直接套二分圖匹配的模板就可以過,如果想要速度快,改成鄰接表即可,
這裡我是使用的前向星,171ms。
程式碼:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <string> 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; }