zoukankan      html  css  js  c++  java
  • L2-3 深入虎穴 (25分)

    L2-3 深入虎穴 (25分)

    建树、找根、找最深的叶子节点

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<map>
    #include<queue>
    #include<iomanip>
    using namespace std;
    #define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);
    
    
    const int N = 100000 + 10;
    int h[N], e[N*100], ne[N*100], idx;
    int du[N];
    void add(int a, int b)
    {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
    
    int ans = 1, de = 0;
    void dfs(int u, int d)
    {
        if (d > de) {
            ans = u, de = d;
        }
        for (int i = h[u]; ~i; i = ne[i])
        {
            int j = e[i];
            dfs(j, d + 1);
        }
    }
    int main()
    {
        STDIN
        int n; 
        cin >> n;
        memset(h, -1, sizeof h);
        add(0,1);
        for (int i = 1; i <= n; i++)
        {
            int k; scanf("%d", &k);
            int x;
            for (int j = 1; j<= k; j++)
            {
                scanf("%d", &x);
                add(i, x);
                du[x]++;
            }
        }
        int ru;
        for (int i = 1; i <= n; i++) {
            if (!du[i])
            {
                ru = i;
                break;
            }
        }
        dfs(ru, 0);
        cout << ans << endl;
    }
  • 相关阅读:
    bzoj3946
    bzoj3065
    bzoj1500
    bzoj1233
    bzoj1089
    bzoj1087
    bzoj1086
    if语句之猜拳
    if语句判断闰年、平年
    if语句判断身高体重是否标准
  • 原文地址:https://www.cnblogs.com/hulian425/p/14031913.html
Copyright © 2011-2022 走看看