Enter the number of nodes with no root tree N, enter n-1 (U, v), enter the number of the root you want to specify root, create a tree with root (//variable length array) a MAXN row at that time each row length can be different to represent the relationship between nodes in the tree * #include <stdio.h> #include <stdlib.h> #include <string.h> #i
Nclude<vector> #include <queue> using namespace std;
const int MAXN = 1000; Vector<int> G[MAXN];
Variable-length array int n, root, P[MAXN] in STL;
void Dfs (int u, int fa)//recursively converts a subtree with u as root, and U's father is fa {int d = g[u].size ();
for (int i = 0; i < D; i++) {Const int& v = g[u][i];
if (v!= FA) Dfs (V, p[v] = u);
int main () {int u, v;
scanf ("%d", &n);
for (int i = 0; i < n-1 ++i) {scanf ("%d%d", &u, &v);
G[u].push_back (v);
G[v].push_back (U);
} scanf ("%d", &root);
P[root] =-1;
DFS (Root,-1);
for (int i = 0; i < n; ++i) printf ("p[%d] =%d\n", I, p[i]);
return 0;
}
/* Output format p[the node] = the node father such as p[5] = 1;
The father of node 5 is 1; When the node is the root node, the corresponding is-1 * *
vector_ Baidu Translation
Vector |
[English]ˈvektə (R) |
[Mei]ˈvɛktɚ |
N. |
N. A course of a vector; [Health] carriers; [Days] Vector path |
Vt. |
to guide by radio; Navigation |
Vector is part of the C + + Standard Template Library, which is occasionally translated as "container" but not accurate. It is a versatile template class and function library that can manipulate a variety of data structures and algorithms. Vector is considered a container because it can hold various types of objects like a container, simply put, vector is a dynamic array of any type that can be stored to add and compress data.