2010 Liaoning Province Nbut 1218 "DFS implementation tree traversal and update"

Source: Internet
Author: User

  • [1218] You is my brother
  • Time limit: Ms Memory limit: 131072 K
  • Links: nbut 1218
  • Description of the problem
  • Input
  • There is multiple test cases.
    The first line have a single integer, N (n<=1000). The next n lines has integers a and B (1<=a,b<=2000) each, indicating B is the father of a. One person has exactly one father, of course. Little A is numbered 1 and Little B is numbered 2.
    Proceed to the end of file.
  • Output
  • For each test case, if Little B is Little A's younger, print "You are my younger". Otherwise, if Little B is Little A's elder, print "You Are my Elder". Otherwise, print "You Are my brother". The output for each test case is occupied exactly one line.
  • Sample input
  • 51 32 43 54 65 661 32 43 54 65 76 7
  • Sample output
  • You are my elderyou is my brother

Test Instructions:

Given N -times input, each time a row a,b, indicating b is a father. Finally , the relationship between node 1 and node 2 "is the comparison depth".

N <=1000 , 1<=a,b<=2000

Analysis:

Test instructions very clear, I use a value for each nodeRanksaves the node's depth, so each time the nodeamerging the tree of the root node into a nodebas the root node of the tree above, I'm going to put the slave nodeaand NodeaThe following nodes are allRankvalues are added to the nodebof theRankvalues. In this way, a struct holds the information for each node, each time it merges two trees.DFSupdating these nodes isOKup.

Code implementation:
#include <cmath> #include <cstdio> #include <string> #include <cstring> #include <iostream > #include <algorithm>using namespace std; #define FIN freopen ("Input.txt", "R", stdin) struct node{in    T Rank; Vector<int> Son; nodes[2000 + 5];void dfs (int pos, const int& val) {Nodes[pos].    Rank + = val; int num = Nodes[pos].    Son.size ();    if (num = = 0) return; for (int i = 0; i < num; i++) {int &np = Nodes[pos].        Son[i];    DFS (NP, Val); }}void Init () {for (int i = 1; i <=; i++) {nodes[i].        Rank = 1; Nodes[i].    Son.clear ();    }}int Main () {//FIN;    int n, a, B;        while (~SCANF ("%d", &n)) {init ();            for (int i = 1; I <= n; i++) {scanf ("%d%d", &a, &b); NODES[B].            Son.push_back (a); DFS (A, nodes[b].        Rank); } if (Nodes[1]. Rank = = Nodes[2].        Rank) printf ("You Are my brother\n"); else if (nodes[1]. Rank > Nodes[2].        Rank) printf ("You Are my elder\n");    else printf ("You Are my younger\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2010 Liaoning Province Nbut 1218 "DFS implementation tree traversal and update"

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.