"Tree structure" no root tree go to root tree __ personal records

Source: Internet
Author: User

	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.

Related Article

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.