[Tree structure] No root tree to a root tree, no root tree to a root tree

Source: Internet
Author: User

[Tree structure] No root tree to a root tree, no root tree to a root tree

// Enter n number of nodes in the rootless tree, n-1 edge (u, v), and root number of the root to be specified, create a root-root tree/* variable-length array. An array with the length of each row of maxn rows can be used to indicate the relationship between nodes in the tree */# include <stdio. h> # include <stdlib. h> # include <string. h >#include <vector >#include <queue> using namespace std; const int maxn = 1000; vector <int> G [maxn]; // int n, root, p [maxn]; void dfs (int u, int fa) in STL, 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", & 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 ;} /* the output format is p [this node] = this node's father, for example, p [5] = 1; node 5's father is 1; if the node is the root node, the corresponding value is-1 */
Vector_ Baidu Translation
Vector [English] incluvekt variance (r) [Us] invalid v LIMIT kt limit
N. Vector; heading; [generate] fungal carrier; [Day] vector diameter
Vt. Use a radio to guide... Navigation


Vector is part of the C ++ standard template library. Chinese characters are occasionally translated as "containers", but they are not accurate. It is a versatile template class and function library that can operate on multiple data structures and algorithms. Vector is considered a container because it can store various types of objects like a container. Simply put, vector is a dynamic array that can store any type, data can be added and compressed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.