HDU - 1845 Jimmy’s Assignment (二分匹配)

Description

Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore,
the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected). A matching is a subset of the graph’s edges, such that no two edges in the subset have a common vertex. A maximum matching is a matching having
the maximum cardinality.

Given a series of instances of the special graph mentioned above, find the cardinality of a maximum matching for each instance.

Input

The first line of input contains an integer number T, representing the number of graph descriptions to follow. Each description contains on the first line an even integer number N (4<=N<=5000), representing the number of vertices.
Each of the next 3*N/2 lines contains two integers A and B, separated by one blank, denoting that there is an edge between vertex A and vertex B. The vertices are numbered from 1 to N. No edge may appear twice in the input.

Output

For each of the T graphs, in the order given in the input, print one line containing the cardinality of a maximum matching.

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

Politehnica University of Bucharest Local Team Contest 2007

题意:给你双向边,求最多留下多少条边使得每条边都没有共有顶点

思路:二分图匹配的定义,对于双向的要/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 (二分匹配)

时间: 2024-10-08 17:20:48

HDU - 1845 Jimmy’s Assignment (二分匹配)的相关文章

hdu 1845 Jimmy’s Assignment (二分图)

Jimmy's Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 896    Accepted Submission(s): 379 Problem Description Jimmy is studying Advanced Graph Algorithms at his university. His most

hdu 2768 Cat vs. Dog (二分匹配)

Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1422    Accepted Submission(s): 534 Problem Description The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunch

hdu1845 Jimmy’s Assignment --- 完备匹配

题意: 要求在一个特殊的图上找最大匹配,该图特点是:无向图,每个节点度数为3,是一个边双连通分量(the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected) 这一点是这样理解的把..) 思路: 一般想法就直接建图求最大匹配,点的范围是5000,不优化可能超时,下面代码是890ms过的. 另一种思路: 完备匹配的条件: 1.

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

HDU - 1045 Fire Net(二分匹配)

Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to s

hdu 4619 Warm up 2 (二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不会相互覆盖.水平放置的牌和竖直放置的牌可能相互覆盖,现在要移去一些牌,使得剩下的牌任何两张都不会相互覆盖,问桌面上最多能剩多少张牌. 分析: 如果把每张牌看作一个结点,则共有两类结点,容易联想到二分图.另外,同方向的牌不会相互覆盖,不同方向的可能相互覆盖,易想到二分图的一个重要性质:同类结点间不会连

HDU 2063 过山车(二分匹配入门)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.net/dark_scope/article/details/8880547 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using nam

HDU 2063 过山车 (二分匹配之匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12475    Accepted Submission(s): 5446 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做pa

hdu 1150 Machine Schedule (经典二分匹配)

//A组n人 B组m人 //最多有多少人匹配 每人仅仅有匹配一次 # include<stdio.h> # include<string.h> # include<algorithm> using namespace std; int n,m,k; int pp[1100][1100],map[1100],vis[1100]; int bfs(int x)//二分匹配模板 { for(int i=1;i<=m;i++)//B组中的人来迎合匹配 { if(!vis[