HDU——2444 The Accomodation of Students
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7086 Accepted Submission(s): 3167
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.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
题目:一些学生之间是朋友关系(关系不能传递),现在要将一堆学生分成两堆,使得在同一堆的学生之间没有朋友关系。如果不可以输出“No”,可以的话输出最多可以分出几对小盆友。
思路:
我们先二分图染色,若能被染成两部分的话说明可以被分成两部分,然后再在我们分出的图上面跑最大匹配。若不能被染成两部分直接输出no
代码:
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 510
using namespace std;
bool flag,vis[N];
int n,m,x,y,tot,ans,col[N],girl[N],head[N],map[N][N];
queue<int>q;
struct Edge
{
int from,to,next;
}edge[N*N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int find(int x)
{
;i<=n;i++)
{
if(!vis[i]&&map[x][i])
{
vis[i]=true;
||find(girl[i])){girl[i]=x; ;}
}
}
;
}
int color(int s)
{
queue<int>q;
q.push(s); col[s]=;
while(!q.empty())
{
int x=q.front();
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
){;}}
else
{
col[t]=col[x]^;
q.push(t);
}
}
q.pop();
}
;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=;flag=;
memset(map,,sizeof(map));
memset(col,-,sizeof(col));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
;i<=m;i++)
{
x=read(),y=read();
map[x][y]=;
add(x,y),add(y,x);
}
;i<=n;i++)
)
{
if(color(i)) break;
}
if(flag) {printf("No\n"); continue;}
memset(girl,-,sizeof(girl));
;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(find(i)) ans++;
}
printf("%d\n",ans);
}
;
}
HDU——2444 The Accomodation of Students的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- 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 判断二分图+二分匹配
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 (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- hdu 2444 The Accomodation of Students 【二分图匹配】
There are a group of students. Some of them may know each other, while others don't. For example, A ...
随机推荐
- Bézier surface(贝塞尔曲面)
Bézier surface(贝赛尔曲面) 贝塞尔曲面是一种用于计算机图形学.计算机辅助设计和有限元建模的数学样条.与贝塞尔曲线一样,贝塞尔曲面由一组控制点定义.与插值在许多方面相似,一个关键的区别是 ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...
- PHP03 移动互联网和PHP
学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建 移动互联网 定义 移动互联网,就是 ...
- 前段开发 jq ajax数据处理详细讲解。
定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 常用的ajax结构模板: function indes(){ $.ajax({ url: '', type: "GET&qu ...
- SMTP error 554 !!
哇,我真的amazing, incredible!! 我只是想写一个简单的邮件,结果他一直报554错误!!! 期间,通过百度,我发现了可能导致 此,讨厌至极的错误,有N多原因: 但我的原因 谜之离谱! ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...
- luogu P1519 穿越栅栏 Overfencing
题目描述 描述 农夫John在外面的田野上搭建了一个巨大的用栅栏围成的迷宫.幸运的是,他在迷宫的边界上留出了两段栅栏作为迷宫的出口.更幸运的是,他所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意 ...
- Floyd算法简单实现(C++)
图的最短路径问题主要包括三种算法: (1)Dijkstra (没有负权边的单源最短路径) (2)Floyed (多源最短路径) (3)Bellman (含有负权边的单源最短路径) 本文主要讲使用C++ ...
- 12. KEY_COLUMN_USAGE
12. KEY_COLUMN_USAGE KEY_COLUMN_USAGE表描述哪些键列具有约束. KEY_COLUMN_USAGE表有以下列: CONSTRAINT_CATALOG :约束所属目录的 ...
- memcached 经典问题或现象
缓存雪崩现象及真实案例 缓存雪崩一般是由某个缓存节点失效,导致其他节点的缓存命中率下降, 缓存中缺失的数据 去数据库查询.短时间内,造成数据库服务器崩溃. 重启 DB,短期又被压跨,但缓存数据也多一些 ...