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 ...
随机推荐
- 微信点餐系统(七)-微信授权获取openid:
章节小结: 1.学会了微信授权的步骤,学会了微信授权的文档 2.学会了使用natapp内网穿透工具 3.加深了虚拟机的网络配置以及基本使用 4.学会了抓包购票工具fiddler的使用 5.微信授权步骤 ...
- 【CodeChef】LECOINS(同余最短路,背包DP)
题意:给定n个物品,每个物品可以取无限次,每个物品有两种属性:价值v和颜色c 现在有q个询问,每次询问是否能取出价值和为S的方案,如有多解输出不同颜色种数的最大值 题意:看到BZOJ评论区有好心人说C ...
- JMS学习十(ActiveMQ支持的传输协议)
ActiveMQ提供了一种连接机制,这种连接机制使用传输连接器(TransportConnector)实现客户端与代理(client - to - broker)之间的通信. 网络连接器(networ ...
- sqli-labs(28a)
0X01构造闭合 爆字段数 /?id=') order by 1%23 ?id=') order by 5%23 偷看一下源码 就只过滤了union select 闭合') 那我们来尝试一下 0X02 ...
- OI常用的常数优化小技巧
注意:本文所介绍的优化并不是算法上的优化,那个就非常复杂了,不同题目有不同的优化.笔者要说的只是一些实用的常数优化小技巧,很简单,虽然效果可能不那么明显,但在对时间复杂度要求十分苛刻的时候,这些小的优 ...
- [CSP-S模拟测试]:表达式密码(模拟)
题目传送门(内部题87) 输入格式 从文件$expression.in$中读入数据.输入一行,一个字符串$S$,表示原表达式,保证为合法表达式 输出格式 输出到文件$expression.out$中. ...
- 运行Spark官方提供的例子
去spark官网把spark下载下来: https://spark.apache.org/downloads.html 解压,可以看下目录: 其中examples目录下提供了java,scala,py ...
- Mysql中经常出现的乱码问题
Mysql中执行SET NAMES utf8这条SQl的作用 1)首先,Mysql服务器的编码和数据库的编码在配置文件my.ini中设置: 用记事本打开配置文件,修改代码:default-charac ...
- (转)WebRTC信令控制与STUN/TURN服务器搭建
转:https://rtcdeveloper.com/t/topic/13742 本文将向大家介绍两个方面的知识: WebRTC信令控制 STUN/TURN服务器的搭建 在前面的文章中已经向大家介绍了 ...
- Python的 counter内置函数,统计文本中的单词数量
counter是 colletions内的一个类 可以理解为一个简单的计数 import collections str1=['a','a','b','d'] m=collections.Counte ...