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 ...
随机推荐
- ECOS-Ecstore 后台管理地址修改
ECStore默认出厂的后台管理地址是: http://域名/index.php/shopadmin http://域名/shopadmin [配置过rewrite后,并开启伪静态] 如果想要更个性的 ...
- Xcode调试之查看变量
从其他开发语言转行进军IOS开发的小伙伴可能会有这样一件苦恼的事情,调试程序时如何查看变量值?我并不喜欢每次都要通过打印去查看变量的值,也不喜欢通过光标悬浮到变量上来显示变量的值,如果要查看变量的属性 ...
- Urbanization
Urbanization time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Thrift源码解析--TBinaryProtocol
本文为原创,未经许可禁止转载. 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚.故打算以TBinaryProtocol为例,分析客户端发请求以及接收服务端返回数据的整个过 ...
- iosOC可变数组选择,冒泡排序
#pragma mark 可变数组的排序 NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@&quo ...
- 图像相似度计算之哈希值方法OpenCV实现
http://blog.csdn.net/fengbingchun/article/details/42153261 图像相似度计算之哈希值方法OpenCV实现 2014-12-25 21:27 29 ...
- nefu 753 n!末尾有多少个0
Problem : 753 Time Limit : 1000ms Memory Limit : 65536K description 计算N!末尾有多少个0 input 输入数据有多组,每组1行,每 ...
- Ubuntu 下安装 nfs
1先安装sudo apt-get install nfs-kernel-server(安装nfs-kernel-server时,apt会自动安装nfs-common和portmap) 分开看就是1.服 ...
- SQL SERVER与C#的数据类型对应表
序号 类别 SQLServer C Sharp 备注 1 整数 bit Boolean True转换为1False转换为0 2 tinyint Byte C Sharp 数据类型都位于System命名 ...
- mongodb导出数据
导出 -d 数据库名 -u 用户名 -p 密码 -c 要导出的表名 -o 要到出的文件地址及类型 C:\Users\Administrator>mongoexport -d fh -u we ...