题意:

  共有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代码

HDU 5285 wyh2000 and pupil (二分图着色)的更多相关文章

  1. HDU 5285 wyh2000 and pupil 判二分图+贪心

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5285 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  2. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  3. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

  4. HDU 5285 wyh2000 and pupil

    题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识. 解法:图染色.某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的 ...

  5. hdu 5285 wyh2000 and pupil(二染色)

    第一次用vector解得题.值得纪念,这道题是二染色问题,我用bfs解得.就是染色,推断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,由于题目要求每一个组至少有一个 ...

  6. ACM: HDU 5285 wyh2000 and pupil-二分图判定

     HDU 5285  wyh2000 and pupil Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  7. HDU 5313 Bipartite Graph (二分图着色,dp)

    题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...

  8. HDU 5285:wyh2000 and pupil

    wyh2000 and pupil  Accepts: 93  Submissions: 925  Time Limit: 3000/1500 MS (Java/Others)  Memory Lim ...

  9. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

随机推荐

  1. 本地虚拟机中匿名ftp上传文件失败的问题

    在10.10.50.230中新建了一个匿名的ftp服务器,结果在10.10.50.241中上传文件时提示: local: README.txt remote: /var/ftp/pub/upload ...

  2. 虚拟机移动后重启网络时提示Device does not seem to be present

    Wmware虚拟机硬盘文件位置移动之后,重新引入到Wmware workStation中, 通过命令ifconfig...没有看到eth0..然后重启网卡 #service network resta ...

  3. SQL Server视图

    想来想去,总想写写SQL Server方面的知识,像视图.存储过程,大数据量操作的优化等等. 先把基础的知识总结个遍先,然后再寻求更高更远的发展.这篇文章,将带大家来看看视图. 何谓视图,视图包含行和 ...

  4. 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  5. Matlab中unifrnd函数使用解析

    1.生成N阶[a,b]均匀分布数组 >> unifrnd(3,5,5,5) ans = 3.8651 4.6677 4.8115 4.3456 4.8560 4.0241 3.4079 3 ...

  6. C# 读取枚举描述信息实例

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  7. linux源码阅读笔记 asm函数

    在linux源码中经常遇到__asm__函数.它其实是函数asm的宏定义 #define __asm__ asm,asm函数让系统执行汇编语句. __asm__常常与__volatile__一起出现. ...

  8. override equals in Java

    equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive ...

  9. MYSQL存储过程中常使用的命令记录

    MYSQL存储过程中常使用的命令记录 1.触发器trigger 查看:show triggers; 2.存储过程procedure 查看:show procedure status; 查看详细:sho ...

  10. Oracle 学习笔记(三)

    1.插入有日期的表,使用 to_date 函数 to_date('1992-12-07', 'yyyy-mm-dd'); 2.使用update更新语句的时候,既可以使用表达式或者数值直接修改数据,也可 ...