HDU - 1845 Jimmy’s Assignment (二分匹配)
Description
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
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
Sample Input
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
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 (二分匹配)的更多相关文章
- HDU 1845 Jimmy’s Assignment(二分匹配)
Jimmy’s Assignment Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Other ...
- HDU 2063 过山车(二分匹配入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- hdu 4619 Warm up 2 (二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不 ...
- HDU 2063 过山车 二分匹配
解题报告:有m个女生和n个男生要结成伴坐过山车,每个女生都有几个自己想选择的男生,然后要你确定最多能组成多少对组合. 最裸的一个二分匹配,这是我第一次写二分匹配,给我最大的感受就是看那些人讲的匈牙利算 ...
- hdu 1528 Card Game Cheater (二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1068 Girls and Boys (二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU - 1068 Girls and Boys(二分匹配---最大独立集)
题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...
- hdu 1150 Machine Schedule (经典二分匹配)
//A组n人 B组m人 //最多有多少人匹配 每人仅仅有匹配一次 # include<stdio.h> # include<string.h> # include<alg ...
随机推荐
- artTemplate字符串模板
1.官网:http://aui.github.io/art-template/
- 小程序获取Unionid
小程序获取用户Unionid,必须授权获取密文.但授权成功后不是永久的.除非关注了公众号或者App微信绑定了, 解决办法是通过code获取openid,然后用openid去数据库查对应的Unionid ...
- Html !!!了解一下
1.什么是超文本 超文本:就是指页面内可以包含图片,链接,甚至音乐,程序等非文字元素; 最常用的超文本格式:超文本标记语言(标记通用语言的下个一个应用)和富文本格式 超文本语言的特点: 简易性:超文本 ...
- java后台转json、转对象、转list集合
前台数据传递到后台转json 1.普通格式转换成对象 String data=request.getParameter("data"); //单数据的时候转换方式 JSONObje ...
- vue simple框架打包遇到报错问题
问题描述:之前一个项目使用vue-simple框架 打包时老是报错 :原因是es6转化es5时候有错误: 解决: alias: { 'vue$': 'vue/dist/vue.esm.js', // ...
- 64位 Qt5.12 MySql 连接问题
关于怎么检查Qt是否带MySql驱动 ,到Qt安装目录下 plugins\sqldrivers下寻找是否有qsqlmysql.dll文件 例如:F:\Qt\Qt5.9.6\5.9.6\msv ...
- C#读取word文档中的内容
原文地址 http://blog.csdn.net/yhrun/article/details/7674540 在使用前需要添加引用巨硬的com组件:Microsoft Word 12.0 objec ...
- 记一次全局分区索引update调优
原始SQL: CREATE OR REPLACE PROCEDURE sp_upd_suppressed_emails( A_LIMIT_BULK IN PLS_INTEGER DEFAULT 20 ...
- Zabbix通过SNMP监控HP Gen10服务器的硬件
http://www.zmzblog.com/monitor/zabbix-how-to-monitoring-hp-gen10-server-hardware.html
- ES6中let和var的区别
通过var定义的变量,作用域是整个封闭函数,是全域的 .通过let定义的变量,作用域是在块级或是子块中. 变量提升现象:浏览器在运行代码之前会进行预解析 -不论var声明的变量处于当前作用域的第几行, ...