Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
DFS
#include <iostream> using namespace std; int n; int graph[1003][1003], visited[1003]; void dfs(int u){ int i; visited[u] = 1; cout << u << " "; for(i = 0; i < n; i++){ if ( graph[u][i] == 1 and !visited[i]){ dfs(i); } } } int main() { int i, j, u, par_of_u; //take number of objects in tree cin>>n; //initialise the graph as zero matrix of n*n for(i=0; i<n; i++) visited[i] = 0; for(i=0; i<n; i++) for(j=0; j<n; j++) graph[i][j] = 0; //take input //n objects => (n-1) objects ka parent hoga //because root ka parent nahi hai for(i=0; i<n-1; i++){ cin >> u >> par_of_u; //this means 'par_of_u' ke childs mei 'u' bhi ek child hai //graph[3][7] = 1 means 3 is parent of 7 //graph[3][7] = 0 means 3 is not a parent of 7 graph[par_of_u][u] = 1; } //assume 0 is root dfs(0); return 0; }
run
|
edit
|
history
|
help
1
A string-integer comparison trick
stackLinkedlist
My First Wall
segmented sieve
fundamental type sizes
Depth of Bin tree
completed
Splitwise Problem - 1
410
Hii