二分图判定 hdu5285 wyh2000 and pupil
来源:互联网 发布:淘宝客服的自我介绍 编辑:程序博客网 时间:2024/06/08 13:23
很明显要求一个二分图。先构图,然后用精典的点染色的方法,求出二分图中两个图的节点数量
因为整个图中有很多个通块,每个通块都做一次二分图
合并的时候,把每个二分图中节点多的那部分的数量累加,就得到了a答案
那么b就是总数减去a
这题本身不难,但是数据非常贱,题目要求每组中必须有一个人,且m可能为0
所以,要进行两个特判
n<=1时,不可能每组至少1人,直接输出Poor wyh
m==0时,没有任何限制,但是b组至少要一个人,所以输出n-1 1(有无数人在这里wa了貌似kuangbin也是)
然后还可能爆栈(虽然最终测评里没有,,还好没这样出数据)
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<functional>#include<algorithm>using namespace std;typedef long long LL;typedef pair<int, int> PII;#pragma comment(linker,"/STACK:102400000,102400000")const int MX = 100000 + 5;const int INF = 0x3f3f3f3f;int vis[MX];vector<int>G[MX];bool DFS(int u, int c, int &a, int &b) { b++; if(c) a++; vis[u] = c; for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(vis[v] == c) return false; if(vis[v] == -1 && !DFS(v, c ^ 1, a, b)) return false; } return true;}int main() { int T, n, m; scanf("%d", &T); while(T--) { memset(vis, -1, sizeof(vis)); scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { G[i].clear(); } for(int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } if(n <= 1) { printf("Poor wyh\n"); continue; } if(m == 0) { printf("%d 1\n", n - 1); continue; } bool sign = true; int Max = 0; for(int i = 1; i <= n; i++) { int a = 0, b = 0; if(vis[i] == -1) { if(!DFS(i, 0, a, b)) { sign = false; break; } Max += max(a, b - a); } } if(!sign) printf("Poor wyh\n"); else { printf("%d %d\n", Max, n - Max); } } return 0;}
0 0
- 二分图判定 hdu5285 wyh2000 and pupil
- HDU5285:wyh2000 and pupil(二分图染色 & 贪心)
- hdu5285 wyh2000 and pupil
- HDU5285.wyh2000 and pupil
- [hdu5285]wyh2000 and pupil
- hdu 5285 wyh2000 and pupil(二分图的判定)
- BestCoder Round #48 ($)HDU5285 wyh2000 and pupil
- hdu5385 wyh2000 and pupil 二分图
- hdu 5285 wyh2000 and pupil 二分图染色发判定,求最大匹配
- HDU 5285 wyh2000 and pupil(染色法判断二分图)
- hdu5258 wyh2000 and pupil(二分图染色)
- hdu 5285 wyh2000 and pupil【二分图+染色法】
- HDU 5285 wyh2000 and pupil(二分图,染色法)
- HDU 5285 wyh2000 and pupil (二分图 bfs染色)
- hdu5285 二分图的判定
- wyh2000 and pupil
- wyh2000 and pupil
- hdu 5285 wyh2000 and pupil(判断是否是二分图+贪心)
- 实习14天
- HDU 2157 How many ways??
- OC学习笔记之C语言字符串处理(非指针版)
- JAVA基础_简析Java中的new,this,super,final关键字
- C++ string详解
- 二分图判定 hdu5285 wyh2000 and pupil
- 写在课程设计之后
- loadrunner使用web_custom_request post数据
- HDU 1061 Rightmost Digit
- 虚拟内存 物理内存
- 常用的一些网址
- HDU 1301 Jungle Roads(Prim求最小生成树)
- 原型模式
- HDU 4565 So Easy!