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 ...
随机推荐
- StackBlur.js
StackBlur.js 是 Mario Klingemann 创建的一个快速的.接近高斯模糊的效果库. 更多信息: http://incubator.quasimondo.com/processin ...
- 变态跳台阶-一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution { public: int jumpFloorII(int number) { ) ; ) ; *jumpFloorII(number-); } };
- 解决服务器连接错误Host ‘XXX’ is not allowed to connect to this MySQL server
这段时间在研究火车头的入库教程,在“配置登陆信息和数据库(mysql)”连接中,出现“服务器连接错误Host 'XXX' is not allowed to connect to this MySQL ...
- no.5.print sum
#-*-coding=utf-8-*- for a in range(1,50,1): for b in range(1,50,1): for c in range(1,50,1): if a+b+c ...
- 淘宝账号基于OAuth2.0的登录验证授权登陆第三方网站
首先得有一个注册的appkey和App Secret 该流程分三个步骤: 第一步:通过用户授权获取授权码Code: 第二步:用上一步获取的Code和应用密钥(AppSecret)通过Https P ...
- Virtualbox下Ubuntu与主机Win7共享文件夹
记下来,免得老google. 1. 在虚拟机设置里设置好win7的共享文件夹位置:如c:\share 2.确定ubuntu下需要共享的文件夹,如~/linuxshare.注意,此文件夹名字必须与win ...
- JS 之高级函数
作用域安全的构造函数 当使用new调用构造函数时,构造函数内部this对象会指向新创建的对象实例.如果不使用new,直接调用的话,则this对象会映射到window对象上.所以需要判断下. eg: f ...
- UI设计师零基础入门到精通精品视频教程【155课高清完整版】
[福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 ...
- 关于matlab中特殊字符, 上标和下标
'T=25\circC',(摄氏度) 下标用 _{下划线} 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...
- JNDI全面总结(zz)
原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...