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 (二分匹配)的更多相关文章

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

    Jimmy’s Assignment Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Other ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...

  3. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  4. hdu 4619 Warm up 2 (二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不 ...

  5. HDU 2063 过山车 二分匹配

    解题报告:有m个女生和n个男生要结成伴坐过山车,每个女生都有几个自己想选择的男生,然后要你确定最多能组成多少对组合. 最裸的一个二分匹配,这是我第一次写二分匹配,给我最大的感受就是看那些人讲的匈牙利算 ...

  6. hdu 1528 Card Game Cheater (二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. hdu 1068 Girls and Boys (二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU - 1068 Girls and Boys(二分匹配---最大独立集)

    题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...

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

    //A组n人 B组m人 //最多有多少人匹配 每人仅仅有匹配一次 # include<stdio.h> # include<string.h> # include<alg ...

随机推荐

  1. webpack-第一个demo

    1.webpack概念 webpack是前端的一个项目构建工具,它是基于node.js开发出来的一个前端工具:借助webpack这个前端自动化构建工具,可以完美实现资源的合并.打包.压缩.混淆等诸多功 ...

  2. 组件内导航之beforeRouteUpdate的使用

    使用场景: 组件复用:路由跳转: beforeRouteUpdate (to, from, next) { // 在当前路由改变,但是该组件被复用时调用 // 举例来说,对于一个带有动态参数的路径 / ...

  3. [CSP-S模拟测试]:旅行计划(分块+DP)

    题目传送门(内部题83) 输入格式 第一行两个整数$n,m$ 接下来$m$行,每行三个整数,$u,v,w$,表示从$u$到$v$有一条权值为$w$的边 接下来一行有一个整数$q$,表示$q$天 接下来 ...

  4. android intent调用系统camera

    利用android的camera通常有两种方式:利用intent调用系统的camera,或者结合surfaceview实现自己定制的camera.先分别对这两种方法说明如下: 一.使用系统自配的cam ...

  5. android遮罩阴影对话框的实现

    定义 <style name="MyDialog" parent="@android:Theme.Dialog"> <item name=&q ...

  6. REST framework 之 分页

    DRF分页组件 为什么要使用分页 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输 ...

  7. Jcaptcha组件和kaptcha组件实现验证码

  8. linux配置ssh公钥认证,打通root用户的免密码输入的scp通道

    1.ssh-keygen ssh-keygen是unix-like系统的一个用来生成.管理ssh公钥和私钥的工具. 2.用法 常用的重要的选项有: -b num   指定生成多少比特长度的key,单位 ...

  9. 【flask_sqlalchemy】模糊查询

    flask_sqlalchemy的查询方法有filter()和filter_() 这2个方法的主要区别如下: 模块 语法 ><(大于和小于)查询 and_和or_查询 filter_by( ...

  10. 测开之路一百二十八:flask之重定向和404

    a.b两个视图,分别返回a的页面和b的页面 重定向:redirect 重定向到路由:请求/a/时,重定向到/b/ 重定向到视图函数:url_for(“函数名“),访问/a/时,重定向到函数b() 主动 ...