二分圖判定

來源:互聯網
上載者:User

標籤:

 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #include<map> 8 #include<stack> 9 #include<queue>10 #include<vector>11 using namespace std;12 #define clr(x,y)    memset(x,y,sizeof(x))13 #define sqr(x)      ((x)*(x))14 #define rep(i,a,b)  for(int i=(a);i<=(b);i++)15 #define LL          long long16 #define INF         0x3f3f3f3f17 #define A           first18 #define B           second19 const int N=1000;20 vector<int>G[N];21 22 int n,m,color[N];23 void init()24 {25     clr(color,0);26 }27 28 bool dfs(int v,int c)29 {30     color[v]=c;31     for(int i=0;i<G[v].size();i++) {32         if(color[G[v][i]]==c) return false;33         if(!color[G[v][i]] && !dfs(G[v][i],-c)) return false;34     }35     return true;36 }37 38 void solve()39 {40      for(int i=0;i<n;i++) {41         if(!color[i]){42             if(!dfs(i,1)) {43                 printf("NO\n");44                 return;45             }46         }47     }48     printf("YES\n");49 }50 51 52 int main()53 {54     int x,y;55     56     init(); 57     scanf("%d%d",&n,&m);58     for(int i=0;i<m;i++) {59         scanf("%d%d",&x,&y);60         G[x].push_back(y);    61     }62     63     solve();64     65     return 0;66 }

 

二分圖判定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.