题解(蒟蒻解法)

吴晨曦  •  13天前


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 005;
vector <int> adj[N];
int d, vis[N];

void dfs(int cur, int depth) {
	d = max(d, depth);
	for (auto p : adj[cur])
		if (!vis[p])
			dfs(p, depth + 1);
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x, y;
		cin >> x >> y;
		if (x == 0 and y == 0)
			continue;
		adj[i].push_back(x);
		adj[i].push_back(y);
	}
	dfs(1, 1);
	cout << d << endl;
	return 0;
} 

解析:

  • DFS可以解决
  • 有亿点难写
  • 用vector的adj来存即可
  • 一直往下搜,到叶子节点,记录每一步的深度。
  • code(不要抄袭):

评论: