您的位置:首页 > 其它

hihoCoder #1121 : 二分图一•二分图判定

2015-03-07 15:04 260 查看

#1121 : 二分图一•二分图判定

Time Limit:10000ms
Case Time Limit:1000ms
Memory Limit:256MB

描述

大家好,我是小Hi和小Ho的小伙伴Nettle,从这个星期开始由我来完成我们的Weekly。

新年回家,又到了一年一度大龄剩男剩女的相亲时间。Nettle去姑姑家玩的时候看到了一张姑姑写的相亲情况表,上面都是姑姑介绍相亲的剩男剩女们。每行有2个名字,表示这两个人有一场相亲。由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别。因此她拜托我检查一下相亲表里面有没有错误的记录,即是否把两个同性安排了相亲。

OK,让我们愉快的暴力搜索吧!

才怪咧。

对于拿到的相亲情况表,我们不妨将其转化成一个图。将每一个人作为一个点(编号1..N),若两个人之间有一场相亲,则在对应的点之间连接一条无向边。(如下图)

/*
@author: Lev
@date:
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <climits>
#include <deque>
#include <sstream>
#include <fstream>
#include <bitset>
#include <iomanip>
#define LL long long
#define INF 0x3f3f3f3f

using namespace std;
struct arc {
int to,next;
arc(int x = 0,int y = -1) {
to = x;
next = y;
}
};
arc e[500000];
int head[100000],color[100000],tot,n,m;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
queue<int>q;
bool bfs(int x) {
while(!q.empty()) q.pop();
q.push(x);
color[x] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(color[e[i].to]) {
if(color[e[i].to] == color[u]) return false;
} else {
color[e[i].to] = -1*color[u];
q.push(e[i].to);
}
}
}
return true;
}
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d %d",&n,&m);
memset(head,-1,sizeof(head));
memset(color,0,sizeof(color));
for(int i = tot = 0; i < m; ++i) {
int u,v;
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
bool ans = true;
for(int i = 1; i <= n; ++i)
if((!color[i]) && (ans = bfs(i))) continue;
else break;
printf("%s\n",ans?"Correct":"Wrong");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: