L2-031 深入虎穴

并没有说根是谁

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;

const int N = 1E5 + 10;
vector<int> g[N];
int depth[N];
bool flag[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    //for (int i = 1; i <= n; i++) g[100001].push_back(i);
    for (int u = 1; u <= n; u++) {
        int k;
        cin >> k;
        for (int i = 0; i < k; i++) {
            int v;
            cin >> v;
            g[u].push_back(v);
            flag[v] = true;            
        }
    }
    int root = 0;
    for (int i = 1; i <= n; i++) {
        if (!flag[i]) {
            root = i;
            break;
        }
    }
    depth[root] = 0;
    queue<int> q;
    q.push(root);
    while (q.size()) {
        int t = q.front();
        q.pop();

        for (auto v : g[t]) {
            depth[v] = depth[t] + 1;
            q.push(v);
        }
    }

    int mxnum = 1, mxdist = 0;
    for (int i = 1; i <= n; i++) {
        //cout << dist[i] << "\n";
        if (depth[i] > mxdist) {
            mxdist = depth[i];
            mxnum = i;
        }
    }

    cout << mxnum << "\n";

    return 0;
}
posted @ 2022-04-13 11:33  Xxaj5  阅读(36)  评论(0编辑  收藏  举报