標籤:hdu 二分圖染色 貪心
wyh2000 and pupilTime Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1040 Accepted Submission(s): 331
Problem DescriptionYoung theoretical computer scientist wyh2000 is teaching his pupils.
Wyh2000 has n pupils.Id of them are from 1 to n.In order to increase the cohesion between pupils,wyh2000 decide to divide them into 2 groups.Each group has at least 1 pupil.
Now that some pupils don‘t know each other(if a doesn‘t know b,thenb doesn‘t know a).Wyh2000 hopes that if two pupils are in the same group,then they know each other,and the pupils of the first group must be as much as possible.
Please help wyh2000 determine the pupils of first group and second group. If there is no solution, print "Poor wyh". Input In the first line, there is an integerT indicates the number of test cases.
For each case, the first line contains two integers n,m indicate the number of pupil and the number of pupils don‘t konw each other.
In the next m lines,each line contains 2 intergers x,y(x<y),indicates that x don‘t know y and y don‘t know x,the pair (x,y) will only appear once.
T≤10,0≤n,m≤100000 Output For each case, output the answer. Sample Input
28 53 45 61 25 83 55 42 34 53 42 4
Sample Output
5 3Poor wyh
Source BestCoder Round #48 ($)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5285
題目大意:一些人要分成兩組,(這群人真的好累啊,一天到晚被分來分去),給出他們之間的不認識關係,兩個不認識的人不能分到一組,且每組都要至少有一個人,問分完後兩組人數的最大可能值和最小可能值
題目分析:題意化簡一下,就是求幾個獨立的二分圖(可能有單點不過不礙事)合成一個大二分圖,使得大二分圖兩側點數差的絕對值最大,顯然這裡用貪心思想,對每個二分圖,我把點數大的那一側的全部並起來作為大二分圖的一側,另一側用n減即可,判斷二分圖和記錄點數採用的是DFS染色法,注意幾個wa點,n<2時肯定沒法分,還有m等於0時,顯然可以有n個平凡圖,這種特殊情況下,因為每組至少有一個人,直接n-1和1即可
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 100005;int n, m, cnt;int head[MAX], color[MAX], num[2];bool vis[MAX], flag;struct EDGE{ int v, next;}e[MAX * 2]; void Add(int u, int v){ e[cnt].v = v; e[cnt].next = head[u]; head[u] = cnt ++;}void DFS(int u, int col){ vis[u] = true; color[u] = col; num[col] ++; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; if(vis[v]) { if(color[v] == color[u]) { flag = true; return; } } else DFS(v, col ^ 1); } return;}int main(){ int T; scanf("%d", &T); while(T --) { memset(head, -1, sizeof(head)); memset(color, -1, sizeof(color)); memset(vis, false, sizeof(vis)); cnt = 0; flag = false; scanf("%d %d", &n, &m); for(int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); Add(u, v); Add(v, u); } if(n < 2) { printf("Poor wyh\n"); continue; } if(m == 0) { printf("%d 1\n", n - 1); continue; } int ans = 0; for(int i = 1; i <= n; i++) { if(!vis[i]) { memset(num, 0, sizeof(num)); DFS(i, 0); if(flag) break; ans += max(num[0], num[1]); } } if(flag) printf("Poor wyh\n"); else printf("%d %d\n", ans, n - ans); }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
HDU 5285 wyh2000 and pupil (DFS染色判二分圖 + 貪心)