Cod sursa(job #1483650)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 9 septembrie 2015 18:08:44
Problema Triplete Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<fstream>
#define x first
#define y second
using namespace std;
int n, m, i, j;
long long nr, nr1;
pair<int, int> p[66000];
short a[4100][4100 / 16 + 2], aux;
int v[(1 << 16) + 2];
ifstream fin("triplete.in");
ofstream fout("triplete.out");
int main(){
    fin>> n >> m;
    for(i = 1; i <= m; i++){
        fin>> p[i].x >> p[i].y;
        p[i].x --; p[i].y --;
        if(p[i].x > p[i].y){
            swap(p[i].x, p[i].y);
        }
        a[p[i].x][p[i].y / 16] += (1 << (p[i].y % 16));
    }
    for(i = 1; i < (1 << 16); i++){
        v[i] = v[i / 2] + i % 2;
    }
    for(i = 1; i <= m; i++){
        nr1 = 0;
        for(j = 0; j <= n / 16 + 1; j++){
            aux = (a[p[i].x][j] & a[p[i].y][j]);
            nr1 += v[aux];
        }
        nr += nr1;
    }
    fout<< nr <<"\n";
    return 0;
}