Cod sursa(job #1217553)

Utilizator cojocarugabiReality cojocarugabi Data 7 august 2014 18:29:53
Problema Triplete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
ifstream fi("triplete.in");
ofstream fo("triplete.out");
#define nmax 4100
typedef struct node{int x;node *next;} *nod;
nod S[nmax];
string C;
int main(void)
{
    int n,m;
    nod p;
    for (fi>>n>>m,getline(fi,C);m--;)
    {
       int x=0,y=0;
       getline(fi,C);
       int A=0,B=C.length();
       while ('0'<=C[A] && C[A]<='9') x=x*10+C[A]-'0',++A;
       ++A;
       while ('0'<=C[A] && C[A]<='9' && A<B) y=y*10+C[A]-'0',++A;
       if (x>y) swap(x,y);
       p=new node;
       p->x=y;p->next=S[x];S[x]=p;
    }
    unsigned int R=0;
    for (int i=1;i<=n;++i)
        for (nod j=S[i];j;j=j->next)
            for (nod a=j->next;a;a=a->next)
    {
        int x=j->x,y=a->x;
        if (x>y) swap(x,y);
        for (nod p=S[x];p;p=p->next)
        if (p->x==y) {++R;break;}
    }
    fo<<R<<"\n";
    return 0;
}