Divide Groups HDU - 4751(二分图染色)

Divide Groups HDU - 4751

题意:

有n个人,第 i i i 个人认识一些人(可能互相认识)
现在要你设计两场活动,使尽可能多的人互相认识

思路:

  1. 显然是二分图染色。
  2. 本题有一个条件,就是已经认识的,就无关紧要了

AC

#include <iostream>
#include <vector>
#include <cstring>
#define pb push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define For(i,x,y) for(int i=(x); i<=(y); i++)
using namespace std;
const int maxn=100+10;
vector<int>e[maxn];
int g[maxn][maxn];
int color[maxn],n;
int fl=0;
void ans(int a){
    if(a)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
bool dfs(int u){
    For(v,1,n){//for(int i=0; i<e[u].size(); i++){
        //int v=e[u][i];
        if(v==u||g[u][v]&&g[v][u])continue;
        if(color[u]==color[v])return false;
        if(!color[v]){
            color[v]=3-color[u];
            if(!dfs(v))return false;
        }
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(0);
    while(cin>>n){
        mst(g,0);
        mst(color,0);fl=1;
        int v;
        For(u,1,n){
            //e[u].clear();
            while(cin>>v,v)g[u][v]=1;//e[u].pb(v);
        }
        For(i,1,n){
            if(!color[i]){
                color[i]=1;
                if(dfs(i))continue;
                fl=0;
                break;
            }
        }
        ans(fl);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值