Created
February 16, 2017 13:21
-
-
Save shawon100/11f5098d0d1c003a0e640b11fd05a0f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| vector<int>edges[1000]; | |
| queue<int>q; | |
| int color[1000],visited[1000],tn; | |
| int bipartite(int s) | |
| { | |
| int j,k,fr; | |
| memset(color,-1,sizeof color); | |
| color[s]=1; | |
| while(!q.empty()) | |
| { | |
| q.pop(); | |
| } | |
| q.push(s); | |
| for(j=1;j<=tn;j++) | |
| { | |
| visited[j]=0; | |
| } | |
| visited[s]=1; | |
| while(!q.empty()) | |
| { | |
| fr=q.front(); | |
| //cout<<"Size Of "<<fr<<"="<<edges[fr].size()<<endl; | |
| for(k=0;k<edges[fr].size();k++) | |
| { | |
| if(visited[edges[fr][k]]==0 && color[edges[fr][k]]==-1) | |
| { | |
| q.push(edges[fr][k]); | |
| visited[edges[fr][k]]=1; | |
| color[edges[fr][k]]=1-color[fr]; | |
| } | |
| } | |
| for(k=0;k<edges[fr].size();k++) | |
| { | |
| if(visited[edges[fr][k]]==1 && color[edges[fr][k]]==color[fr]) | |
| { | |
| return false; | |
| } | |
| } | |
| q.pop(); | |
| } | |
| return true; | |
| } | |
| int main() | |
| { | |
| int i,e,p,n,u,v,f,m,t,y; | |
| //freopen("11396.txt","r",stdin); | |
| //freopen("11396out.txt","w",stdout); | |
| while(1) | |
| { | |
| cin>>tn; | |
| e=(tn*3)/2; | |
| if(tn==0) | |
| { | |
| break; | |
| } | |
| memset(edges,0,sizeof edges); | |
| while(1) | |
| { | |
| cin>>u>>v; | |
| if(u==0 && v==0) | |
| { | |
| break; | |
| } | |
| edges[u].push_back(v); | |
| edges[v].push_back(u); | |
| } | |
| if(bipartite(1)) | |
| { | |
| cout<<"YES"<<endl; | |
| } | |
| else | |
| { | |
| cout<<"NO"<<endl; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment