J-whistle ' s New Car gym-101147j

Source: Internet
Author: User
Statements

Whistle has bought a new car, which have an infinite fuel tank capacity.

He discovered an irregular country since it has n cities and there is exactly n-1roads between them, of Course, all cities is connected. He's so much clever, he realized, the country are like a rooted tree of n nodes and Node 1 is the root. Each City i has one filling station by which he can fill his car's fuel tank in no more than xi l Iter. Whistle liked the country very much, and he wants to know what's the most attractive city in the country are. The attractiveness of the city i is defined by what much it ' s reachable from other cities, in other words the ATT Ractiveness of City are the number of cities j that satisfies these condition:city j is in the subtree of city i  (except for city i itself). Whistle'll start at City j and would only fill his car ' s fuel tank with xjliters and never fill it again u Ntil He reach City i Whistle should be able to reach CITY&NBSp;i with non-negative fuel.

He knows the length of every road and that 1 miles would take exactly 1 liter on any road.

As you know, Whistle are very clever, but he's not this good at the programming, so he asked your to help him. He wants to know the attractiveness of all city, so that he can decide which city to live in. Input

The first line of input contains one integer T, the number of test cases.

The next line contains one integer (1≤n≤500, a), the number of cities in the country.

The next line contains n integers (1≤xi≤1, 000, 000, 000).

Each one of the next n-1 line contains three integers A, B, C (1≤a, B≤n and 1≤c≤1, all, $), that means th Ere is a road between City A and city B of length c. Output

For each test case, output a line containing n integers, and the attractiveness of each city. Example Input

1
4
5 5 1 2 +
2 3 5
3 4 5
Output
0 2 1 0
Note

Large I/O files. Please consider using fast input/output methods.


This question, at first think of too complex, all kinds of want to mess up no success, embarrassment.

The tree DP seems to be out of the way.

Finally found directly in the DFS sequence to do this problem, it can be maintained, because the only problem is how to find the fastest way to the point, want to two points but in the tree can not be two points, but now is a chain, so nature can be maintained, and then all the problems solved, Before want to use line tree but do not know where to write residue, WA2, and then learn the next prefix and maintenance of the idea.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int MAXN = 1E6+10;
int t,n;

LL VALUE[MAXN];
int head[maxn],_cnt;
	struct edge{int to,next;
LL W;
}edge[maxn<<1];
	Inline init () {for (int i = 1; I <= n; i++) head[i] = 1;
_cnt = 0;
	} inline void ADD (int u,int v,ll w) {edge[_cnt].to = v;
	EDGE[_CNT].W = W;
	Edge[_cnt].next = Head[u];
Head[u] = _cnt++;
} LL Sum[maxn],tot;
int ANS[MAXN];
int ID[MAXN],FA[MAXN];
		void Dfs (int now,int father) {for (int i = head[now]; ~i; i = edge[i].next) {int v = edge[i].to;
			if (v! = father) {Fa[v] = now;
			Sum[tot] = SUM[TOT-1]+EDGE[I].W;
			Id[tot] = v;
			int pos = Lower_bound (Sum,sum+tot+1,sum[tot]-value[v])-sum;
			if (pos < tot) ans[now]++,ans[fa[id[pos]]]--;
			tot++;
			DFS (V,now);
			tot--;
		Ans[now] + = Ans[v];
	}}} int main () {freopen ("car.in", "R", stdin);
	int u,v;
	LL W;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n);
		Init (); for (int i = 1; I <= n; i++) scanf ("%lld", &value[i]);
			for (int i = 1; i < n; i++) {scanf ("%d%d%lld", &u,&v,&w);
			ADD (U,V,W);
		ADD (V,U,W);
		} memset (ans,0,sizeof (ans));
		Sum[0] = 0;
		Id[0] = 1;
		tot = 1;
		DFS (a); for (int i = 1; I <= n; i++) printf ("%d%c", Ans[i], (i = = N)? '
	\ n ': ');
} return 0; }

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.