The Accomodation of Students

本文探讨了如何通过算法解决学生住宿分配问题,确保互不认识的学生被安排在同一宿舍,以实现最大化的双人间使用效率。文章提供了具体算法实现,包括数据结构定义、匹配过程描述及示例输入输出。

The Accomodation of Students

http://acm.hdu.edu.cn/showproblem.php?pid=2444

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9349    Accepted Submission(s): 4104


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
 
先用num数组判重,然后跑二分图。。还有,输出是No不是NO。。
 1 #include<iostream>
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstring>
 5 #include<algorithm>
 6 #define mem(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 
 9 int mp[205][205];
10 int man[205],woman[205];
11 int flag[205],vis[205];
12 int n,m;
13 
14 int DFS(int u){
15     for(int i=1;i<=n;i++){
16         if(mp[u][i]&&!vis[i]){
17             vis[i]=1;
18             if(!man[i]||DFS(man[i])){
19                 woman[u]=i;
20                 man[i]=u;
21                 return 1;
22             }
23         }
24     }
25     return 0;
26 }
27 
28 int Match(){
29     mem(man,0);
30     mem(woman,0);
31     int ans=0;
32     for(int i=1;i<=n;i++){
33         if(!woman[i]){
34             mem(vis,0);
35             ans+=DFS(i);
36         }
37     }
38     return ans/2;
39 }
40 
41 int main(){
42     while(cin>>n>>m){
43         int a,b;
44         mem(mp,0);
45         mem(flag,0);
46         int Flag=0;
47         for(int i=1;i<=m;i++){
48             cin>>a>>b;
49             mp[a][b]=mp[b][a]=1;
50             if(!flag[a]&&!flag[b]) flag[a]=1,flag[b]=2;
51             else if(flag[a]==1&&!flag[b]) flag[b]=2;
52             else if(flag[a]==2&&!flag[b]) flag[b]=1;
53             else if(!flag[a]&&flag[b]==1) flag[a]=2;
54             else if(!flag[a]&&flag[b]==2) flag[a]=1;
55             else if(flag[a]==flag[b]&&flag[a]) Flag=1;
56         }
57         if(Flag) cout<<"No"<<endl;
58         else cout<<Match()<<endl;
59     }
60 }
View Code

 

转载于:https://www.cnblogs.com/Fighting-sh/p/9751941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值