Any door: http://codevs.cn/problem/2370/
Title Description
Description
The small computer room has a tree of trees, there are n nodes, the node labeled 0 to N-1, there are two worms named fluttering dogs and dogs, separated on two different nodes. One day, they want to climb a node to get a base, but as two worms, they don't want to spend too much energy. It is known that crawling from a node to its father's node will cost C energy (crawling from the Father node to the same node), they want to find a way to spend the most energy, so that when the foundation of energy, they find you want you to design a program to find this road, ask you to tell them at least how much energy to spend
Enter a description
Input Description
The first line is an n, and the next n-1 line has three integers u,v, c. Indicates that node u climbs to Node v takes the effort of C.
Line n+1 has an integer m indicating an m inquiry. The next m line has two integers per row, and V indicates the node where two bugs are located.
Output description
Output Description
There is a total of M lines, one integer for each line, indicating the shortest distance to be drawn for that inquiry.
Sample input
Sample Input
3
1 0 1
2 0 1
3
1 0
2 0
1 2
Sample output
Sample Output
1
1
2
Data range and Tips
Data Size & Hint
1<=n<=50000, 1<=m<=75000, 0<=c<=1000
Test Instructions Summary:
One thing to note is that the information given to the edge is given to the child node and then to the Father node.
Problem Solving Ideas:
DFS preprocessing the distance from each node to the root node of the tree (if a double-sided edge is to be weighed)
Tarjan find the nearest public ancestor, the old routine (if you build two-way side to judge weight)
TIP:
These topics should pay attention to find root node (when building one-way side), if it is not clear who is the child node who is the Father node, built two-way side, the operation of the sentence can be weighed.
AC Code:
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <vector>6 #defineINF 0x3f3f3f3f7 #defineLL Long Long8 using namespacestd;9 Const intMax_n = 5e4+5;Ten Const intMax_m =8e4; One structedge{intV, W, NXT;} edge[max_n<<1]; A structQuery - { - intV, id; the Query () {}; -Query (int_v,int_id): V (_v), id (_id) {}; - }; -Vector<query>Q[max_n]; + - intHead[max_n], CNT; + intDis[max_n]; A intFa[max_n]; at BOOLVis[max_n]; - BOOL inch[Max_n]; - intAns[max_m]; - intN, M; - - voidInit () in { -Memset (Head,-1,sizeof(head)); tomemset (DIS,0,sizeof(DIS)); +memset (Vis,false,sizeof(Vis)); -Memsetinch,false,sizeof(inch)); thememset (ans,0,sizeof(ans)); *CNT =0; $ }Panax Notoginseng - voidAddedge (int from,intTo,intweight) the { +EDGE[CNT].V =to ; AEDGE[CNT].W =weight; theEDGE[CNT].NXT = head[ from]; +head[ from] = cnt++; - } $ $ intGETFA (intx) {returnFA[X]==X?X:FA[X] =GETFA (fa[x]);} - /* - int GETFA (int x)//Find Ancestors the { - int root = x;Wuyi while (Fa[root]! = root) root = Fa[root]; the - int tmp; Wu While (fa[x]! = root) { - tmp = fa[x]; About fa[x] = root; $ x = tmp; - } - return root; - } A */ + voidDfsintSintf) the { - for(inti = Head[s]; I! =-1; i =edge[i].nxt) { $ //if (edge[i].v = = f) Continue; //If we build two-way side to go heavy theDIS[EDGE[I].V] = Dis[s] +EDGE[I].W; the DFS (EDGE[I].V, s); the } the } - in voidTarjan (intSintf) the { the intRoot =s; AboutFa[s] =s; the for(inti = Head[s]; I! =-1; i =edge[i].nxt) { the intEiv =edge[i].v; the //if (Eiv = = f) Continue; //If we build two-way side to go heavy + Tarjan (Eiv, root); -FA[GETFA (EIV)] =s; the }BayiVis[s] =true; the for(inti =0; I < q[s].size (); i++){ the if(VIS[Q[S][I].V] &&!Ans[q[s][i].id]) { -Ans[q[s][i].id] = dis[q[s][i].v] + dis[s]-2*DIS[GETFA (Q[S][I].V)]; - } the } the } the the intMain () - { the init (); thescanf"%d", &N); the for(inti =1, U, V, W; i < N; i++){94 //scanf ("%d%d%d", &u, &v, &w); //build two-way edge thescanf" %d%d%d", &v, &u, &W); the Addedge (U, V, W); the //Addedge (V, U, W); //build two-way edge98 inch[V] =true; About } -scanf"%d", &M);101 for(inti =1, u, v; I <= M; i++){102scanf"%d%d", &u, &v);103 Q[u].push_back (Query (v, i));104 Q[v].push_back (Query (U, i)); the }106 107 intRoot =0;108 for(inti =0; i < N; i++)if(!inch[i]) {root = i; Break;}109 theDFS (Root,-1);111Tarjan (Root,-1); the 113 for(inti =1; I <= M; i++){ theprintf"%d\n", Ans[i]); the } the return 0;117}
Codevs 2370 Small-room tree