首先是要构造二分图,然后二分图的最大匹配。

还有没完全证明过我的方法的正确性,但是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的更多相关文章

  1. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成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 ...

  3. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  5. HDU——2444 The Accomodation of Students

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  9. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  10. 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 ...

随机推荐

  1. SharePoint 2013 列表启用搜索

    转载自:http://www.cnblogs.com/jianyus/p/3470117.html SharePoint 2013列表搜索的设置,只是进行完全爬网,就可以使用.如果开启爬网不是很熟练可 ...

  2. UGUI学习笔记

    基本情况:熟悉NGUI  没接触过UGUI 目标:熟练掌握UGUI,并用在实际项目中 一 在网上寻找视频教程,快速了解UGUI http://www.taikr.com/course/89 不错的视频 ...

  3. 二十一、oracle pl/sql分类一 存储过程

    存储过程用于执行特定的操作,当建立存储过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数,可以将执行部分的数据传递到 ...

  4. 二、oracle sql*plus常用命令

    一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户 ...

  5. Windows Access Token

    security descriptor A structure and associated data that contains the security information for a sec ...

  6. ShellExecute, WinExec, CreateProcess区别

    ShellExecute ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. 有几个API函数都可以实现这些功能 ...

  7. hdu_3562_B-number(记忆化搜索|数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:给你一个n,为比n小的能整除13并数字中有13的数有多少个 题解:记忆化搜索:记dp[i] ...

  8. NoSQL数据库种类

    NoSQL数据库的四大分类   键值(Key-Value)存储数据库   这一类数据库主要会使用到一个哈希表,这个表中有一个特定的键和一个指针指向特定的数据.Key/value模型对于IT系统来说的优 ...

  9. Canvas 数学、物理、动画学习笔记一

    Canvas 第五章 数学.物理和运动学习笔记让人映像深刻的运动,需要我们不只是简单的知道如何移动对象,还需要知道怎么按用户期望看到的方式去移动它们.这些需要基于数学知识的基本算法和物理学作用.基于点 ...

  10. 关于C++中虚函数表存放位置的思考

    其实这是我前一段时间思考过的一个问题,是在看<深入探索C++对象模型>这本书的时候我产生的一个疑问,最近在网上又看到类似的帖子,贴出来看看: 我看到了很多有意思的答案,都回答的比较好,下面 ...