116632024-11-04 11:46:55MagyarKendeSZLGFertőzési sorozat (50 pont)cpp17Accepted 50/5018ms632 KiB
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
    cin.tie(0), ios::sync_with_stdio(0);

    int N, M, K;
    cin >> N >> M >> K;

    vector<int> a(K);
    for (int& x : a) cin >> x;
    set<int> aSet(a.begin(), a.end());

    vector<vector<int>> g(N + 1);
    while (M--) {
        int U, V;
        cin >> U >> V;
        g[U].push_back(V);
        g[V].push_back(U);
    }

    vector<int> result;
    for (int start = 1; start <= N; start++) {
        vector<int> vis(N + 1);
        vis[start] = 1;
        queue<int> q({start});

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int v : g[u]) {
                if (!vis[v]) {
                    vis[v] = vis[u] + 1;
                    q.push(v);
                }
            }
        }

        vector<int> order(K);
        set<int> orderSet;
        for (int i = 0; i < K; i++) {
            orderSet.insert(order[i] = vis[a[i]]);
        }

        bool ok = 1;
        for (int i = 1; i < K; i++) {
            int d = order[i] - order[i - 1];
            ok = ok && (d == 0 || d == 1);
        }

        if (!ok) continue;

        if (orderSet.size() <= 2) {
            if (ok) { result.push_back(start); }
            continue;
        }

        orderSet.erase(orderSet.begin());
        orderSet.erase(*orderSet.rbegin());

        for (int u = 1; u <= N; u++) {
            ok = ok && !(!aSet.count(u) &&
                         orderSet.count(vis[u]));
        }

        if (ok) { result.push_back(start); }
    }

    cout << result.size() << "\n";
    for (int x : result) cout << x << " ";
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms508 KiB
2Accepted0/01ms320 KiB
3Accepted0/03ms320 KiB
4Accepted2/21ms320 KiB
5Accepted2/21ms320 KiB
6Accepted2/23ms320 KiB
7Accepted2/23ms320 KiB
8Accepted2/23ms572 KiB
9Accepted2/24ms632 KiB
10Accepted2/210ms464 KiB
11Accepted1/11ms320 KiB
12Accepted2/24ms320 KiB
13Accepted2/24ms456 KiB
14Accepted2/23ms320 KiB
15Accepted2/24ms320 KiB
16Accepted2/24ms456 KiB
17Accepted2/23ms320 KiB
18Accepted1/14ms508 KiB
19Accepted1/14ms392 KiB
20Accepted1/13ms320 KiB
21Accepted1/112ms468 KiB
22Accepted1/112ms500 KiB
23Accepted1/19ms460 KiB
24Accepted1/19ms464 KiB
25Accepted1/18ms320 KiB
26Accepted1/110ms320 KiB
27Accepted1/112ms464 KiB
28Accepted1/18ms500 KiB
29Accepted1/110ms460 KiB
30Accepted1/18ms464 KiB
31Accepted1/18ms428 KiB
32Accepted1/19ms320 KiB
33Accepted1/118ms320 KiB
34Accepted1/112ms320 KiB
35Accepted1/112ms468 KiB
36Accepted1/110ms512 KiB
37Accepted1/110ms320 KiB
38Accepted1/112ms468 KiB
39Accepted1/19ms508 KiB
40Accepted1/114ms472 KiB