L2-038 病毒溯源

#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

vector<int> g[N];
bool st[N];
int cnt = 0;
vector<int> res;
vector<int> temp;
int d = 0;

void dfs(int u, vector<int>& temp, int dep) {
    if (dep > d) {
        res.clear();
        d = dep;
        res = temp;
    }

    for (auto v : g[u]) {
        temp.push_back(v);
        dfs(v, temp, dep + 1);
        temp.pop_back();
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, S;
    cin >> n;
    map<int, int> pre;
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;
        int u = i;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            st[v] = true;
            g[u].push_back(v);
        }
        if (g[u].size()) sort(g[u].begin(), g[u].end());
    }
    for (int i = 0; i < n + 1; i++) {
        res.push_back(i);
    }

    int root = -1;
    for (int i = 0; i < n; i++) {
        if (!st[i]) {
            root = i;
            break;
        }
    }

    dfs(root, temp, 1);
    
    cout << res.size() + 1 << "\n";
    cout << root << " "[res.size() == 0];
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << " "[i == res.size() - 1];
    }

    return 0;
}
posted @ 2022-04-16 16:41  Xxaj5  阅读(115)  评论(0编辑  收藏  举报