wyh2000 and pupil
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1195 Accepted Submission(s): 364
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".
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
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
//让你求是不是二分图 如果是的话输出两个子集的个数 并且第一个子集的个数尽可能大于第二个
//不是输出poor~ 这题很坑 特判几个数据
//染色 + bfs+邻接表
//染色时由于有多个连通分支 所以每次每个连通分支标记完要取出最大的值 累加起来就是最大的
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int n,m;
int co1,co2;
struct Node
{
int v;
int next;
}Edge[200010];
int pre[100010];
int c[100100];
int color[100010];
void add(int u,int v,int index)
{
Edge[index].v=v;
Edge[index].next=pre[u];
pre[u]=index;
}
int bfs(int v)
{
queue <int> q;
q.push(v);
color[v]=1;
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(color[e]==color[b])
return false;
if(!color[e])
{
color[e]=color[b]==1?2:1;
q.push(e);
if(color[b]==2)
co1++;
else
co2++;
}
}
}
return true;
}
int main()
{
int t,a,b;
scanf("%d",&t);
while(t--)
{
int index=1;
memset(color,0,sizeof(color));
memset(pre,-1,sizeof(pre));
memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b,index++);
add(b,a,index++);
c[a]++,c[b]++;
}
if(n==0||n==1)
{
printf("Poor wyh\n");
continue;
}
if(m==0)
{
printf("%d %d\n",n-1,1);
continue;
}
int flag,cnt,num_max;
flag=cnt=num_max=0;
for(int i=1;i<=100001;i++)
{
if(c[i])
cnt++;
}
for(int i=1;i<=n;i++)
{
if(!color[i]&&c[i])
{
co1=1; //开始标记为1
co2=0;
if(!bfs(i))
{
flag=1;
break;
}
// printf("%d %d\n",co1,co2);
num_max+=max(co1,co2); 累加
}
}
if(flag)
printf("Poor wyh\n");
else
printf("%d %d\n",num_max+n-cnt,cnt-num_max);
}
return 0;
}