AIZU ONLINE JUDGE Aizu ALDS1_11_B Depth First Search Solution
Problem Link : https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
bool vis[N];
vector<int> adj[N];
int tim, ind[N], outd[N];
void dfs(int node) {
vis[node] = 1;
ind[node] = tim++;
for (int i = 0; i < adj[node].size(); i++) {
int child;
child = adj[node][i];
if (!vis[child]) dfs(child);
}
outd[node] = tim++;
}
int main() {
int n;
tim = 1;
cin >> n;
for (int i = 0; i < n; i++) {
int u, k;
cin >> u >> k;
for (int j = 0; j < k; j++) {
int v;
cin >> v;
adj[u].push_back(v);
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= n; i++)
cout << i << " " << ind[i] << " " << outd[i] << endl;
}
No comments