Cod sursa(job #1554188)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 21 decembrie 2015 00:14:52
Problema Triplete Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <bitset>
#include <utility>

using namespace std;

class InputReader {
    public:
        InputReader() {}
        InputReader(const char *file_name) {
            input_file = fopen(file_name, "r");
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
        inline InputReader &operator >>(int &n) {
            while(buffer[cursor] < '0' || buffer[cursor] > '9') {
                advance();
            }
            n = 0;
            while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
                n = n * 10 + buffer[cursor] - '0';
                advance();
            }
            return *this;
        }
    private:
        FILE *input_file;
        static const int SIZE = 1 << 17;
        int cursor;
        char buffer[SIZE];
        inline void advance() {
            ++ cursor;
            if(cursor == SIZE) {
                cursor = 0;
                fread(buffer, SIZE, 1, input_file);
            }
        }
};

const int NMAX = 4136;
const int MMAX = 65605;
const int BASE = 31;

int n;
int graph[NMAX][NMAX / BASE];
pair <int, int> edges[MMAX];

inline int intersect(int a, int b) {
    int ans = 0;
    for (int i = 0; i <= n; ++ i)
        ans += __builtin_popcount(graph[a][i] & graph[b][i]);
    return ans;
}

int modulo[NMAX];

int main()
{
    InputReader cin("triplete.in");
    ofstream cout("triplete.out");

    int m = 0;
    cin >> n >> m;

    for (int i = 0; i < BASE; ++ i)
        modulo[i] = i;
    for (int i = BASE; i < n; ++ i)
        modulo[i] = modulo[i - BASE];

    n = (n - 1) / BASE; //Numarul de grupe
    for (int i = 1; i <= m; ++ i) {
        cin >> edges[i].first >> edges[i].second;
        -- edges[i].first;
        -- edges[i].second;

        graph[edges[i].first][edges[i].second / BASE] |= (1 << modulo[edges[i].second % BASE]);
        graph[edges[i].second][edges[i].first / BASE] |= (1 << modulo[edges[i].first % BASE]);
    }

    long long int ans = 0;
    for (int i = 1; i <= m; ++ i)
        ans += intersect(edges[i].first, edges[i].second);

    cout << ans / 3 << '\n';

    //cin.close();
    cout.close();
    return 0;
}