博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Accomodation of Students
阅读量:6479 次
发布时间:2019-06-23

本文共 3777 字,大约阅读时间需要 12 分钟。

The Accomodation of Students
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit     

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
题意:最大分配,
意:有一些学生,他们之间有些互相认识,有些不认识,其中若A和B认识,B和C认识,并不代表A和C认识,现在提供互相认识的学生编号,问是否能将这些学生分成两组,同一组的学生互相不认识,如果可以,则分配给这些学生双人间,只有是朋友才能住双人间,问最多要准备几间房(
即问是否为二分图,若是,其最大匹配数是多少
http://www.cnblogs.com/syhandll/p/4721270.html
分析:
首先进行染色,判断是否是二分图,如果是二分图的话求出最大匹配,如果不是的话,输出No
 
判断二分图的常见方法是染色法: 开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图。
http://blog.csdn.net/w144215160044/article/details/47404665
 
1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 9 //int main(int argc, char** argv) 10 11 #define N 100512 13 int n, m, flag, ans, x, y;14 int maps[N][N], vis[N], used[N], c[N];15 16 int found(int u)17 {18 for(int i = 1; i <= n; i++)19 {20 if(! vis[i] && maps[u][i])21 {22 vis[i] = 1;23 if(!used[i] || found(used[i]))24 {25 used[i] = u;26 return true;27 }28 }29 }30 return false;31 }32 33 void dfs(int u, int k) //所谓染色34 {35 c[u] = k;36 for(int i = 1; i <= n; i++)37 {38 if(maps[u][i] && !c[i]) 41 dfs(i, -k);42 }43 }44 45 46 int main()47 {48 while(scanf("%d%d", &n, &m) != EOF)49 {50 memset(used, 0, sizeof(used));51 memset(maps, 0, sizeof(maps));52 memset(c, 0, sizeof(c));53 54 ans = flag = 0;55 56 while(m--)57 {58 scanf("%d%d", &x, &y);59 maps[x][y] = 1;60 vis[x] = vis[y] = true;61 }62 63 for(int i = 1; i <= n; i++)64 {65 if(!vis[i])66 continue;67 else if(!c[i])68 dfs(i, 1);69 }70 for(int i = 1; i <= n; i++)71 {72 for(int j = 1; j <= n; j++)73 {74 if(maps[i][j] && c[i]+c[j]) //一个集合里有认识的人了,flag = 175 flag = 1;76 }77 }78 if(flag)79 printf("No\n");80 else81 {82 for(int i = 1; i <= n; i++)83 {84 memset(vis, 0, sizeof(vis));85 if(found(i) && c[i])86 ans++;87 }88 printf("%d\n", ans);89 }90 }91 return 0;92 }

 

 

转载于:https://www.cnblogs.com/Tinamei/p/4722232.html

你可能感兴趣的文章
moosefs即将发布新版
查看>>
Forefront Client Security部署前准备
查看>>
WCF4.0新特性体验(12):服务发现WS-Discovery之Managed Service Discovery
查看>>
FOSCommentBundle功能包:运行测试
查看>>
python
查看>>
SmartGit 试用过期
查看>>
c#参数传递几点小结
查看>>
python 测试驱动开发的简单例子
查看>>
设计模式:观察者模式
查看>>
JDBC中驱动加载的过程分析
查看>>
Aes 加密简单例子
查看>>
AE 线编辑
查看>>
python 回溯法 子集树模板 系列 —— 15、总结
查看>>
软件设计之UML—UML的构成[上]
查看>>
蚂蚁金服硅谷ATEC科技大会:看技术如何带来平等的机会
查看>>
[SPLEB]CodeSmith原理剖析(1)
查看>>
PSR-2:编码风格指导
查看>>
jquery跟随屏幕滚动代码
查看>>
Jquery的$命名冲突
查看>>
UITextField用法
查看>>