Vijos 1460 Rally

Source: Internet
Author: User

Describe

After the motor show, the amusement park decided to hold a Grand Hill Road rally, mediocre and rhyme nature will also come to participate in the contest.

There are n connected timekeeping points on the field, n-1 the track (forming a tree). The height of each point is different (the parent node height must be greater than the child node), and the adjacent points are connected by the track. Because of insufficient horsepower, so rhyme of the remote control car can only from high to low. and rhyme of the car to run every track will take a certain amount of time.

The organizer has planned to hold a M-stage competition, each from the first U point to the first point of time, of course, there are many games rhyme of the remote control car is not able to participate (because to uphill). He wants to know how many races he can take, and he wants to know the total time he has to finish these stages.

The track is one-way.

Format input Format

The first row of two integers n,m.

Next n-1 Line 3 integers a, B, t for each line.

The remote control car that represents rhyme can spend t-seconds from the first point to the first B-timer.

Next m line 2 integers per line u, V, meaning as described in the description.

Output format

The first line outputs a positive integer representing the number of stages that can be attended.

The second line outputs a positive integer that indicates the total time.

Example 1 sample input 1[copy]
6 21 2 12 4 12 5 15 6 11 3 12 64 5
Sample output 1[Copy]
12
Limit

Each test point 1s

Tips

The height of the first chronograph point is the highest;
U≠v;
For 50% of data n≤1000 m≤1000;
For 100% of data n≤10000 m≤100000;
The answer is less than 2^64.

The two points of the LCA, but there is a realistic condition is that there must be a point between the two points is the ancestor of another point, or rhyme rhyme can not participate (according to test instructions), so judge on the line. Remember the answer open long long!!

CODE:

#include <iostream>#include<cstdio>#include<cstring>#defineREP (i, S, N) for (int i = s; i <= n; i + +)#defineRep_ (i, S, N) for (int i = n; i >= s; i-)#defineMax_n 10000 + 10using namespacestd;structnode{intV, W, next;} E[max_n<<1];intHead[max_n], top =0;voidAddintUintVintW) {e[+ + top].v = V; E[TOP].W = W; E[top].next = Head[u]; Head[u] =top;}intN, M, Deep[max_n], Sum[max_n], go[max_n][ -];voidDFS (intXintLast ) {     for(inti = head[x]; I i =E[i].next) {        if(E[I].V! =Last ) {go[e[i].v][0] = x; DEEP[E[I].V] = Deep[x] +1; SUM[E[I].V]= Sum[x] +E[I].W;        DFS (E[I].V, x); }    }}voidprework () {REP (k,1, the) REP (I,1, N) go[i][k]= Go[go[i][k-1]][k-1];}voidGoup (int&x,intk) {Rep_ (i,0, the){        if((1<< i) & k) x =Go[x][i]; }}intLCA (intXinty) {    if(Deep[x] > Deep[y]) goup (x, Deep[x]-Deep[y]); if(Deep[y] > Deep[x]) goup (y, Deep[y]-deep[x]); if(x = = y)returnx; Rep_ (i,0, the){        if(Go[x][i]! =Go[y][i]) x= Go[x][i], y =Go[y][i]; }     returngo[x][0];}BOOLUsed[max_n];intMain () {Freopen ("1.in","R", stdin); scanf ("%d%d", &n, &m); memset (Used,0,sizeof(used)); REP (i,1N1){        intU, V, W; scanf"%d%d%d", &u, &v, &W); Add (U, V, W);        Add (V, u, W); USED[V]=1; }        intR; REP (i,1, N)if(! Used[i]) R =i; intRET =0; Long Longres =0; DFS (R,0);    Prework (); REP (i,1, M) {        intU, v; scanf"%d%d", &u, &v); if(Lca (U, v) = = u) RET + +, res + = (Long Long) (Sum[v]-Sum[u]); } cout<< ret << Endl << Res <<Endl; return 0;}

Vijos 1460 Rally

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.