Ultraviolet A 10596 Morning Walk

Source: Internet
Author: User

UVA_10596

This is a pure Euler Loop problem. For more information about the theory, see Liu rujia's White Book P112. If this graph is connected and each vertex is connected only to an even number of links, then there must be an Euler loop. Therefore, we only need to judge whether the graph is connected and the number of links connected to each vertex.

At first, I didn't pay attention to the existence of R = 0. I re it many times, and when R = 0, I must output Not Possible, there is a path intersection, but there is no path. It is really strange ......

#include<stdio.h>
#include<string.h>
int dgr[210],p[210];
int find(int x)
{
return p[x]==x?x:(p[x]=find(p[x]));
}
int main()
{
int i,j,u,v,N,R,ok,num;
while(scanf("%d%d",&N,&R)!=EOF)
{
if(R==0)
{
printf("Not Possible\n");
continue;
}
memset(dgr,0,sizeof(dgr));
for(i=0;i<N;i++)
p[i]=i;
for(i=0;i<R;i++)
{
scanf("%d%d",&u,&v);
if(find(u)!=find(v))
p[find(u)]=find(v);
dgr[u]++;
dgr[v]++;
}
ok=1;
for(i=0;!dgr[i];i++);
for(j=i+1;j<N;j++)
if(dgr[j]&&find(i)!=find(j))
{
ok=0;
break;
}
num=0;
if(ok)
for(i=0;i<N;i++)
if(dgr[i]%2!=0)
num++;
if(!ok||num>0)
printf("Not Possible\n");
else
printf("Possible\n");
}
return 0;
}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.