Cod sursa(job #1742247)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 16 august 2016 00:06:17
Problema Triplete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>
#define bits(x) __builtin_popcount(x)
using namespace std;

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

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],
    x[MMAX],
    y[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>>x[i]>>y[i];

        if(x[i] > y[i])
            swap(x[i], y[i]);

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

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

    gs<<ant<<'\n';

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