L2-3 深入虎穴 (25分)
阿新 • • 發佈:2020-11-24
建樹、找根、找最深的葉子節點
#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); constint 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; }