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 ...
随机推荐
- 八、Foundation -常用结构体
一.NSRange 在foundation/NSRange.h中对NSRange的定义 typedef struct _NSRange{ NSUInteger location; NSUInteger ...
- C#定制并发送HTML邮件
HTML格式的邮件能够使用所有html/css使得邮件更丰富,比如现在很多newsletter 都是使用的html邮件. 今天试了一下,如何把图片嵌入到html中呢? 方法一,你的图片host到了in ...
- CENTOS7 添加自定义快捷键(启动TERMINAL,显示桌面等)
centos7中不自带启动终端的快捷键,可以自定义添加. 点击右上角的用户名,点击设置,在设置面板点击键盘,选择快捷键选项卡进入快捷键设置界面. 点击“+”添加快捷键.名称自定义(如"启动终 ...
- Java开发环境的搭建
确定自己的操作系统版本并下载安装JDK 1.下载JDK windows系统: 右键我的电脑->属性;如下图: 2.下载JDK 下载地址:http://www.oracle.com/index.h ...
- Resource interpreted as Script but transferred with MIME type text/plain:
我用script做ajax跨域,请求返回的是个文本字符串,chrome提示:Resource interpreted as Script but transferred with MIME type ...
- 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构(转)
http://www.cnblogs.com/guozili/p/3496265.html 基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通 ...
- valueOf和toString
有一道经典的题目: var add = function() {___}; console.log(add(3)(4)(5)); // 输出60 题目要求能无限相乘,请补充add函数. 首先很显然,a ...
- PHP Yii1.1.13(一):命令行创建应用~shop
第一节 初始目录结构 (1)初识目录结构 在创建应用之前,我们来看一下Yii 1.x版本的目录结构:将yii-1.1.13安装文件解压到网站根目录下,打开framework目录,其目录如下图所示 (2 ...
- redis async client 与自有框架集成
hiredis的异步接口已经支持ae libuv libev 和 libevent集成,具体头文件可以参见redis/deps/hiredis/adapters,样例参见redis/deps/hire ...
- 初探JAVA中I/O流(一)
一.流 流,这里是对数据交换的形象称法.进程是运行在内存中的,在运行的过程中避免不了会与外界进行数据交互.比如将数据从硬盘.控制台.管道甚至是套接字(具体点应该是我们电脑上的网卡)读到我们进程锁所占据 ...