The Accomodation of Students(二分图判断+匈牙利)
2016-05-07 22:11
381 查看
The Accomodation of Students
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
2444
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:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
2444
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 <cstdio> #include <cstring> #include <algorithm> #define MAXN 201 using namespace std; int Map[MAXN][MAXN]; int color[MAXN];//染色 int N, M; bool judge(int u)//判断是否是二分图 { for(int i = 1; i <= N; i++) { if(Map[u][i] == 0 && Map[i][u] == 0) continue; ///注意判断条件,因为这是建的有向图了,因为在一般的二分图匹配的过程中是有向图和 ///无向图都一样的,因为用的是match都做标记了,但是在这里dfs判断的时候就必须不一样了 if(color[u] == color[i]) return false; if(!color[i]) { color[i] = 3 - color[u]; if(!judge(i)) return false; } } return true; } int match[MAXN]; bool used[MAXN]; int DFS(int u) { for(int i = 1; i <= N; i++) { if(Map[u][i] == 0) continue; if(!used[i]) { used[i] = true; if(match[i] == -1 || DFS(match[i])) { match[i] = u; return 1; } } } return 0; } void solve() { memset(Map, 0, sizeof(Map)); int a, b; for(int i = 1; i <= M; i++) { scanf("%d%d", &a, &b); Map[a][b] = 1; } memset(color, 0, sizeof(color)); color[1] = 1; if(!judge(1)) { printf("No\n"); return ; } int ans = 0; memset(match, -1, sizeof(match)); for(int i = 1; i <= N; i++) { memset(used, false, sizeof(used)); ans += DFS(i); } printf("%d\n", ans); int main() { while(scanf("%d%d", &N, &M) != EOF) { solve(); } return 0; }
当做无向图:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 201 using namespace std; int Map[MAXN][MAXN]; int color[MAXN];//染色 int N, M; bool judge(int u)//判断是否是二分图 { for(int i = 1; i <= N; i++) { if(Map[u][i] == 0) continue; if(color[u] == color[i]) return false; if(!color[i]) { color[i] = 3 - color[u]; if(!judge(i)) return false; } } return true; } int match[MAXN]; bool used[MAXN]; int DFS(int u) { for(int i = 1; i <= N; i++) { if(Map[u][i] == 0) continue; if(!used[i]) { used[i] = true; if(match[i] == -1 || DFS(match[i])) { match[i] = u; return 1; } } } return 0; } void solve() { memset(Map, 0, sizeof(Map)); int a, b; for(int i = 1; i <= M; i++) { scanf("%d%d", &a, &b); Map[a][b] = Map[b][a] = 1; } memset(color, 0, sizeof(color)); color[1] = 1; if(!judge(1)) { printf("No\n"); return ; } int ans = 0; memset(match, -1, sizeof(match)); for(int i = 1; i <= N; i++) { memset(used, false, sizeof(used)); ans += DFS(i); } printf("%d\n", ans / 2); //////对两个部里的都匹配了,这样就相当于匹配了两次了 } int main() { while(scanf("%d%d", &N, &M) != EOF) { solve(); } return 0; }
相关文章推荐
- android应用开发(触摸事件的开发)
- 1001 Problem A
- Myeclipes
- 20150302+JQuery-02
- HDU 1056 - HangOver
- CNContact对通讯录的基本使用(第二篇)
- 数组循环右移
- Android adb.exe程序启动不起来,如何处理
- HOG特征+SVM训练过程
- 数据结构学习笔记06排序 (快速排序、表排序、基数排序)
- 20150302+JQuery-01
- 图片轮换特效
- poj2406 扩展kmp
- CAFFE学习笔记(二)Caffe_Example之测试mnist
- HDU 2544 最短路(Dijkstra算法)
- UE4:默认打开最近工程
- Head插件——学习Elasticsearch的锋刃利器!
- 使用__slots__限定实例的成员列表
- poj2503字符串HASH
- FZU Problem 2232 炉石传说