迭代dfs
作者:
lazy_go
,
2022-04-13 20:34:15
,
所有人可见
,
阅读 180
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;
const int N = 1e2 + 10;
int globalTime;
enum Color{
WHITE, GRAY, BLACK
};
Color color[N];
int discoverTime[N], finishTime[N];
vector<vector<int> > adj;
void dfs(int u){
stack<int> s;
s.push(u);
globalTime ++;
discoverTime[u] = globalTime;
color[u] = GRAY;
while(!s.empty()){
int v = s.top();
bool end = true;
for(int j : adj[v]){
if(color[j] == WHITE){
s.push(j);
globalTime ++;
discoverTime[j] = globalTime;
color[j] = GRAY;
end = false;
break;
}else {
//check BE
}
}
if(end){
globalTime ++;
finishTime[v] = globalTime;
color[v] = BLACK;
s.pop();
}
}
}
int main(){
int n;
scanf("%d", &n);
adj.resize(n+1);
for(int i = 1; i <= n; i ++){
int u, k, x;
scanf("%d%d", &u, &k);
adj[u].resize(k);
for(int j = 0; j < k; j ++){
scanf("%d", &adj[u][j]);
}
}
for(int i = 1; i <= n; i ++){
sort(adj[i].begin(), adj[i].end());
}
for(int i = 1; i <= n; i ++){
if(color[i] == WHITE){
dfs(i);
}
}
for(int i = 1; i <= n; i ++){
printf("%d %d %d\n", i, discoverTime[i], finishTime[i]);
}
return 0;
}