sgu 172 分类: sgu 2015-03-08 17:34 35人阅读 评论(0) 收藏


二分图判断。我用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;   
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-03-08 17:34  <Dash>  阅读(159)  评论(0编辑  收藏  举报