题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1845
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
就是这个智障题,卡了我这个智障10多分钟。一直tle。
劝告大家,求最大匹配数的时候,dfs函数和vis数组一定要用bool 型!!!!!bool型!!bool 型
以前知道Bool型比Int快,这道题才知道,快100多ms,用int的0 1表示超市,用bool表示862ms。
也是给自己一个劝告,学到了。
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<string.h> #include<set> #include<vector> #include<queue> using namespace std; const int maxn = 7000; const int inf = 0x3f3f3f3f; typedef long long ll; struct node { int x, y, next; }edge[maxn*3]; int head[maxn], vis[maxn], linker[maxn]; int t, n, m, tot; void addedge(int u, int v) { edge[tot].x = u; edge[tot].y = v; edge[tot].next = head[u]; head[u] = tot++; edge[tot].x = v; edge[tot].y = u; edge[tot].next = head[v]; head[v] = tot++; return; } int dfs(int x) { for (int i = head[x]; i != -1; i = edge[i].next) { int v = edge[i].y; if (!vis[v]) { vis[v] = 1; if (linker[v] == -1 || dfs(linker[v])) { linker[v] = x; return 1; } } } return 0; } int hun() { int ans = 0; memset(linker, -1, sizeof(linker)); for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); ans += dfs(i); } return ans; } int main() { freopen("C://input.txt", "r", stdin); scanf("%d", &t); while (t--) { tot = 0; scanf("%d", &n); m = n * 3 / 2; memset(head, -1, sizeof(head)); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); } printf("%d\n", hun() / 2); } return 0; }