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;
}