HD2444The Accomodation of Students(并查集判断二分图+匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4091 Accepted Submission(s): 1876
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
const int MAX = ;
int father[MAX],a[MAX];
int g[MAX][MAX],vis[MAX],link[MAX];
int n,m;
int find_father(int x)
{
if(x == father[x])
return x;
int t = find_father(father[x]);
a[x] = (a[father[x]] + a[x]) % ;
return father[x] = t;
}
int dfs(int x)
{
for(int i = ; i <= n; i++)
{
if(vis[i] == && g[x][i])
{
vis[i] = ;
if(link[i] == || dfs(link[i]))
{
link[i] = x;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = ; i <= n; i++)
{
father[i] = i;
a[i] = ;
}
memset(g,,sizeof(g));
memset(link,,sizeof(link));
int flag = ,ans = ;
while(m--)
{
int x,y,fx,fy;
scanf("%d%d",&x,&y);
g[x][y] = g[y][x] = ;
fx = find_father(x);
fy = find_father(y);
if(fx != fy)
{
father[fy] = fx;
if(a[x] == )
{
a[fy] = - a[y];
}
else
a[fy] = a[y];
}
else
{
if(a[x] == a[y])
{
flag = ;
}
}
}
if(flag)
{
printf("No\n");
}
else
{
for(int i = ; i <= n; i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",ans/);
}
} return ;
}
HD2444The Accomodation of Students(并查集判断二分图+匹配)的更多相关文章
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- 线段树、最短路径、最小生成树、并查集、二分图匹配、最近公共祖先--C++模板
线段树(区间修改,区间和): #include <cstdio> #include <iostream> #include <cstring> using name ...
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质
小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)
P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu1558--并查集+判断线段相交
简单的计算几何题,判断两线段是否相交.将相交的两线段使用并查集归到一类中.查询时输出线段对应集合中元素的个数. #include<stdio.h> struct Point{ double ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
随机推荐
- 课题:如何培养自己的SEO资源
课题:如何培养自己的SEO资源 问:做SEO最重要的是什么?[针对性的流量]答:看获取一样东西的门槛.稀缺性,人人可得价值不大外链 内容 流量[正确]针对性的自然流量是用户自愿带来的,价值高,能形成购 ...
- 变态跳台阶-一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution { public: int jumpFloorII(int number) { ) ; ) ; *jumpFloorII(number-); } };
- Chrome 消息传递机制
Chrome插件开发入门(二)——消息传递机制 Blog | Qiushi Chen 2014-03-31 9538 阅读 Chrome 插件 由于插件的js运行环境有区别,所以消息传递机制是一个重要 ...
- Matlab中的数据类型
Matlab中有15种基本数据类型,主要是整型.浮点.逻辑.字符.日期和时间.结构数组.单元格数组以及函数句柄等. 1.整型:(int8:uint8:int16:uint16:int3 ...
- [CareerCup] 1.4 Replace Spaces 替换空格
1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has ...
- app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...
- FPGA学习之基本结构
如何学习FPGA中提到第一步:学习.了解FPGA结构,FPGA到底是什么东西,芯片里面有什么,不要开始就拿个开发板照着别人的东西去编程.既然要开始学习FPGA,那么就应该从其基本结构开始.以下内容是我 ...
- 『方案』《女友十年精华》 ORC 图片 文字识别 详解
目的需求: 2008年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...
- xcode更新,想想也是醉了
每次更新,都要整个文件全部更新,这下载速度,想想也是醉了,苹果你就不能搞了更新包吗!!
- 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...