`
jishublog
  • 浏览: 869218 次
文章分类
社区版块
存档分类
最新评论

HDU 4751 Divide Groups (BFS)

阅读更多

传送门: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;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics