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 Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » May Challenge 2012  » Pruning Trees

Pruning Trees

Problem code: PRUNING

  • All Submissions

All submissions for this problem are available.

You have a tree (an acyclic, connected graph) with edge weights, but it is far too big. You are to prune the tree by deleting some edges so that the following property holds. For each connected component C of the pruned tree, there is some node rC such that for every node v in C, the total weight of the edges on the path between v and rC is at most some given value d.

You happen to like heavy trees so you want to delete the minimum possible total edge weight to satisfy this property.

Input

The first line of each test case is an integer T ≤ 40 indicating the number of test cases to follow. The first line of each test case contains two integers n and d that indicate there are n nodes in the tree and d is as specified in the problem description. Suppose the nodes in the tree are indexed by integers between 0 and n-1. Then n-1 lines follow, each containing three integers u,v, and w. This indicates that there is an edge of weight w between nodes u and v. The input will be such that the graph is connected with no cycles. No edge will be given more than once and no edge in the input will be a loop (i.e. u ≠ v for each input edge).

Bounds: 1 ≤ n ≤ 100, 0 ≤ d ≤ 109, and, for each edge u,v,w in the input, 0 ≤ u < v ≤ n-1 and 1 ≤ w ≤ 107.

Output

The output for each test case consists of a single line containing a single integer denoting the maximum total weight of edges that can remain after deleting some edges so that the remaining graph satisfies the property described in the problem description.

Example

Input:
3
5 10
0 1 10
1 2 9
1 3 10
3 4 9
10 1
0 1 1
0 2 1
1 3 1
1 4 1
2 5 1
2 6 1
2 7 1
6 8 1
6 9 1
7 2
0 1 1
0 2 2
1 3 1
1 4 2
2 5 2
2 6 7

Output:
29
7
7


Author: friggstad
Tester: pieguy
Editorial http://discuss.codechef.com/problems/PRUNING
Date Added: 7-01-2011
Time Limit: 5 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, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Can someone explain the tree

sak3t @ 1 May 2011 11:15 PM

Can someone explain the tree structure more please...

totally confused about what we have to do..

Can someone clear the problem

k0stia @ 2 May 2011 08:10 PM

Can someone clear the problem ?

For instance, the first example. Why we got the 29 as answer ?

 

There exists a subgraph of the given tree which has vertices V={0,1,2,3,4}, and edges E={(1,2), (3,4)}, and satisfies the given conditions. Obviously, to get such subgraph we need to remove two edges (0,1) and (1,3) with sum cost 20. Thus, the answer should be 20. Where is the mistake ?

@Kostia: You have to output

theluy @ 2 May 2011 08:32 PM

@Kostia: You have to output the "maximum total weight of edges that can remain after deleting some edges [...]", not the weight of the deleted edges.

There is something that i'm

gareve @ 5 May 2011 08:14 AM

There is something that i'm missing:

In the first example we can prune:

a) (1,3)

But the total weight from 0 to 2 its 19 > 10

or

b) (1,2)

But the total weight from 0 to 4 its 29 > 10

Both cases are the only prunes that i see to have a total weight of 29.

What i'm missing?

Read the condition you have

triplem @ 5 May 2011 08:34 AM

Read the condition you have to satisfy after pruning again. Just because the distance between 0 and 2 after pruning (1,3) is more than 10 doesn't mean that isn't valid.

can anyone expnain that how

anshuiiita @ 5 May 2011 10:44 AM

can anyone expnain that how the answer of 2nd and 3rd test case is 7?

Stephen, will u please

anshuiiita @ 5 May 2011 05:26 PM

Stephen, will u please explain how dose the output of 2nd or 3rd case is 7.

@anshuman for both cases

demo @ 8 May 2011 09:56 PM

@anshuman

for both cases delete edge 0-1 and 2-6

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

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.