uva 11396 二分图判定 (每条边连接的点不在同一范围内)

来源:互联网 发布:好搜刷排名软件 编辑:程序博客网 时间:2024/06/07 03:45
#include<cstdio>#include<cstring>using namespace std;struct node{int to,next;}e[10000]; int head[10000],cnt,vis[100000];void add_edge(int from,int to){e[cnt].to=to;e[cnt].next=head[from];head[from]=cnt++;}int dfs(int u,int v,int flag){vis[u]=flag;for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;if(to==v)continue;if(vis[to]!=-1){if(vis[to]==flag)return 0;continue;}if(!dfs(to,u,flag^1))return 0; }return 1;}int main(){int a,b,n;    while(scanf("%d",&n) == 1 && n){    cnt=0;    memset(head,-1,sizeof(head));    memset(vis,-1,sizeof(vis));    while(scanf("%d%d",&a,&b) == 2 && (a+b)){    add_edge(a,b);    add_edge(b,a);}if(dfs(1,-1,1))printf("YES\n");elseprintf("NO\n");}}

原创粉丝点击