2 8 5 3 4 5 6 1 2 5 8 3 5 5 4 2 3 4 5 3 4 2 4
5 3 Poor wyh
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 100005;
int n, m, cnt;
int head[MAX], color[MAX], num[2];
bool vis[MAX], flag;
struct EDGE
{
int v, next;
}e[MAX * 2];
void Add(int u, int v)
{
e[cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void DFS(int u, int col)
{
vis[u] = true;
color[u] = col;
num[col] ++;
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].v;
if(vis[v])
{
if(color[v] == color[u])
{
flag = true;
return;
}
}
else
DFS(v, col ^ 1);
}
return;
}
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
memset(head, -1, sizeof(head));
memset(color, -1, sizeof(color));
memset(vis, false, sizeof(vis));
cnt = 0;
flag = false;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
Add(u, v);
Add(v, u);
}
if(n < 2)
{
printf("Poor wyh\n");
continue;
}
if(m == 0)
{
printf("%d 1\n", n - 1);
continue;
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
memset(num, 0, sizeof(num));
DFS(i, 0);
if(flag)
break;
ans += max(num[0], num[1]);
}
}
if(flag)
printf("Poor wyh\n");
else
printf("%d %d\n", ans, n - ans);
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5285 wyh2000 and pupil (DFS染色判二分图 + 贪心)
原文地址:http://blog.csdn.net/tc_to_top/article/details/47066435