Good morning, I'm trying to write a program to print the Euler Tour order on a tree.
But the code isn't successful, is there any problem regarding the way I input data, many many thanks.
We assume the root of the tree is node 0.
Input Format
The first line is n, which is the size of tree .
The following n lines are information of children of nodes
The i-th line represents the child nodes of node i, 0<=i<=n.
The first integer,c , in i-th line represents number of children of node i , and the remaining c integers represents the children of node i.
Constraints
0<=n<=10000
Sample Input
6
1 1
2 2 3
0
2 4 5
0
0
Sample Output
Euler tour: 0 1 2 2 3 4 4 5 5 3 1 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <bits/stdc++.h>
using namespace std;
#define MAX 10000
vector<int> adj[MAX];
int vis[MAX];
int Euler[2 * MAX];
void add_edge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void eulerTree(int u, int &index)
{
vis[u] = 1;
Euler[index++] = u;
for (auto it : adj[u]) {
if (!vis[it]) {
eulerTree(it, index);
Euler[index++] = u;
}
}
}
void printEulerTour(int root, int N)
{
int index = 0;
eulerTree(root, index);
for (int i = 0; i < (2*N-1); i++)
cout << Euler[i] << " ";
}
int main()
{
int N;
cin>>N;
while(N--)
{
int i,node=0;
cin>>i;
while(i--)
{ int u; cin>>u;
add_edge(node, u);}
node++;
}
cout<<"Euler tour:"<<"\n";
printEulerTour(0, N);
return 0;
}
|