Ural 1069. Prufer Code

Source: Internet
Author: User

1069. Prufer Codetime limit:0.25 Second
Memory Limit:8 MBA tree (i.e. a connected graph without cycles) with vertices is given ( N≥2). Vertices of the tree is numbered by the integers 1,..., N. A prufer code for the tree was built as follows:a leaf (a vertex that's incident to the only edge) with a minimal number is taken. Then this vertex and the incident edge is removed from the graph, and the number of the vertex that is adjacent to the L EAF is written down. In the obtained graph once again a leaf with a minimal number are taken, removed and this procedure is repeated until the O nly Vertex is left. It is clear, the only vertex and the vertex with the number N. The written down set of integers ( N−1 numbers, each of a range from 1 to N) is called a Prufer codeof the graph. Your task is, given a prufer code, to reconstruct a tree, and i.e. to find out the adjacency lists for every vertex in the GRA Ph. Assume that 2≤ N≤7500inputa set of numbers corresponding to a prufer code of some tree. The numbers is separated with a spaces and/or line breaks. Outputadjacency lists for each vertex. format:a vertex number, colon, numbers of adjacent vertices separated with a space. The vertices inside lists and lists itself should is sorted by vertex number in a ascending order (look at sample output) . Sample
input Output
2 1 6) 2 6
1:4 62:3 5 63:24:15:26:1 2
problem Author:Magaz Asanov
problem Source:Ural State univerisity Personal Contest Online February ' 2001 Students Session Tags:Graph theory()difficulty:522 Test instructions: Give Prufer code, ask to find out the original tree. Analysis: It's easy to know after Prufercode.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineMIT (2147483647) + #defineINF (1000000001) A #defineMLL (1000000000000000001LL) at #defineSZ (x) ((int) (x). Size ()) - #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front - #definePub push_back - #definePOF Pop_front - #definePOB pop_back in #defineFT first - #defineSD Second to #defineMk Make_pair +  -InlineintGetint () the { *     intRet =0; $     CharCh =' ';Panax Notoginseng     BOOLFlag =0; -      while(! (Ch >='0'&& Ch <='9')) the     { +         if(Ch = ='-') Flag ^=1; ACh =GetChar (); the     } +      while(Ch >='0'&& Ch <='9') -     { $RET = RET *Ten+ Ch-'0'; $Ch =GetChar (); -     } -     returnFlag? -Ret:ret; the } - Wuyi Const intN =7510; the intN, Arr[n]; - intCnt[n]; Wupriority_queue<int>que; -vector<int>Ans[n]; About  $InlinevoidInput () - { -     intx; -      while(Cin >>x) A     { +arr[n++] =x; thecnt[x]++; -     } $n++; the } the  theInlinevoidSolve () the { -      for(inti =0; I < n; i++) in         if(!cnt[i +1]) Que.push (-(i +1)); the      for(intStep =1; Step <= N-1; step++) the     { About         intx =-que.top (), now = Arr[step-1]; the Que.pop (); the         if(! (--cnt[now])) Que.push (-Now ); the ans[now].pub (x); + ans[x].pub (now); -     } the Bayi      for(inti =1; I <= N; i++) the     { theprintf"%d:", i); - sort (Ans[i].begin (), Ans[i].end ()); -         intLength =sz (ans[i]); the          for(intj =0; J < length; J + +) theprintf"%d", Ans[i][j]); theprintf"\ n"); the     } - } the  the intMain () the {94Freopen ("a.in","R", stdin); the Input (); the Solve (); the     return 0;98}
View Code

Ural 1069. Prufer Code

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.