HDU2444_The Accomodation of Students(二分图/最大匹配+判二分图)
2014-08-15 21:45
399 查看
解题报告
题目传送门
题意:
问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两认识的人可以去开房哈。求最大的开房数。
思路:
先判断是否能生成二分图,用黑白标记法,如果颜色相同就没办法分成两部分。
再求二分图的最大匹配。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int mmap[310][310],vis[310],pre[310],n,m,_hash[300];
int dfs(int x) {
for(int i=1; i<=n; i++) {
if(!vis[i]&&mmap[x][i]) {
vis[i]=1;
if(pre[i]==-1||dfs(pre[i])) {
pre[i]=x;
return 1;
}
}
}
return 0;
}
int main() {
int t,i,j,u,v;
while(~scanf("%d%d",&n,&m)) {
int f=0,cnt=0;
memset(pre,-1,sizeof(pre));
memset(mmap,0,sizeof(mmap));
memset(_hash,0,sizeof(_hash));
for(i=0; i<m; i++) {
scanf("%d%d",&u,&v);
if((_hash[u]==_hash[v])&&_hash[u]!=0)
f=1;
if(!_hash[u]&&!_hash[v]) {
_hash[u]=1;
_hash[v]=2;
}
if(!_hash[u]&&_hash[v]) {
if(_hash[v]==1)
_hash[u]=2;
else _hash[u]=1;
}
if(!_hash[v]&&_hash[u]) {
if(_hash[u]==1)
_hash[v]=2;
else _hash[v]=1;
}
mmap[u][v]=mmap[v][u]=1;
}
if(f) {
printf("No\n");
} else {
int ans=0;
for(i=1; i<=n; i++) {
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",ans/2);
}
}
return 0;
}
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2529 Accepted Submission(s): 1198
Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
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.
Input
For each data set:
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.
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3
Source
2008 Asia Harbin Regional Contest Online
题目传送门
题意:
问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两认识的人可以去开房哈。求最大的开房数。
思路:
先判断是否能生成二分图,用黑白标记法,如果颜色相同就没办法分成两部分。
再求二分图的最大匹配。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int mmap[310][310],vis[310],pre[310],n,m,_hash[300];
int dfs(int x) {
for(int i=1; i<=n; i++) {
if(!vis[i]&&mmap[x][i]) {
vis[i]=1;
if(pre[i]==-1||dfs(pre[i])) {
pre[i]=x;
return 1;
}
}
}
return 0;
}
int main() {
int t,i,j,u,v;
while(~scanf("%d%d",&n,&m)) {
int f=0,cnt=0;
memset(pre,-1,sizeof(pre));
memset(mmap,0,sizeof(mmap));
memset(_hash,0,sizeof(_hash));
for(i=0; i<m; i++) {
scanf("%d%d",&u,&v);
if((_hash[u]==_hash[v])&&_hash[u]!=0)
f=1;
if(!_hash[u]&&!_hash[v]) {
_hash[u]=1;
_hash[v]=2;
}
if(!_hash[u]&&_hash[v]) {
if(_hash[v]==1)
_hash[u]=2;
else _hash[u]=1;
}
if(!_hash[v]&&_hash[u]) {
if(_hash[u]==1)
_hash[v]=2;
else _hash[v]=1;
}
mmap[u][v]=mmap[v][u]=1;
}
if(f) {
printf("No\n");
} else {
int ans=0;
for(i=1; i<=n; i++) {
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",ans/2);
}
}
return 0;
}
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2529 Accepted Submission(s): 1198
Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
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.
Input
For each data set:
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.
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3
Source
2008 Asia Harbin Regional Contest Online
相关文章推荐
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- The Accomodation of Students---hdu2444(二分图,最大匹配)
- HDU2444 The Accomodation of Students(染色法判断二分图+最大匹配)
- hdu 2444 The Accomodation of Students(二分图判断+求最大匹配)
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- hdu 2444 The Accomodation of Students(二分图判断,二分图最大匹配)
- HDU 2444 The Accomodation of Students 判断二分图 + 最大匹配
- hdu 2444 The Accomodation of Students(判断二分图,二分图最大匹配)
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
- HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)
- hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)
- HDU - 2444 The Accomodation of Students(二分图判断+最大匹配)
- hdu 2444 The Accomodation of Students (判断二分图,二分图最大匹配)
- hdoj 2444 The Accomodation of Students 【黑白染色判二分图 + 最大匹配】
- HDU 2444 The Accomodation of Students(判断是否为二分图+最大匹配)
- hdu 2444 The Accomodation of Students (判断二分图+最大匹配)
- hdu 2444 The Accomodation of Students 二分图判断 + 最大匹配
- HDU 2444 The Accomodation of Students【二分图判断+最大匹配】
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
- HDOJ 题目2444 The Accomodation of Students(染色法判二分图,最大匹配)