首页 > 代码库 > poj1274_二分匹配_静态邻接表

poj1274_二分匹配_静态邻接表

贴一下板子。。。
 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7  8 #define MAX 202 9 bool flag, visit[MAX];10 int match[MAX];11 int cow, stall;12 int head[MAX];13 14 struct edge {15     int to, next;16 } e[MAX*MAX];17 int index_;18 19 void addedge(int u, int v) {20     e[index_].to = v;21     e[index_].next = head[u];22     head[u] = index_;23     index_++;24 }25 26 bool dfs(int u) {27     int v;28     for(int i=head[u]; i!=0; i=e[i].next) {29         v = e[i].to;30         if(!visit[v]) {31             visit[v] = true;32             if(match[v]==-1 || dfs(match[v])) {33                 match[v] = u;34                 return true;35             }36         }37     }38     return false;39 }40 41 int MaxMatch() {42     int sum = 0;43     memset(match, -1, sizeof(match));44     for(int i=1; i<=cow; i++) {45         memset(visit, false, sizeof(visit));46         if(dfs(i)) sum++;47     }48     return sum;49 }50 int main() {51     freopen("test.txt", "r", stdin);52     int k, ans, m;53     while(scanf("%d %d", &cow, &stall) != EOF) {54         memset(head, 0, sizeof(head));55         index_ = 1;56         for(int i=1; i<=cow; i++) {57             scanf("%d", &k);58             for(int j=0; j<k; j++) {59                 scanf("%d", &m);60                 addedge(i, m);61             }62         }63         ans = MaxMatch();64         printf("%d\n", ans);65     }66     return 0;67 }
View Code

 

poj1274_二分匹配_静态邻接表