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

还有没完全证明过我的方法的正确性,但是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. localStorage请使用getItem 和setITem

    最近看别人的代码,发现他们在从localStorage里面的时候喜欢用dot来操作,而不是get setItem,记得以前说过这个事.下面再说一次吧. 用dot方式来操作(   每次以'hello'= ...

  2. 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. ThinkPad 禁用 触摸板

    执行 xinput wowk@wowk:~$ xinput ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTES ...

  4. Kmeans聚类算法

    K-means也是聚类算法中最简单的一种了,但是里面包含的思想却是不一般.最早我使用并实现这个算法是在学习韩爷爷那本数据挖掘的书中,那本书比较注重应用.看了Andrew Ng的这个讲义后才有些明白K- ...

  5. github 上传项目基本步骤

    说来也惭愧,我是最近开始用github,小白一个,昨天研究了一个下午.终于可以上传了,所以今天写点,一来分享是自己的一些经验,二来也是做个记录,万一哪天又不记得了:) 废话不多说,直接来,这次主要介绍 ...

  6. linux服务器出现严重故障后的原因以及解决方法

    1.把系统安装光盘插入,重启机器,启动时迅速按下Del键,进入CMOS,把启动顺序改为光盘先启动,这样就启动了Linux安装程序,按F5,按提示打入Linux rescue回车,进入救援模式,接下来是 ...

  7. cp 覆盖 \cp a test\a

    使用cp命令覆盖文件总是提示要输入yes或no,一个两个就算了,大量的文件复制就不行了,即使加上-f参数也无法强行覆盖.苦思冥想不得解,终于在查阅了众多资料后让我找到了解决方法,这里写出来,让有同样困 ...

  8. 修改index.php 清空mylog1.txt

    进入编辑php文件vim index.php(无则新建) -->进入命令行模式--输入a(append)-->进入编辑模式-->编辑好-->esc退出编辑模式-->:q! ...

  9. KMP算法类习题——字符串匹配

    Description For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p- ...

  10. 【记录】ACM计划

    ACM进阶计划ACM队不是为了一场比赛而存在的,为的是队员的整体提高.大学期间,ACM队队员必须要学好的课程有:lC/C++两种语言l高等数学l线性代数l数据结构l离散数学l数据库原理l操作系统原理l ...