HDU 2444 The Accomodation of Students
首先是要构造二分图,然后二分图的最大匹配。
还有没完全证明过我的方法的正确性,但是AC了.....
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=*+;
const int Maxn=+;
int N,M;
int U[maxn],V[maxn];
int F[Maxn];
vector<int>G[Maxn];
int St;
int dis[Maxn],flag[Maxn];
queue<int>Q;
int Belong[Maxn]; int nx,ny;
int g[Maxn][Maxn];
int cx[Maxn],cy[Maxn];
int mk[Maxn]; void init()
{
for(int i=; i<=N; i++) G[i].clear();
memset(F,,sizeof F);
memset(Belong,,sizeof Belong);
for(int i=; i<=N; i++) dis[i]=INF;
nx=N,ny=N;
memset(g,,sizeof(g));
} void SPFA()
{
while(!Q.empty()) Q.pop();
memset(flag,,sizeof flag);
flag[St]=;
dis[St]=;
Q.push(St);
while(!Q.empty())
{
int h=Q.front();
Q.pop();
flag[h]=;
for(int i=; i<G[h].size(); i++)
{
if(dis[h]+<dis[G[h][i]])
{
dis[G[h][i]]=dis[h]+;
if(!flag[G[h][i]])
{
flag[G[h][i]]=;
Q.push(G[h][i]);
}
}
}
}
} int path(int u)
{
for(int v=; v<=ny; v++)
{
if(g[u][v]&&!mk[v])
{
mk[v]=;
if(cy[v]==-||path(cy[v]))
{
cx[u]=v;
cy[v]=u;
return ;
}
}
}
return ;
} int MaxMatch()
{
int res=;
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
for(int i=; i<=nx; i++)
{
if(cx[i]==-)
{
memset(mk,,sizeof(mk));
res=res+path(i);
}
}
return res;
} int main()
{
while(~scanf("%d%d",&N,&M))
{
init();
for(int i=; i<=M; i++) scanf("%d%d",&U[i],&V[i]);
for(int i=; i<=M; i++)
{
G[U[i]].push_back(V[i]);
G[V[i]].push_back(U[i]);
}
for(int i=; i<=N; i++)
if(dis[i]==INF) St=i,SPFA();
for(int i=; i<=N; i++)
{
if(dis[i]%==) Belong[i]=;
else Belong[i]=;
}
int Fail=;
for(int i=; i<=M; i++)
if(Belong[U[i]]==Belong[V[i]])
{
Fail=;
break;
}
if(Fail==) printf("No\n");
else
{
for(int i=; i<=M; i++)
{
if(Belong[U[i]]==&&Belong[V[i]]==)
g[U[i]][V[i]]=;
if(Belong[V[i]]==&&Belong[U[i]]==)
g[V[i]][U[i]]=;
}
printf("%d\n",MaxMatch());
}
}
return ;
}
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 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 ...
随机推荐
- javascript焦点图之垂直滚动
html代码布局,需要用到定位,不细说了 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 杭电20题 Human Gene Functions
Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...
- #define const extern
将父类中的常量放到. m文件,子类就不会重复包含了.之后再.h文件中用extern NSSting * const ILScoreShowStartTime;// extern 用来声明变量和函数.c ...
- python脚本文件删除
昨天有需求需要用python脚本删除一个目录下的文件.遇到了点麻烦. 使用的是shutil.rmtree(dir)函数,这个函数可以删除有内容的目录,而shutil.rmdir(dir)只能删除空目录 ...
- input时间输入框小解
date 年月日 month 年月 week 年周(ios上不可用,android部分会显示当前是第几周) time 时分(ios不显示时/分的字样) datetime 为一输入框,基本 ...
- Hibernate的查询,二级缓存,连接池
Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...
- oracle账户锁定解决方法
今天进使用orcle中,发现系统中,system账户登录里提示账户被锁定 ,后来查了查资料,问题解决,方法如下: Microsoft Windows [版本 5.2.3790] (C) 版权所有 19 ...
- LINUX修改IP地址
以前都是使用自动IP动态分配获取IP的,虽然每次获得的ip都是相同的,但我还是决定自己设置一个IP.输入命令:[root@localhost ~]# ifconfig eth0 219.246.177 ...
- MFC单选按钮
先为对话框加上2个radio button,分别是Radio1和Radio2. 问题1:如何让Radio1或者Radio2默认选上?如何知道哪个被选上了? 关键是选上,“默认”只要放在OnInitDi ...
- OpenCV成长之路:图像直方图
http://ronny.blog.51cto.com/8801997/1394115 2014-04-11 13:47:27 标签:opencv 直方图 统计表 原创作品,允许转载,转载时请务必以超 ...