Catch—-二分圖的判定與奇環的判定

來源:互聯網
上載者:User

題目:http://acm.hdu.edu.cn/showproblem.php?pid=3478

註:
二分圖的判定(染色法)

二分圖與奇環的關係:

如果一圖是二分圖,那麼它一定沒有奇環。如果一圖沒有奇環的話,那麼它可以是二分圖。

原始碼:

#include <stdio.h>#include <string.h>#define N 120000#define M 1020000typedef  struct{    int v,nxt;}node;node e[M];int head[N],f[N],d[N];int n,m,s,id,u,v;int flag,cas,t;void add(int u,int v){    e[id].v=v;    e[id].nxt=head[u];    head[u]=id;    id++;    e[id].v=u;    e[id].nxt=head[v];    head[v]=id;    id++;}int dfs(int i)    //白色  -1  黑色  1{   int se,u;   if(flag==0)  return 0;   for(int x=head[i];x!=0;x=e[x].nxt)   {      u=e[x].v;      se=-f[i];      if(f[u]!=0)      {          if(se!=f[u])  {  flag =0;  return 0;}      }      else      {          f[u]=se;          dfs(u);      }   }   return 1;}int main(){    //freopen("D:\\a.txt","r",stdin);    scanf("%d",&cas);    t=0;    while(t<cas)    {      t++;      scanf("%d %d %d",&n,&m,&s);      for(int i=0;i<n;i++)  head[i]=0;      for(int i=1;i<=2*m;i++)   e[i].nxt=0;      id=1;      memset(d,0,sizeof(d));       for(int i=0;i<m;i++)       {           scanf("%d %d",&u,&v);           d[u]++;  d[v]++;           add(u,v);       }       flag=0;      for(int i=0;i<n;i++)       if(d[i]==0)       {           printf("Case %d: NO\n",t);           flag=1;           break;       }       if(flag)  continue;       memset(f,0,sizeof(f));       flag=1;   f[s]=1;       dfs(s);       if(flag==0)  printf("Case %d: YES\n",t);       else   printf("Case %d: NO\n",t);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.