sgu 172


二分图判断。我用DFS做的。

【话说俄罗斯小学生都这么爽,无力吐槽】


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>

#define V(x) mp[x].v
#define Next(x) mp[x].next
#define Trans(x) (3-x)

const int MAXN = 205, MAXM = 30005;

int n , m;
struct edeg{int v,next;}mp[MAXM<<1] = {0};
int head[MAXN] = {0} , el = 0;
bool hash[MAXN] = {0};
int color[MAXN] = {0};
int cnt[3] = {0};

void newedge(const int &u, const int &v)
{
    ++el;V(el) = v;  
    Next(el) = head[u];
    head[u] = el; 
}

bool dfs(const int x,const int clo)
{
    hash[x] = true;color[x] = clo;
    cnt[clo] ++;

    for(int i = head[x]; i ; i = Next(i))
    {
       int p = V(i);

       if(hash[p] == false)
        {
          if(dfs(p,Trans(clo))==false)return false;
         }  
       else  
        {
          if(color[x] == color[p]) return false;
        }
    }

    return true;
}

int main()
{
    bool ans ;

#ifndef ONLINE_JUDGE    
    freopen("sgu172.in","r",stdin);
    freopen("sgu172.out","w",stdout);
#endif

    scanf("%d%d",&n,&m);

    for(int i = 1 ; i <= m; i ++)
    {
        int a , b; scanf("%d%d",&a,&b);

        newedge(a,b); newedge(b,a);
    }

    ans = true;

    for(int i = 1 ; i <= n && ans; i++)
      if(hash[i] == false)
         ans = dfs(i , 1);

    if(ans == true)
    {
        puts("yes");printf("%d\n",cnt[1]);
        int i;
        for(i = 1; i <= n; i++)
          if(color[i] == 1)printf("%d ",i);
    }
    else
      puts("no");

#ifndef ONLINE_JUDGE
    fclose(stdin);  
    fclose(stdout);
#endif
    return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值