HDU 4751 Divide Groups(判断是否为二分图)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int MAXN = 100 + 10;
struct Edge
{
    int to;
    int next;
}edge[MAXN*MAXN];
int head[MAXN];
int G[MAXN][MAXN];
int col[MAXN];
int tot;
void AddEdge(int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}
bool dfs(int u, int c)
{
    col[u] = c;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v = edge[i].to;
        if(col[v] != -1)
        {
            if(col[v] == c)
                return false;
        }
        else if(!dfs(v, col[u]^1))
            return false;
    }
    return true;
}
int main()
{
    int n;
    while(scanf("%d", &n)!=EOF && n)
    {
        memset(G, 0, sizeof(G));
        int t;
        FOR(i, 1, n)
        {
            while(scanf("%d", &t) && t)
            {
                G[i][t] = 1;
            }
        }
        init();
        FOR(i, 1, n)
        {
            FOR(j, i + 1, n)
            {
                if(G[i][j] == 0 || G[j][i] == 0)
                {
                    AddEdge(i, j);
                    AddEdge(j, i);
                }
            }
        }
        memset(col, -1, sizeof(col));
        int flag = 1;
        for(int i=1;i<=n;i++)
        {
            if(col[i] == -1 && dfs(i, 0) == false)
            {
                flag = 0;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

时间: 2024-10-17 07:03:49

HDU 4751 Divide Groups(判断是否为二分图)的相关文章

hdu 4751 Divide Groups 二分图

题意给出所有人之间的关系,单向的,问能不能将这群人分成两组,使得每组内部的任意两人都互相认识. 先把单向边都换成无向边,即如果a,b互相认识那么不变,如果只是单向边的话那么则认为他们两个不认识,然后假设能分成满足题意的两个集合,那么新图的补图中这两个集合内部是没有边的,所以只要判断补图是不是二分图即可. #include <cstdio> #include <cstring> #include <queue> using namespace std; int G[210

HDU 4751 Divide Groups

Divide Groups Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 475164-bit integer IO format: %I64d      Java class name: Main   This year is the 60th anniversary of NJUST, and to make the celebration more colo

hdu 4751 Divide Groups 2—sat问题 还是未理解

Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1443    Accepted Submission(s): 512 Problem Description This year is the 60th anniversary of NJUST, and to make the celebration mor

HDU 4751 Divide Groups(二分图的判断)

Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos. After carefully planning, Tom200 announced his activity plan, on

hdu 4751 Divide Groups(dfs染色 或 2-sat)

Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.   After carefully planning, Tom200 announced his activity plan

hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是赛后看的网上大神,所以转载过来了,dfs染色的时候很巧妙,巧妙的用到了就两个无向完全图 #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <qu

hdu 4751 2013南京赛区网络赛 二分图判断 **

和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace st

hdu 5971 Wrestling Match 判断能否构成二分图

http://acm.hdu.edu.cn/showproblem.php?pid=5971 Wrestling Match Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 25    Accepted Submission(s): 15 Problem Description Nowadays, at least one wrestli

hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2444 Description There are a group of students. Some of them may know