Triangle on the tree to solve re Runtime Error

Source: Internet
Author: User
Tags integer first row time limit

Time limit: 4000ms

Single Point time: 2000ms

Memory Limit: 256MB

Describe

There is a tree, there is a caterpillar in the tree. It has lived in this tree for a long time and knows its structure well. So it has always been the shortest way in the tree, will not go a detour. It also loves triangles especially, so when it crawls up and down the tree, it always thinks, if you saw the branches/trunks that had just climbed, could you choose three from them to spell a triangle?

Input

The first row of the input data contains an integer T that represents the number of data groups.
Next there is the T-group data, each set of data:
The first row contains an integer N that represents the number of nodes (from 1 to N) on the tree.
The next N-1 line contains three integers a, B, Len, which indicates that there is a branch/trunk length of Len between Node A and Node B.
The next line contains an integer M representing the number of inquiries.
The next m line is two integers per line s, T, which means the caterpillar crawls from S to T, asking if the branches/trunks in the distance can be spelled into triangles.

1≤t≤5
Small data: 1≤n≤100, 1≤m≤100, 1≤len≤10000
Big data: 1≤n≤100000,1≤m≤100000, 1≤len≤1000000000

Output

For each set of data, first output a line of "case#x:" Where X is the number of data sets, starting at 1.
Next, for each query output row, contains "Yes" or "No" to indicate whether a triangle can be spelled.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Sample input

2

5

1 2 5

1 3 20

2 4 30

4 5 15

2

3 4

3 5

5

1 4 32

2 3 100

3 5 45

4 5 60

2

1 4

1 3

Sample output

Case #1:

No

Yes

Case #2:

No

Yes

This problem was done yesterday always reported re, that is, run-time error runtime errors, because it is the test system does not allow the creation of multiple classes, even if the internal class is not, the way is to rewrite the form of the number of groups.

Add: It's OK to create multiple classes, this can't be a heap overflow or any other problem, hopefully not to mislead everyone. The end result is tle, still need to improve.

Ideas:

1, read the data, build the diagram

2, breadth first traversal, find the destination point. This process is definitely the shortest way, because this is a tree, two points between only one path (without duplication). In the traversal process, save the parent node of each point traversal, so that you can know which nodes have gone through.

3, traversing any of the three edges of the combination can be composed of triangles.

I created some test data myself and felt that large data should also be available and not timed out. Only the last step seems to be less efficient, in fact, some articles say that when the number of sides more than 50 o'clock, must be able to form a triangle, so in fact it does not traverse so many times to return.

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.