有n个关系,他们之间某些人相互认识。这样的人有m对。
你需要把人分成2组,使得每组人内部之间是相互不认识的。
如果可以,就可以安排他们住宿了。安排住宿时,住在一个房间的两个人应该相互认识。
最多的能有多少个房间住宿的两个相互认识。

先是要判断是否为二部图,然后求最大匹配。

学习一下模板~

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
vector<int> g[maxn];
int linker[maxn];
bool used[maxn];
int uN;
int matchs[maxn];
int cnt[maxn];
bool dfs (int u) {
int i;
for (i=;i<g[u].size();i++) {
int v=g[u][i];
if (!used[v]) {
used[v]=true;
if (linker[v]==-||dfs(linker[v])) {
linker[v]=u;
return true;
}
}
}
return false;
}
int hungary () {
int res=;
int u;
memset(linker,-,sizeof(linker));
for (u=;u<=uN;u++) {
memset(used,false,sizeof(used));
if (dfs(u)) res++;
}
return res;
}
bool judge (int x,int y) {
int i;
for (i=;i<g[x].size();i++) {
if (cnt[g[x][i]]==) {
cnt[g[x][i]]=-y;
matchs[g[x][i]]=true;
if (!judge(g[x][i],-y)) return false;
}
else if (cnt[g[x][i]]==y) return false;
}
return true;
}
bool matched () {
int i;
memset(matchs,false,sizeof(matchs));
for (i=;i<=uN;i++) {
if (g[i].size()&&!matchs[i]) {
memset(cnt,,sizeof(cnt));
cnt[i]=-;
matchs[i]=true;
if (!judge(i,-)) return false;
}
}
return true;
}
int main () {
int m;
int i;
int u,v;
while (~scanf("%d %d",&uN,&m)) {
for (i=;i<=uN;i++)
if (g[i].size()) g[i].clear();
while (m--) {
scanf ("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
if (matched()) printf ("%d\n",hungary()/);
else printf ("No\n");
}
return ;
}

HDU2444 The Accomodation of Students(二分图最大匹配)的更多相关文章

  1. HDU2444 The Accomodation of Students —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...

  2. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

  3. HDU2444 :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 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  5. HDU2444 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 (二分图存在的判定以及最大匹配数)

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

  7. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  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. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

随机推荐

  1. Windows常用批处理命令 CMD BAT (CMD目录操作等dir)

    Windows常用批处理 CMD BAT CMD获取文件夹下所有文件的名称并打印(输出)到指定目录 dir C:\Users\\Desktop\png\*.*/s >c:\.txt //cmd ...

  2. LED Holiday Light -Picking LED Christmas Lights, 4 Things

    LED Christmas lights are not very cheap, but you should know that LED lights can be used for more th ...

  3. python3练习100题——025

    原题链接:http://www.runoob.com/python/python-exercise-example25.html 题目:求1+2!+3!+...+20!的和. 我的代码: s =[] ...

  4. MANIFEST.MF详解及配置的注意事项

    一.详解 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介 ...

  5. 微信h5游戏如何在微信中做好域名防封 防屏蔽的 工作

    最近微信开始大封杀,不知道原因是什么,可能是因为违规网站太多了吧,很多网站都被错杀了,下面我们聊一下怎样才能避免域名被封杀呢. 在各种不同的域名当中,能够做出了更适合的选择,这些对于大家域名防封_域名 ...

  6. day11 ss命令 查看网络端口使用情况及对应pid centos7

    [root@oldboyedu ~]# ss -lntup Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNC ...

  7. layui-form下隐藏元素的验证问题

    1.情景: 设置为display:none的必填字段,在点击提交(form 包含 class="layui-form":button包含属性 lay-submit)的时候,依旧验证 ...

  8. Rumor

    Vova promised himself that he would never play computer games... But recently Firestorm — a well-kno ...

  9. Led Night Light Factory: Traveler Led Night Light

    Wake up in a strange hotel room in the evening and find the way to the bathroom, without stepping on ...

  10. Bootstrap入门(2)表格

    Bootstrap入门(四)表格 <table>标签 首先,引入bootstrap的css文件,然后表格内容放在一个class为table的<table>标签中(class=& ...