Cod sursa(job #694958)

Utilizator vladstoickvladstoick vladstoick Data 28 februarie 2012 09:39:04
Problema Triplete Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include<fstream>
using namespace std;
ifstream in("triplete.in");
ofstream out("triplete.out");
int n , m , x , y ;
unsigned int a[4100][32];
long long biti(unsigned int x)
{
    long long rez=0;
    while(x)
    {
        rez++;
        x=x&(x-1);
    }
    return rez;
}

void scrie()
{
    for(int i=1 ; i<=n ; i++)
    {
        for(int j=1 ; j<=n ; j++)
            if(a[i][j/32] &  (1<<(j%32)))
                out << "1\t";
            else
                out << "0\t";
        out << "\n";
    }
}

int main()
{
    in>>n>>m;
    for(int i=1;i<=m;i++)
    {
        in>>x>>y;
        a[x][y/32] |= 1<<(y%32);
        a[y][x/32] |= 1<<(x%32);
    }
    long long rez=0;
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
            if(a[i][j/32]&(1<<(j%32)))
            {
                for(int k=0;k*32<n;k++)
                    rez =rez+ biti( a[i][k] & a[j][k] );
            }
    out<<rez/3;
    return 0;
}