wyh2000 and pupil
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 794 Accepted Submission(s): 260
Problem Description
Young 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 ,then b 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".
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 ,then b 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 integer
T
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
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
2 8 5 3 4 5 6 1 2 5 8 3 5 5 4 2 3 4 5 3 4 2 4
Sample Output
5 3 Poor wyh 题意:有n个小朋友,其中m对互不认识,求能不能把他们分成两组,使得组内没有互不认识的人,并且使其中一组人数最大。如果没有可行方案,则输出Poor wyh,如果有则输出两组的人数。 思路:把m对数连线组成1或以上个无向连通图。如果他们都不是二分图,则没有可行方案。如果都是二分图,则取每个二分图中数量大的一分和没包括在图中的独立点即组成了较大的一组。而对于二分图的判断,可用染色法, 开始对任意一未染色的顶点染色,之后判断其相连的顶点,若未染色则将其染上和相邻顶点不同的颜色,若已经染色且颜色和相连顶点的颜色相同则说明不是二分图,若颜色不同则继续判断,用深搜即可。#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> using namespace std; vector<int> e[100009]; int ran[100009]; bool erfentu=true; int e1, e2, n, m; void dfs(int i) { if(ran[i]==0) { ran[i]=1; e1++; } for(int j=0; j<e[i].size(); j++) { int ee=e[i][j]; if(ran[ee]==ran[i]) { erfentu=false; return; } else if(ran[ee]!=0) { continue; } else if(ran[ee]==0) { if(ran[i]==1) ran[ee]=2, e2++, dfs(ee); else ran[ee]=1, e1++, dfs(ee); } } } int main() { int t; scanf("%d", &t); while(t--) { memset(ran, 0, sizeof(ran)); for(int i=0; i<100009; i++) e[i].clear(); scanf("%d %d", &n, &m); if(n<2) { printf("Poor wyh\n"); continue; } if(m==0) { printf("%d 1\n", n-1); continue; } for(int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); e[a].push_back(b); e[b].push_back(a); } int ansmin=0; for(int i=1; i<=n; i++) { e1=0, e2=0; if(e[i].size()>0 && ran[i]==0) dfs(i); ansmin+=min(e1, e2); } if(erfentu) { printf("%d %d\n", n-ansmin, ansmin); } else { printf("Poor wyh\n"); } } return 0; }