Uva_000020
This question is a bit greedy, that is, if a point has a lot of degrees, it should be prioritized and connected to more vertices.
I first estimated the qsort time and thought it was troublesome, but later the boss said that he had used qsort before, so I started to write qsort. It may be because the pruning is used properly or the number of data on the subject is small.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a[10010],n,N,sum;
int cmp(const void *_p,const void *_q)
{
int *p=(int *)_p;
int *q=(int *)_q;
return *p-*q;
}
int judge()
{
int i,j,k,t;
qsort(a,n,sizeof(a[0]),cmp);
for(k=n-1;k>0;k--)
if(a[k])
{
for(i=k-1;i>=0;i--)
if(a[i])
{
a[i]--;
a[k]--;
sum-=2;
if(!a[k])
break;
}
if(a[k])
break;
qsort(a,k,sizeof(a[0]),cmp);
}
if(sum==0)
return 1;
else
return 0;
}
int main()
{
int i,j,k,max;
while(1)
{
scanf("%d",&N);
if(N==0)
break;
n=max=sum=0;
for(i=0;i<N;i++)
{
scanf("%d",&a[n]);
if(a[n])
{
if(a[n]>max)
max=a[n];
sum+=a[n];
n++;
}
}
if(sum==0)
{
printf("Possible\n");
continue;
}
else if(max>=n||sum%2!=0)
{
printf("Not possible\n");
continue;
}
if(judge())
printf("Possible\n");
else
printf("Not possible\n");
}
return 0;
}