Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Wednesday, February 19, 2014

VFRIENDS-Very Friends

Very Friends

below given code is for vfriends spoj or very friends spoj.
this is problem of graph in which degree of undirected graph is given u have to check whether there will exits a simple graph or not.
for this u have to check
1-> sum of all degree is even.
2-> it should satisfy following condition.
\sum_{i=1}^{k}d_i \leq k(k-1) + \sum_{i=k+1}^n  \min(d_i,k) \quad \text{for } k \in \{1,\dots,n\} \, .
this theorem was given by  Erdős–Gallai theorem  and proof was given by Havel (1955)
for more details read this

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
 int t;
 scanf("%d".&t);
 while(t--)
 {
  int n,i,j;
  scanf("%d",&n);
  int a[n+9];
  int *sum=(int *)calloc(n+9,sizeof(int));
  int temp=n,flag=1;
  long long s=0,count=0,ksum=0;
  for(i=n;i>=1;i--)
  {
   scanf("%d",&a[i]);
   s=s+a[i];
  }
  if(s%2==0)
  {
   sum[0]=0;
   flag=1;
   for(i=1;i<=n;i++)
    sum[i]=sum[i-1]+a[i];
   for(i=1;i<=n;i++)
   {
    ksum=0;
    for(j=i+1;j<=n;j++)
     ksum+=min(a[j],i);
    if( sum[i] > (i*(i-1) + ksum))
    {
     flag=0;
     break;
    }
   }
   if(flag)
    printf("HAPPY\n");
   else
    printf("SAD\n");
  }
  else
   printf("SAD\n");
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us