Cod sursa(job #1742243)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 15 august 2016 23:57:25
Problema Triplete Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>
#define bits(x) __builtin_popcount(x)
using namespace std;

const int MMAX = 66000, //:O
          NMAX = 4100,
             B = 16;

struct PII {
    int x, y;

    inline PII() { }
    inline PII(int _x, int _y) {
        x = _x;
        y = _y;
    }
};

class hpstream {
private:
    static const int BMAX = 1<<17;

    FILE *file;
    int   buff;
    char  str[BMAX];

    inline char nextch(void) {
        if(buff==BMAX) {
            fread(str, 1, BMAX, file);
            buff = 0;
        }
        return str[buff++];
    }

public:
    hpstream() {}
    hpstream(const char *str) {
        file = fopen(str, "r");
        buff = BMAX;
    }

    inline hpstream &operator>> (int &arg) {
        char ch;

        arg = 0;

        while(!isdigit(ch=nextch()));

        arg=ch-'0';
        while(isdigit((ch=nextch())))
            arg=arg*10+ch-'0';

        return *this;
    }

    void close(void) {
        fclose(file);
    }
};

int g[NMAX][NMAX/B];
PII e[MMAX];

int main(void) {
    hpstream fs("triplete.in");
    ofstream gs("triplete.out");
    int n, m, ant;

    ant = 0; ///(Antwort)

    fs>>n>>m;
    for(int i=1; i<=m; ++i) {
        fs>>e[i].x>>e[i].y;

        if(e[i].x < e[i].y)
            swap(e[i].x, e[i].y);

        g[e[i].x][e[i].y/B] |= 1 << (e[i].y % B);
    }

    for(int i=1; i<=m; ++i)
    for(int j=0; j<=n/B; ++j)
        ant+=bits(g[e[i].x][j]&g[e[i].y][j]);

    gs<<ant<<'\n';

    fs.close();
    gs.close();
    return 0;
}