DFS 0分4MLE求助
查看原帖
DFS 0分4MLE求助
1016101
xiaobeichen楼主2024/9/11 13:01
#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int N=1e7+5;
vector<LL>tree[N];
LL Max=-1;
void dfs(LL u,LL f,LL dep){
	Max=max(Max,dep);
	for(LL v:tree[u]){
		if(v==f||v==u||u==f)continue;
		dfs(v,u,dep+1);
	}
}
int main(){
	LL n;
	cin>>n;
	for(LL i=1;i<=n;i++){
		LL v1,v2;
		cin>>v1>>v2;
		tree[i].push_back(v1);
		tree[i].push_back(v2);
	}
	dfs(1,0,0);
	cout<<Max;
	return 0;
} 
2024/9/11 13:01
加载中...