传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4751
题意:给你一个有向图,问你能不能把这个有向图分成两个完全有向图(可以为空,可以有一个点)
题解:用两个集合来表示着两个完全图,当推出矛盾的时候就输出NO。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
typedef __int64 LL;
const int N=110;
const int mod=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;
int n;
int xh[N][N];
int c[N];
bool bfs(int t)
{
int i,j,w;
queue<int> q;
q.push(t);
while(!q.empty())
{
w=q.front();
q.pop();
ford(i,1,n)
{
if(w==i||xh[i][w]&&xh[w][i]) // 是w的朋友暂时不确定他属于哪个集合
continue;
if(c[i]==-1) //不是w的朋友肯定属于另一个集合
{
c[i]=c[w]^1;
q.push(i);
}
else if(c[i]==c[w]) // 不是w朋友但和他属于同一集合 矛盾
return true;
}
}
return false;
}
int main()
{
// freopen("input.txt","r",stdin);
while(~si1(n))
{
mset(xh,0);
ford(i,1,n)
{
int a;
while(si1(a)&&a)
xh[i][a]=1;
}
mset(c,-1);
bool flag=0;
ford(i,1,n)
{
if(c[i]==-1)
{
c[i]=0;
if(bfs(i))
{
flag=1;
break;
}
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
分享到:
相关推荐
hdu 3085 一道bfs题 一点值得注意的,鬼魅先走,mm与gg后走,直接模拟这个这个场景,突出时间的先后性,一秒与一秒的区别。。。这样模拟很难出错
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
ACM HDU题目分类,我自己总结的大概只有十来个吧
HDU最全ac代码
hdu 1166线段树代码
hdu动态规划算法集锦
hdu题目分类
自己做的HDU ACM已经AC的题目
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
HDU图论题目分类