HDU 4751 Divide Groups 二分图判断

题目描述:

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, one that contains two characters:
1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one’s energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.

Input
The input contains several test cases, terminated by EOF.
Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.

Output
If divided successfully, please output “YES” in a line, else output “NO”.

Sample Input

3
3 0
1 0
1 2 0 

Sample Output

YES 

题目分析:

从1到n有n个人,都有人认识他(她),这些认识他(她)的人就是第i+1行的那些人(0说明下面没有别的人认识他(她)了)。
要把所有不相互认识的人分开,是否可行?
这题是要把不互相认识的人分开,所以在把输入的存入一个图之后,要把那些不互相认识的人建边,进行二分图判断。
注意:这题因为把不认识的人建边之后不能保证是连通图,所以不能从1点往下找,而要把每个点都找一遍。
近似的模板题。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 210;

struct node
{
    int next;
    int to;
}edge[MAXN*MAXN];
int n,m;
int col[MAXN];

int id;
int head[MAXN];
void init()
{
    id=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v)
{
    edge[id].to=v;
    edge[id].next=head[u];
    head[u]=id++;
}

bool colour(int u)//双色法判断是否是二分图
{
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if (col[v]==-1)//连结的v结点没有被填色
        {
            col[v]=!col[u];//填上与u结点相反颜色
            if (!colour(v)) return false;//不能在v结点后完成填色
        }
        else if (col[v]==col[u]) return false;//u与v同色
    }
    return true;
}


bool mp[MAXN][MAXN];
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        memset(mp,0,sizeof(mp));
        for(int i=1; i<=n; i++)
        {
            int t;
            while(~scanf("%d",&t) && (t!=0))
            {
                mp[i][t]=1;
            }
        }
        for(int i=1; i<=n; i++)
          for(int j=i+1; j<=n; j++)
             if (!mp[i][j] || !mp[j][i])//不互相认识的人才能建边
             {
                 addedge(i,j);
                 addedge(j,i);
             }
        bool flag=1;
        for(int i=1; i<=n; i++)
        {
            memset(col,-1,sizeof(col));//初始没有被填色
            col[i]=1;//给i点涂色
            if (!colour(i)) flag=0;
        }
        if (flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值