gym-100814d Frozen Rivers

Source: Internet
Author: User
Tags time 0

In winter, all small rivers of Al-asi great river in Syria is frozen. But when spring comes back they start to melt. These small rivers is connected to each of the other exactly like a tree with the direct edge in the tree have a value equal To the amount of ice in it.

Here is the tree of the sample test case:

When rivers start-to-melt, water starts to flow from Node 1 (root of the tree) to any node, it can reach. When the water first reaches a node u, an ice starts to melt in all it direct children edges at a rate of 1 unit per second. Once (U, v) edge completely melted, the rate of melting of all, and edges that start from u would slow down to be 0.5 uni T per second, while the other edges this start from v start melting with 1 unit per second.

Given the rivers tree, and a certain time, can you tell us what many leaves of the tree did the water reach? A leaf is a node this has no children. Input

The first line contains T the number of test cases. For each test case, the first line contains N the number of nodes (2≤n≤100, 000). followed by N-1 lines, each line describes the nodes 2 to N. Each line would contain 2 integers Pi and Ci (1≤pi≤n) (0≤ci≤100,000) representing the parent of the i-th node (I starts from 2 here) and the amount of ice on the edge connecting the current node to its parent. Node 1 is the root of the tree. After that there was a line contains (1≤q≤100), the number of queries. Then Q lines contain the times of these queries in seconds (0≤query time≤1012). Output

Print one line for each query in the should contain the number of leaves the water reached at T He time of the query. Example Input

1
4
1 3
1 5
2 2
8
1
2
3
4
5
6
7
8
Output
0
0
0
0
1
1
2
2
Note

In the sample test case:

At time 0:water are at node 1

At time 1:water have melted 1 unit of Edge (1, 2), and 1 unit of Edge (1, 3)

At time 3:water have completely melted edge (1, 2). The rate of melting of (1, 3) drops to 0.5 unit/second, while Edge (2, 4) starts to melt at rate 1 Unit/second.

At time 5:water have completely melted edge (2, 4), and the remaining edge (1, 3) have 4 units melted, 1 to go.

At time 7:ice completely melted in all edges.


#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <
String.h> #define LL long long using namespace std;
const int MAXN=1E5+10;
const LL INF=0X3F3F3F3F;
vector<int>g[maxn];//note MAXN According to test instructions value int nu;
ll TEM[MAXN],ANS[MAXN],VIR[MAXN];
	void BFs () {queue<int>q;
	int V;
	Q.push (1);
	   while (!q.empty ()) {int Cur=q.front ();
	   Q.pop ();
	   if (G[cur].size () ==0)//If the current node is a leaf, store the corresponding time in another array (this node is a node constructed with a vector) {ans[nu++]=tem[cur];
	      } else//Search for child nodes of this node {ll minn=inf;
	  	      for (int k=0;k<g[cur].size (); k++) {v=g[cur][k];
	  	      Minn=min (Minn,vir[v]);//Find out the minimum time spent in a child node, that is, the last node's water all flows into a child node for the minimum period.
	      Q.push (v);
	  	      } for (int s=0;s<g[cur].size (); s++) {v=g[cur][s]; tem[v]=tem[cur]+minn+ (Vir[v]-minn) *2;//the time at which the parent node's water is fully flowed into each child node at the minimum time (formula: this node time = time of the previous node + this inflow minimum time + (traffic-minimum time) * *),
	  In fact, the flow can be regarded as time, because the unit time flow 1, the latter is called 2 because the smallest flow after the end of the remaining nodes per unit of time 0.5来 flow.    }}}} int main () {int t,n,a,c,l,b;
     ll X;
	 scanf ("%d", &t);
	 tem[1]=0;
	  for (int i=0;i<t;i++) {scanf ("%d", &n);
	  nu=0;
	  memset (ans,0,sizeof (ans));
	  memset (tem,0,sizeof (TEM));
	  for (int m=0;m<=n;m++) {g[m].clear ();
	   } for (int j=2;j<=n;j++)//Here is a trick to cleverly construct the vector tree.
	    {scanf ("%d%d", &a,&b);
		Vir[j]=b;
	   G[a].push_back (j);
	   } BFS ();
	   cin>>l;
	   	  Sort (ans,ans+nu);//The time to do the sort while (l--) {scanf ("%lld", &x) for water to flow completely into each leaf; 
	   ll *xx=upper_bound (ans,ans+nu,x);//upper_bound (start,end, lookup number) returns the address of the next number of numbers to find; printf ("%d\n", Xx-ans);
}} return 0;  }
The above are not enough to welcome correct.

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.