HDU 5285 wyh2000 and pupil (二分图着色)

时间:2021-09-15 06:29:07

题意:

  共有n个小学生,编号为1−n。将所有小学生分成2组,每组都至少有1个人。但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a。Wyh2000希望每组中的小学生都互相认识。而且第一组的人要尽可能多。请你帮wyh2000求出第一组和第二组的人数是多少。如果找不到分组方案,则输出"Poor wyh"。

思路:

  二分图着色。给的就是无向图,每次都累加人多的颜色即可。若不能着色,必定不能分成2组。如果全部都是1个颜色,那么要让其中1人过第2组。我勒个去,就因为赭色时颜色号码开小了而错。

 //#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <vector>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+; vector<int> vect[N];
int col[N];
set<int> mapp; int color(int u, int f)
{
mapp.clear();
deque<int> que;
que.push_back(u);
col[u]=f;
mapp.insert(u);
while(!que.empty())
{
int x=que.front();
que.pop_front(); for(int i=; i<vect[x].size(); i++)
{
int tmp=vect[x][i];
if(col[tmp]==col[x]) return ; //冲突
if(col[tmp]==) //没色
{
mapp.insert(tmp);//一个连同分量装进去
col[tmp]=-col[x];
que.push_back(tmp);
}
}
}
return ;
} int cal(int n)
{
memset(col,,sizeof(col));
int big=, small=, k=;
for(int i=; i<=n; i++)
{
if(!col[i])
{
if(!color(i, ++k)) return ; //统计人数
int a=, b=;
set<int>::iterator it=mapp.begin();
for(int j=; j<mapp.size(); j++)
{
if(col[*it]==k) a++;
if(col[*it]==-k) b++;
it++;
} if(a<b) swap(a, b);
big+=a;
small+=b;
}
}
if(!small) return big-;
else return big;
} int main()
{
freopen("input.txt", "r", stdin);
int t, n, m, a, b;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++) vect[i].clear(); for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
vect[a].push_back(b);
vect[b].push_back(a);
}
if(n<=)
{
puts("Poor wyh");
continue;
}
else if(n==)
{
puts("1 1");
continue;
} int tmp=cal(n);
if(tmp) printf("%d %d\n",tmp, n-tmp);
else puts("Poor wyh"); }
return ;
}

AC代码