Problem: Example

Input

4 2 1 1
1 2
3
4
3
1 2
2 4
3 4


Output

2 1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e8;

int main() {
    int N, A[3];
    cin >> N >> A[0] >> A[1] >> A[2];
    vector<int> idx(N);
    iota(idx.begin(), idx.end(), 0);
    int repr[3] = {};
    for(int i=0; i<3; ++i) {
        vector<int> tmp(A[i]);
        for(int j=0; j<A[i]; ++j) {
            cin >> tmp[j];
            tmp[j]--;
        }
        sort(tmp.begin(), tmp.end());
        repr[i] = tmp[0];
        for(int j=1; j<A[i]; ++j) {
            idx[tmp[j]] = tmp[0];
        }
    }
    int M;
    cin >> M;
    vector<vector<int>> g(N);
    for(int i=0; i<M; ++i) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        g[idx[x]].push_back(idx[y]);
        g[idx[y]].push_back(idx[x]);
    }
    vector<vector<int>> d(3, vector<int>(N, INF)), mi(3, vector<int>(N));
    for(int i=0; i<3; ++i) {
        iota(mi[i].begin(), mi[i].end(), 0);
    }
    for(int i=0; i<3; ++i) {
        int s = repr[i];
        d[i][s] = 0;
        mi[i][s] = s;
        queue<int> que;
        que.push(s);
        while(!que.empty()) {
            int v = que.front();
            que.pop();
            for(auto to : g[v]) {
                if(d[i][to] >= d[i][v] + 1) {
                    mi[i][to] = min(mi[i][to], mi[i][v]);
                }
                if(d[i][to] == INF) {
                    que.push(to);
                    d[i][to] = d[i][v] + 1;
                }
            }
        }
    }
    vector<int> d2(N), mi2(N, INF);
    int resd = INF, resid = INF;
    for(int i=0; i<N; ++i) {
        for(int j=0; j<3; ++j) {
            d2[i] += d[j][i];
            mi2[i] = min(mi2[i], mi[j][i]);
        }
        if(resd > d2[i]) {
            resd = d2[i];
            resid = mi2[i];
        }
        if(resd == d2[i]) {
            resid = min(resid, mi2[i]);
        }
    }
    cout << resd << ' ' << resid+1 << endl;
}