Cod sursa(job #8454)

Utilizator georgianaGane Andreea georgiana Data 24 ianuarie 2007 19:52:53
Problema Triplete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct list
{
	int nod;
	struct list *urm;
};

int n,m,i,j,x,h[4097];
struct list *v[4097];

int main()
{
    freopen("triplete.in","r",stdin);
    freopen("triplete.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (int k=1;k<=n;k++) v[k]=NULL;
    for (int k=0;k<m;k++)
    {
        scanf("%d %d",&i,&j);
        if (i>j) x=i,i=j,j=x;
      	struct list *p;
        p=(struct list*) malloc(sizeof(struct list));
        p->urm=v[i];
        p->nod=j;
        v[i]=p;
    }
    x=0;
    for (int i=1;i<=n;i++)
    {
        memset(h,0,sizeof(h));
        struct list *p;
        p=v[i];
        while (p!=NULL) h[p->nod]=1,p=p->urm;
        p=v[i];
        while (p!=NULL)
        {
            struct list *q;
            q=v[p->nod];
            while (q!=NULL)
            {
                if (h[q->nod]==1) x++;
                q=q->urm;
            }
            p=p->urm;
        }
    }
    printf("%d\n",x);
    fclose(stdout);
    return 0;
}