Cod sursa(job #1718297)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 17 iunie 2016 11:55:47
Problema Triplete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>

#define BUF_SIZE 16384
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a=0;
    char c=nextch();
    while((c<'0' || c>'9') && c!='-')
        c=nextch();
    int semn=1;
    if(c=='-'){
        semn=-1;
        c=nextch();
    }
    while('0'<=c && c<='9'){
        a=a*10+c-'0';
        c=nextch();
    }
    return a*semn;
}

unsigned char v[4096][512];
int nbit[256];
int main(){
    fi=fopen("triplete.in","r");
    fo=fopen("triplete.out","w");
    for(int i=1;i<256;i++){
        nbit[i]=nbit[i/2]+i%2;
    }
    int n=nextnum(), m=nextnum();
    for(int i=0;i<m;i++){
        int x=nextnum(), y=nextnum();
        x--;
        y--;
        v[x][y/8]+=(1<<(y%8));
        v[y][x/8]+=(1<<(x%8));
    }
    int s=0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++){
            if(v[i][j/8]&(1<<(j%8))){
                for(int k=0;k<512;k++)
                    s=s+nbit[v[i][k]&v[j][k]];
            }
        }

    fprintf(fo,"%d", s/3);
    fclose(fi);
    fclose(fo);
    return 0;
}