The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8418 Accepted Submission(s): 3709
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
先判断能不能分成二分图 , 不是就输出No。。只有1个的时候也不是
然后求完美匹配即可。。。我不会写匈牙利了。。。。只会写hk。。。还是套模板。。。
因为没有分左右 所以要除2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int dx[maxn], dy[maxn], cx[maxn], cy[maxn], used[maxn], vis[maxn];
int nx, ny, dis;
vector<int> G[];
int n, m;
int bfs()
{
queue<int> Q;
dis = INF;
mem(dx, -);
mem(dy, -);
for(int i=; i<=nx; i++)
{
if(cx[i] == -)
{
Q.push(i);
dx[i] = ;
}
}
while(!Q.empty())
{
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int v=; v<G[u].size(); v++)
{
int i = G[u][v];
if(dy[i] == -)
{
dy[i] = dx[u] + ;
if(cy[i] == -) dis = cy[i];
else
{
dx[cy[i]] = dy[i] + ;
Q.push(cy[i]);
}
}
}
}
return dis != INF;
} int dfs(int u)
{
for(int v=; v<G[u].size(); v++)
{
int i=G[u][v];
if(!used[i] && dy[i] == dx[u] + )
{
used[i] = ;
if(cy[i] != - && dis == dy[i]) continue;
if(cy[i] == - || dfs(cy[i]))
{
cy[i] = u;
cx[u] = i;
return ;
}
}
}
return ;
} int hk()
{
int res = ;
mem(cx, -);
mem(cy, -);
while(bfs())
{
mem(used, );
for(int i=; i<=nx; i++)
if(cx[i] == - && dfs(i))
res++;
}
return res;
} int istwo(int u)
{
queue<int> E;
mem(vis, -);
E.push(u);
vis[u] = ;
while(!E.empty())
{
u = E.front(); E.pop();
for(int i=; i<G[u].size(); i++)
{
int v = G[u][i];
if(vis[v] == -)
{
if(vis[u] == ) vis[v] = ;
else vis[v] = ;
E.push(v);
}
else if(vis[v] == vis[u])
return ;
}
}
return ;
} int main()
{
while(cin>> n >> m && n+m)
{
for(int i=; i<maxn; i++) G[i].clear(); for(int i=; i<m; i++)
{
int u, v;
cin>> u >> v;
G[u].push_back(v);
G[v].push_back(u);
} if(!istwo() || n == )
{
cout<< "No" <<endl;
continue;
}
nx = n; ny = n;
cout<< hk()/ <<endl; } return ;
}
The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)的更多相关文章
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
/*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...
- (匹配)The Accomodation of Students --HDU --2444
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2444 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- B - The Accomodation of Students - hdu 2444(最大匹配)
题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求 ...
- HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 1281 棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- hdu 1045 Fire Net(二分匹配 or 暴搜)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 2444 交叉染色判断二分图+二分最大匹配
/*1A 31ms*/ #include<stdio.h> #include<string.h> #define N 300 int n; struct node { int ...
随机推荐
- tarjan 求割点
在无向连通图中,如果将其中一个点以及所连的所有边都删掉,图就不再连通的话,那么这个点就叫做割点 首先将所有的点分为:1.环中点 2.不成环的单点割点一般出现的情况是:如果(处在不同环中/一环一单点/均 ...
- DB2编目配置及管理
打开命令:db2cmd 我一般打开的方式如下: (在db2) ...
- 解决Ubuntu14.04安装Chrome浏览器打不开的问题
1.安装Chrome浏览器 wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/ ...
- Android Notification的使用 - z
http://blog.csdn.net/new_one_object/article/details/55511253 另,博主其它文章也很好
- 使用Gzip压缩数据,加快页面访问速度
在返回的json数据量大时,启用Gzip压缩,可以提高传输效率.下面为Gzip压缩对json字符串压缩并输出到页面的代码. 一.代码 /** 向浏览器输出字符串响应数据,启用 ...
- IIS发布问题
下午发布一个IIS ,出现一个很奇葩的问题,在本地跑代码运行都正常,但是发布到IIS上后 访问提示: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framewo ...
- spring配置多个事务管理器
<tx:annotation-driven/> <bean id="transactionManager1" class="org.springfram ...
- python 回溯法 子集树模板 系列 —— 8、图的遍历
问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...
- 【第六课】Nginx常用配置下详解
目录 Nginx常用配置下详解 1.Nginx虚拟主机 2.部署wordpress开源博客 3.部署discuz开源论坛 4.域名重定向 5.Nginx用户认证 6.Nginx访问日志配置 7.Ngi ...
- java数据结构之hashMap
初学JAVA的时候,就记得有句话两个对象的hashCode相同,不一定equal,但是两个对象equal,hashCode一定相同,当时一直不理解是什么意思,最近在极客时间上学习了课程<数据结构 ...