CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » September 2010 Challenge » Trees Again

Trees Again

Problem code: TREES

  • All Submissions

All submissions for this problem are available.

Given a tree, you need to count how many subtrees exist such that the length of the longest path in the subtree is at most K.

Input :

The first line contains the number of test cases T. T test cases follow. For each test case, the first line contains N and K. The following N - 1 lines contain two integers ai and bi, indicating an edge between nodes ai and bi in the tree. There is a blank line after each test case.

Output :

Output T lines, one corresponding to each test case, containing the required answer.

Sample Input :
2
3 1
0 1
1 2

6 3
0 1
1 2
2 3
2 4
3 5

Sample Output :
5
23

Constraints :
1 <= T <= 2000
2 <= N <= 60
0 <= ai,bi < N
1 <= K <= N - 1

Author: syco
Date Added: 20-08-2010
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

can i assume, by the very

gopi6sigma @ 1 Sep 2010 08:05 PM

can i assume, by the very definition of tree, the tree as well as subtrees are connected .. ??

Thank u

The examples don't make

dogbox @ 1 Sep 2010 08:25 PM

The examples don't make sense. The number of subtrees cannot exceed the number of nodes (excluding the empty subtree). You have exactly one subtree rooted at each node. For the first example, there are only 3 subtrees (including the original tree) with maximum path lengths of 2, 1, and 0, which should yield a result of 2, rather than 5.

@scott subtrees with max path

gopi6sigma @ 1 Sep 2010 09:37 PM

@scott

subtrees with max path length 0 are 0, 1, 2

subtrees with max path length 1 are {0,1} and {1,2}

If that's the case, I think a

dogbox @ 1 Sep 2010 09:49 PM

If that's the case, I think a better term would be subgraph, rather than subtree. {0}, {1}, and {0, 1} shouldn't be considered subtrees.

@Scott :

syco @ 2 Sep 2010 12:38 AM

@Scott : http://mathworld.wolfram.com/Subtree.html

Is a test case like:   3 1 0

gopi6sigma @ 2 Sep 2010 09:40 AM

Is a test case like:

 

3 1

0 1

2 1

i.e, a disconnected one ?? Hope a tree is at least weakly connected.

who can explain the output

liubiaoyong @ 2 Sep 2010 12:08 PM

who can explain the output for the second test case?

I can get only 20 subtrees.

0,1,2,3,4,5,

01,12,23,24,35,

012,123,124,235,324,

0123,0124,1235,4235

 

1234, 01234, 12345

pdwd @ 2 Sep 2010 02:46 PM

1234, 01234, 12345

A directed tree is

gopi6sigma @ 2 Sep 2010 08:47 PM

A directed tree is a directed graph which would be a tree if the directions on the edges were ignored. Some authors restrict the phrase to the case where the edges are all directed towards a particular vertex, or all directed away from a particular vertex (see arborescence).

 

Can we assume the first part of the definition .. ??

How many subtrees should i

gopi6sigma @ 2 Sep 2010 09:49 PM

How many subtrees should i get for the following test case:  ( Tell if this case is valid )

 

3 1

0 1

2 1

 

is it {0}, {1}, {2}, {0,1}, {2,1}, {0,1,2}

 

should {0, 1, 2} included ??  please help

  @ govardhan reddy your

v_new.c @ 2 Sep 2010 11:11 PM

 

@ govardhan reddy

your example does not satisfy the property of tree

Thanks for ur reply atul So I

gopi6sigma @ 3 Sep 2010 01:37 AM

Thanks for ur reply atul

So I can assume, all the nodes are reachable from at least one node of the tree. Can I assume that node as '0'   ??

@liubiaoyong: the cases u

ashish.pant @ 3 Sep 2010 01:52 AM

@liubiaoyong: the cases u have presented are only paths. Ofcourse they are subtrees, but u have missed few other subtrees too. like 1234, 12345, 01234. in these subtrees too the longest path doesnt exceed 3.

@ govardhan reddy you don't

v_new.c @ 3 Sep 2010 06:23 AM

@ govardhan reddy

you don't have to assume the root node of given tree it's given in the edge list.

According to wiki, "a tree is

burdakovd @ 3 Sep 2010 03:58 PM

According to wiki, "a tree is an undirected graph in which any two vertices are connected by exactly one simple path.".

 

But you say that input

3 1

