HDU 2444
2015-08-09 20:43
281 查看
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3561 Accepted Submission(s): 1656
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
//二分染色+匈牙利
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3561 Accepted Submission(s): 1656
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
//二分染色+匈牙利
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struct Node { int v; int next; }Edge[200*200*2+10]; int pre[210]; void add(int u,int v,int index) { Edge[index].v=v; Edge[index].next=pre[u]; pre[u]=index; } int flag[210],vis[210]; int link[210]; int c[210]; int bfs(int v) { queue <int> q; vis[v]=1; flag[v]=1; q.push(v); while(!q.empty()) { int b=q.front(); q.pop(); for(int i=pre[b];i!=-1;i=Edge[i].next) { int e=Edge[i].v; if(flag[e]==flag[b]) return 0; if(!flag[e]) { flag[e]=flag[b]==1?2:1; q.push(e); } } } return 1; } bool Find(int v) { for(int i=pre[v];i!=-1;i=Edge[i].next) { int e=Edge[i].v; if(!vis[e]) { vis[e]=1; if(link[e]==-1||Find(link[e])) { link[e]=v; return true; } } } return false; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int a,b,index=1; memset(flag,0,sizeof(flag)); memset(pre,-1,sizeof(pre)); memset(link,-1,sizeof(link)); memset(c,0,sizeof(c)); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); add(a,b,index++); add(b,a,index++); c[a]++,c[b]++; } int state=0; for(int i=1;i<=n;i++) { if(!flag[i]&&c[i]) { if(!bfs(i)) { state=1; break; } } } int cnt=0; if(state) printf("No\n"); else { for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(Find(i)) cnt++; } printf("%d\n",cnt/2); //除以2 } } return 0; }
相关文章推荐
- Android四大组件之三:ContentProvider数据共享
- unbuntu下如何更新adobe flash插件
- IM-即时通讯技术概述
- 协议详解3——IP
- PyQt5初级教程--PyQt5中的拖放[10/13]
- HDU 5366
- 【C/C++学院】0726-cppIDE/一级指针/指针数组/函数指针/函数指针数组/二级指针
- Flatten Binary Tree to Linked List
- Spring注解
- 顿悟,职业生涯最重要的是行业水平的积累,而不是多学某一门语言(很危险)——遥想铁血强人刘志军对铁路行业的理解以及执行力
- 使用Jayrock开源组件创建参数可为空的接口
- Program D--贪心-区间覆盖
- 图片下载缓存防止OOM
- Sqrt(x)
- 2、按下按键S1控制LED1.LED2.LED3实现跑马灯效果(CC2540开发寄存器设置)
- 2015 HUAS Summer Trainning #4 D
- Java文件删除问题
- Javascript判断变量是否定义的方法,欢迎补充
- 代码获取Android版本等信息
- 指针和动态内存