Hdu 5423 Rikka with Tree (DFS)

Source: Internet
Author: User

problem Description
As we know, Rikka isPoor at math. Yuta isWorrying about ThisSituation, so he gives Rikka some math tasks to practice. There isOne of them:for a tree T, let F (t,i) be the distance between Vertice1and Vertice I. (the length of each edge is 1). Trees A and B are similiarifand onlyifThe same number of vertices and forEach I meet F (a,i) =F (b,i). Trees A and B are differentifand onlyifThey has different numbers of vertices or there exist an number I which vertice I had different fathersinchTree aand tree B when Vertice1  isRoot. Tree A isSpecialifand onlyifThere doesn't exist an tree B which A and B is different and A and B are similiar.Now he wants to knowifA tree isSpecial. It isToo difficult forRikka. Can you help her?

Input
 - testcases. For each testcase, the first line contains a number n (1≤n≤). Then n−1 lines follow. Each line contains the numbers u,v (1 is an edge between U and V.

Output
if  is " YES " " NO ".

Sample Input
3
1 2
2 3
4
1 2
2 3
1 4

Sample Output
YESNO


Hintfor the second testcase, this tree was similiar with the given tree:41 21 43 4

SourceBestcoder Round #53 (Div.2)

According to test instructions can be constructed out of the special tree has three cases (all with node 1 as the root), the first is a straight line, the second is a straight end of the flowering, the third is direct flowering. Then you can know for each depth of the points for 1,1,1,...,x,0,0, .... Then DFS can look it up again.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <Set>6#include <map>7#include <queue>8#include <algorithm>9 using namespacestd;Ten #defineN 1006 Onevector<int>G[n]; Avector<int>Deep[n]; - intVis[n]; - voidDfsintStintd) { thevis[st]=1; - Deep[d].push_back (ST); -      for(intI=0; I<g[st].size (); i++){ -         intu=G[st][i]; +         if(!Vis[u]) { -DFS (u,d+1); +         } A     } at } - intMain () - { -     intN; -      while(SCANF ("%d", &n) = =1){ -  in          for(intI=0; i<=n;i++) { - g[i].clear (); to deep[i].clear (); +         } -memset (Vis,0,sizeof(Vis)); the  *          for(intI=1; i<n;i++){ $             intu,v;Panax Notoginsengscanf"%d%d",&u,&v); - G[u].push_back (v); the g[v].push_back (u); +         } ADfs1,0); the  +  -         intt=0; $          while(deep[t].size () = =1) t++; $  -         intnum=deep[t].size (); -         if(num+t!=N) { theprintf"no\n"); -}Else{Wuyiprintf"yes\n"); the         } -          Wu     } -     return 0; About}
View Code

Hdu 5423 Rikka with Tree (DFS)

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.