题意:有一群人,已知某两人之间互相不认识,要把这群人分成两部分,每部分至少一人,且在每部分内没有人互不认识。

解法:图染色。某场bestcoder第二题……看完题觉得是个二分图……完全不会二分图什么的……但是为了挣扎一下百度了一下二分图的判定方法,知道了可以用染色法,这样如果是二分图的话将每个连通分量里点数量最多的颜色的点数量(像个绕口令诶)相加就可以了。然而激动万分的我早忘了还有每部分至少一人这个条件……直到我和队友研究怎么hack别人的时候他才告诉我还有这么个条件……(哭)还好来得及……

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int vis[100005];
vector <int> v[100005];
int bfs(int u)
{
queue <int> q;
q.push(u);
vis[u] = 0;
int res[2] = {1, 0};
while(!q.empty())
{
int tmp = q.front();
q.pop();
int len = v[tmp].size();
for(int i = 0; i < len; i++)
{
if(vis[v[tmp][i]] == -1)
{
vis[v[tmp][i]] = !vis[tmp];
q.push(v[tmp][i]);
res[vis[v[tmp][i]]]++;
}
else if(vis[v[tmp][i]] == vis[tmp])
return -1;
}
}
return max(res[0], res[1]);
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
int n, m;
for(int i = 0; i < 100005; i++)
v[i].clear();
memset(vis, -1, sizeof vis);
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
int ans = 0;
for(int j = 1; j <= n; j++)
{
if(vis[j] == -1)
{
int tmp = bfs(j);
if(tmp == -1)
{
ans = -1;
break;
}
else
{
ans += tmp;
}
}
}
if(ans == -1 || (ans == n && n < 2))
{
puts("Poor wyh");
}
else if(ans != n)
printf("%d %d\n", ans, n - ans);
else
printf("%d %d\n", ans - 1, n - ans + 1);
}
}
return 0;
}

  

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 (二分图着色)

    题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...

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

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

  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 5285:wyh2000 and pupil

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

  8. 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. iOS 通览(二)

    一.关键词 extern:C语言的函数外部声明. 如果你要在一个.c或者.m中使用另外一个.c文件的函数的话,需要在文件中写入目标函数的外部引用的声明. 二.自定义View 自定义View添加控件对象 ...

  2. git - 必备指令

    1. 查看远程分支 加上-a参数可以查看远程分支,远程分支会用红色表示出来(如果你开了颜色支持的话): $ git branch -a master remote tungway v1. * zron ...

  3. 在工程中添加pch文件

    在Xcode6之前,新建一个工程的时候,系统会帮我们自动新建一个以工程名为名字的pch (precompile header)文件,在开发过程中,可以将那些整个工程都广泛使用的头文件包含在该文件下,编 ...

  4. 在SpringMVC利用MockMvc进行单元测试

    spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.ht ...

  5. flash链接需要后台调用时的插入flash方法

    <!--<textarea id="syslink" style="display:none;">// 1:打宝塔 2:山寨玩法 3:跨服竞技 ...

  6. replace()替换文字

    var test = text.innerHTML; b = test.replace(/任晓强/g,"你好"); console.log(b); html: <div id ...

  7. 10 things you should know about NoSQL databases

    For a quarter of a century, the relational database (RDBMS) has been the dominant model for database ...

  8. 5.查找最小的k个元素(数组)

    题目: 输入n个整数,输出其中最小的k个,例如输入1,2,3,4,5,6,7,8这8个数,则最小的4个是1,2,3,4(输出不要求有序) 解: 利用快速排序的partition,算导上求第k大数的思想 ...

  9. live555源码研究(五)------DynamicRTSPServer类

    一.类DynamicRTSPServer作用 1,提供RTSP服务 二.类DynamicRTSPServer继承关系图

  10. OWASP-ZAP

    Zed Attack Proxy简写为ZAP,是一个简单易用的渗透测试工具,是发现Web应用中的漏洞的利器,更是渗透测试爱好者的好东西. ZAP下载地址:https://www.owasp.org/i ...