0 1

2 1

 

does not satisfy the property of tree.

 

So are trees in this problem directed or undirected?

(

for example for input

3 1

0 1

0 2

 

will be subtree {0, 1, 2} included in answer? (for directed tree it will be (max path length is 1), for undirected - won't (max path length will be 2))

)

 

If they are directed, is this statement correct for author's definition of tree: "the edges are all directed towards a particular vertex, or all directed away from a particular vertex (see arborescence)." ?


So final questions about

burdakovd @ 3 Sep 2010 06:26 PM

So final questions about problem statement:

 

1) Are tree and subtrees connected (at least weakly)?

2) Are trees directed or undirected?

3) Can we assume that tree contains vertex v, such that for any other vertex u, there is exactly one directed path from v to u? (Arborescence)

 

Examples of input in problem statement don't help in any of these questions.


Trees are undirected as

pdwd @ 3 Sep 2010 07:56 PM

Trees are undirected as problem says.

@Burkadov 1) The subtree has

anshuman_singh @ 3 Sep 2010 08:42 PM

@Burkadov

1) The subtree has to be connected by definition.

2) Undirected.

3) Yes.

@Anshuman: please tell if

ashish.pant @ 3 Sep 2010 10:07 PM

@Anshuman: please tell if this is a valid input or not.

3 1

0 1

2 1

It was mentioned above that

burdakovd @ 4 Sep 2010 12:46 AM

It was mentioned above that input

3 1

0 1

2 1

"does not satisfy the property of tree".

 

Can author comment on it?

 

@Burdakov Daniel It satisfies

balakrishnan_v @ 4 Sep 2010 01:14 AM

@Burdakov Daniel

It satisfies the properties of a tree!

Codechef Can categorize the

theeporithirumugam @ 4 Sep 2010 02:34 AM

Codechef Can categorize the problems based on difficulty level ,so that beginners can spend time on easy and medium problems ,,Just rating the problem will help us

@radhakrishnan After contest,

tec @ 4 Sep 2010 08:13 AM

@radhakrishnan

After contest, the problems will be put into different problemset categorized by difficulty.

To practice, go to the practice section, and choose the difficulty you want.

But during the contest, I think it is part of the challenge to recognized the difficulty of the problem.

  Is a tree itself considered

moshiur @ 5 Sep 2010 12:31 AM

 

Is a tree itself considered a subtree of it ?

trees... again... :D

comco @ 5 Sep 2010 12:47 AM

trees... again... :D

@Moshiur Yes. Subtree is

anshuman_singh @ 5 Sep 2010 09:01 AM

@Moshiur Yes. Subtree is basically a connected subset of edges from the tree.

can anybody tell me how we

sumit_lnmiit @ 6 Sep 2010 12:54 PM

can anybody tell me how we can down our time limit??

2nd test case not

idontknowisitim @ 7 Sep 2010 02:41 AM

2nd test case not clear

@Przemyslaw

as o/p sugg by u include 01234, 12345

how it can b valid as max length for 2nd test case is 3

and 01234, 12345 gives length of 4

plz anybody clarify

No, those two subtrees have a

triplem @ 7 Sep 2010 04:04 AM

No, those two subtrees have a maximum path length of 3. What path do you think has a length of 4?

Is 0 the root of the

shadow @ 7 Sep 2010 01:52 PM

Is 0 the root of the tree??

If not then what is?

The tree is not considered

triplem @ 7 Sep 2010 04:54 PM

The tree is not considered rooted.

Thanks Stephen Just one more

shadow @ 7 Sep 2010 05:05 PM

Thanks Stephen

Just one more thing does it means that the subgraph {3,2,4} in second example has maximum length of 2??

Yes, because path 3-2-4 has

triplem @ 8 Sep 2010 02:47 AM

Yes, because path 3-2-4 has length 2.

@stephen path 12345 has

idontknowisitim @ 9 Sep 2010 07:29 PM

@stephen

path 12345 has length 5

how it is 3

plz clarify

12345 is not a path. There

triplem @ 10 Sep 2010 02:55 AM

12345 is not a path. There isn't any edge from 3 to 4 or 4 to 5. If there were, it would be a path of length 4.

Do we have to print a modulo

utkarsh_lath @ 11 Sep 2010 11:53 AM

Do we have to print a modulo or will the answer always fit into integer?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com