Cerinta completa

You are given unweighted, undirected graphs, , , and , with vertices each, where the graph has edges and the vertices in each graph are numbered from through . Find the number of ordered triples , where , , such that there is an edge in , an edge in , and an edge in .

Input Format

The first line contains single integer, , denoting the number of vertices in the graphs. The subsequent lines define , , and . Each graph is defined as follows:

  1. The first line contains an integer, , describing the number of edges in the graph being defined.
  2. Each line of the subsequent lines (where ) contains space-separated integers describing the respective nodes, and connected by edge .

Constraints

  • , and
  • Each graph contains no cycles and any pair of directly connected nodes is connected by a maximum of edge.

Output Format

Print a single integer denoting the number of distinct triples as described in the Problem Statement above.

Sample Input

3
2
1 2
2 3
3
1 2
1 3
2 3
2
1 3
2 3

Sample Output

3

Explanation

There are three possible triples in our Sample Input:

Thus, we print as our output.


Limbajul de programare folosit: cpp14

Cod:

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <iterator>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

inline bool isdiff(int a, int b, int c) {
    return a != b && a != c && b != c;
}

int main() {
    int n;
    while(~scanf("%d", &n)) {
        vector<vpii> gall(n);
        vector<vi> g[3];
        rep(k, 3) {
            g[k].assign(n, vi());
            int m;
            scanf("%d", &m);
            for(int i = 0; i < m; ++ i) {
                int u, v;
                scanf("%d%d", &u, &v), -- u, -- v;
                gall[u].push_back(mp(v, k));
                gall[v].push_back(mp(u, k));
                g[k][u].push_back(v);
                g[k][v].push_back(u);
            }
            rep(i, n)
                sort(all(g[k][i]));
        }
        rep(i, n)
            sort(all(gall[i]));
        vi tmp;
        int ans = 0;
        rep(i, n) for(int j : g[0][i]) if(i != j) {
            const vi &v = g[1][i], &w = g[2][j];
            tmp.clear();
            set_intersection(all(v), all(w), back_inserter<vi>(tmp));
            for(int k : tmp)
                ans += k != i && k != j;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464650109

Link challenge: https://www.hackerrank.com/challenges/tripartite-matching/problem

Tripartite Matching