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. mobx使用

    1.mobx状态管理 安装:creact-react-app mobx action.store.reducer. action是一个函数,事件调用actions直接修改state,Actions是唯 ...

  2. SQL 以逗号分隔查询;调用自定义函数

    select col from [dbo].[GetInPara]('101,102,103',',') USE [xxx] GO /****** Object: UserDefinedFunctio ...

  3. luoguP1025+codevs 1039 数的划分 x

    luoguP1025 + codevs1039 数的划分 2001年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Des ...

  4. 页面禁止刷新处理Js实现

    document.onkeydown = function(e) { e = window.event || e; var k = e.keyCode; //屏蔽ctrl+R,F5键,ctrl+F5键 ...

  5. 【spoj2774】最长公共子串

    题目描述: 给你两个字符串,求它们最长公共子串的长度,如果不存在公共子串则输出0. 样例输入: yeshowmuchiloveyoumydearmotherreallyicannotbelieveit ...

  6. PHP 发邮件《转》

    导读:PHP自带的mail()函数,是php内置发邮件的函数,该函数虽然简单,但是要想真正可以发邮件得有很复杂的配置.不适合新手,以及项目实际的应用的开发. php的mail()函数复杂配置,使得直接 ...

  7. Bug管理工具MantisBT-2.18.0安装教程

    Bug管理工具MantisBT安装教程 MantisBT官网下载地址:https://sourceforge.net/projects/mantisbt/# 写于:2018.12.1 如上传博客资料图 ...

  8. 大数据笔记(二十三)——Scala语言基础

    一.Scala简介:一种多范式的编程语言 (*)面向对象 (*)函数式编程:Scala的最大特点 (*)基于JVM 二.Scala的运行环境 (1)命令行:REPL 进入: scala 退出::qui ...

  9. CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin,mysql8.0.1/mysql5.7.22+centos7,windows mysql安装、配置

    介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...

  10. system系统调用返回值判断命令是否执行成功

    system函数对返回值的处理,涉及3个阶段: 阶段1:创建子进程等准备工作.如果失败,返回-1. 阶段2:调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1) ...