Description
Input
Output
Sample Input
2 4 1 2 1 3 1 4 2 3 2 4 3 4 4 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output
2 2
Source
题意:给你双向边,求最多留下多少条边使得每条边都没有共有顶点
思路:二分图匹配的定义,对于双向的要/2,用vector会超时,要用邻接表
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int MAXN = 5010; const int MAXM = 50010; struct Edge { int to, next; } edge[MAXM]; int head[MAXN], tot; int linker[MAXN]; bool used[MAXN]; int n, m; void init() { tot = 0; memset(head,-1,sizeof(head)); } void addEdge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } bool dfs(int u) { for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (!used[v]) { used[v] = true; if (linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } } return false; } int solve() { int ans = 0; memset(linker, -1, sizeof(linker)); for (int i = 0; i < n; i++) { memset(used, false, sizeof(used)); if (dfs(i)) ans++; } return ans; } int main() { int t; scanf("%d",&t); while (t--) { scanf("%d", &n); m = n*3/2; int u,v; init(); while (m--) { scanf("%d%d", &u, &v); u--; v--; addEdge(u,v); addEdge(v,u); } printf("%d\n", solve()/2); } return 0; }
HDU - 1845 Jimmy’s Assignment (二分匹配),布布扣,bubuko.com
HDU - 1845 Jimmy’s Assignment (二分匹配)
原文地址:http://blog.csdn.net/u011345136/article/details/37874377