(匹配)The Accomodation of Students --HDU --2444
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2444
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#problem/B
题意:
有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就算出房间最多需要多少间,否则就输出No。
朋友的朋友不是朋友,r代表他们之间的关系
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 205
#define INF 0x3f3f3f3f struct node {int v, next;}e[N]; int n, m, p[N], used[N], Head[N], nx, bnt, f[N], r[N], G[N][N], x[N]; int FindFather(int x)
{
int k = f[x];
if(f[x]!=x)
{
f[x] = FindFather(f[x]);
r[x] = (r[x] + r[k])%;
}
return f[x];
} int Find(int u)
{
for(int i=; i<=n; i++)
{
if(!used[i] && G[u][i])
{
used[i]=;
if(!p[i] || Find(p[i]))
{
p[i] = u;
return true;
}
}
}
return false;
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
int i, ans=, u, v; bnt = nx = ;
memset(G, , sizeof(G));
memset(p, , sizeof(p));
memset(used, , sizeof(used));
memset(x, , sizeof(x));
for(i=; i<=n; i++)
{
f[i] = i;
r[i] = ;
} for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] =; int ru = FindFather(u);
int rv = FindFather(v); if(ru==rv && r[u]==r[v])
ans = ;
else if(ru!=rv)
{
f[ru] = rv;
r[ru] = (r[u] + r[v] + ) % ;
}
} if(ans)
printf("No\n");
else
{
for(i=; i<=n; i++)
{
u = FindFather(i);
if(r[i]==)
x[nx++] = i;
} int an=;
for(i=; i<nx; i++)
{
memset(used, , sizeof(used));
if(Find(x[i]))
an++;
} printf("%d\n", an);
}
}
return ;
}
(匹配)The Accomodation of Students --HDU --2444的更多相关文章
- The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- B - The Accomodation of Students - hdu 2444(最大匹配)
题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求 ...
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
/*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...
- HDOJ 2444 The Accomodation of Students
染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- session会话时间
session对象失效在下列情况下被删除: A.程序调用HttpSession.invalidate() B.距离上一次收到客户端发送的session id时间间隔超过了session的最大有效时间 ...
- python的读写和写读操作
# 读写操作 (读写操作是正常的)f = open('log',mode='r+',encoding='utf-8') # log是文件名 print(f.read()) f.write(" ...
- sam/bam格式
1)Sam (Sequence Alignment/Map) ------------------------------------------------- 1) SAM 文件产生背景 随着Ill ...
- 52. N-Queens II (Array; Back-Track)
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 判断UNITY版本号
代码示例: #if (UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_5_7 || UNITY_5_8 || UNITY_5_9)u ...
- SSH框架分模块开发
------------------siwuxie095 SSH 框架分模块开发 1.在 Spring 核心配置文件中配置多个内容,容易造成 配置混乱,不利于维护 「分模块开发主要针对 Spring ...
- PAT L3-008 喊山(广搜)
喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的.原来它是彝族先民用 ...
- 【校招面试 之 C/C++】第9题 C++多态
C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来调用相应的函数.如果对象类型是派生类,就调用派生类的函数:如果对象类型是基类 ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- c语言定义函数指针和typedef简写
二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...