Advertisement
bradd123

UVa-11396

Sep 4th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. /* UVa 11396 using bipartite graph check*/
  2.  
  3. #include<cstdio>
  4. #include<queue>
  5. using namespace std;
  6. vector<int> AdjList[510];
  7. int main()
  8. {
  9.     //freopen("in.txt","r",stdin);
  10.     int n;
  11.     while(scanf("%d",&n)&& n){
  12.         for(int i=0;i<510;i++) AdjList[i].clear();
  13.         int u,v;
  14.         while(scanf("%d %d",&u,&v)&& (u!=0 || v!=0)){
  15.             AdjList[u].push_back(v);
  16.             AdjList[v].push_back(u);
  17.         }
  18.         queue<int> q;q.push(1);
  19.         vector<int> color(n,500);color[1]=0;
  20.         bool isBipartite=true;
  21.         while(!q.empty()&isBipartite){
  22.             int u=q.front();q.pop();
  23.             int sz=AdjList[u].size();
  24.             for(int j=0;j<sz;j++){
  25.                 int v=AdjList[u][j];
  26.                 if(color[v]==500){
  27.                     color[v]=1-color[u];
  28.                     q.push(v);
  29.                 }
  30.                 else if(color[v]==color[u]){
  31.                     isBipartite=false;break;
  32.                 }
  33.             }
  34.         }
  35.         if(isBipartite) printf("YES\n");
  36.         else printf("NO\n");
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement