#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<int>G[maxn];
int in[maxn],out[maxn],d[maxn];
int tot = 0;
void dfs(int x,int pre,int deep){
in[x] = ++tot;
d[x] = deep;
for(int i = 0;i < G[x].size();i++){
int v = G[x][i];
if(v != pre) dfs(v,x,deep + 1);
}
out[x] = tot;
}
int main(){
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;i++){
int x,y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1,0,0);
for(int i = 1;i <= n;i++){
printf("%d %d %d\n",i,in[i],out[i]);//节点i的子树的编号
}
}
dfs序
最新推荐文章于 2025-05-01 15:41:04 发布