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

Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

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.

 
Input
For each data set:
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.

 
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 
Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
 
Sample Output
No 3
 
题意:A认识B,B认识C但是A不认识C,把ABC分成两个集合,每个集合中的人都认识,不能分成输出No,能的话然后从两个集合中选两个认识的组一个房间也就是在两个集合中求最大匹配,用到了种类并查集
 #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(并查集判断二分图+匹配)的更多相关文章

  1. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  2. 线段树、最短路径、最小生成树、并查集、二分图匹配、最近公共祖先--C++模板

    线段树(区间修改,区间和): #include <cstdio> #include <iostream> #include <cstring> using name ...

  3. hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)

     题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...

  4. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  5. HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)

    题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...

  6. P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)

    P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...

  7. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  8. hdu1558--并查集+判断线段相交

    简单的计算几何题,判断两线段是否相交.将相交的两线段使用并查集归到一类中.查询时输出线段对应集合中元素的个数. #include<stdio.h> struct Point{ double ...

  9. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

随机推荐

  1. Kakfa分布式集群搭建

    本位以最新版本kafka_2.11-0.10.1.0版本讲述分布式kafka集群环境的搭建过程.服务器列表: 172.31.10.1 172.31.10.2 172.31.10.3 1.下载kafka ...

  2. python数字图像处理(7):图像的形变与缩放

    图像的形变与缩放,使用的是skimage的transform模块,函数比较多,功能齐全. 1.改变图片尺寸resize 函数格式为: skimage.transform.resize(image, o ...

  3. [CareerCup] 6.1 Find Heavy Bottle 寻找重瓶子

    6.1 You have 20 bottles of pills. 19 bottles have 1.0 gram pills, but one has pills of weight 1.1 gr ...

  4. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  5. UIView的layoutSubviews和drawRect方法何时调用 ———转

    转自:http://jianyu996.blog.163.com/blog/static/112114555201305113018814/ 首先两个方法都是异步执行.layoutSubviews方便 ...

  6. IOS开发之—— iOS 支付 [支付宝、银联、微信]

    支付宝iOSsdk官方下载sdk地址:https://b.alipay.com/order/productDetail.htm?productId=2013080604609654&tabId ...

  7. GCC:条件判断中赋值语句和函数结尾时无返回值的警告

    有下面非常经典的一个字符串复制程序. test1.c #include <stdio.h> int main() { char str_t[]="This String come ...

  8. 【转】十分详细的xStream解析

    转自博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html xStream框架 xStream可以轻易的将Java对象和xml文 ...

  9. JavaScript split() 方法

    split() 方法用于把一个字符串分割成字符串数组.

  10. Web fonts

    不同类型的文件字体: Embedded OpenType (glyphicons-halflings-regular.eot) Scalable Vector Graphics (glyphicons ...