CF1280D.Miss Punyverse

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Oak has nn nesting places, numbered with integers from 11 to nn . Nesting place ii is home to bib_i bees and wiw_i wasps.

Some nesting places are connected by branches. We call two nesting places adjacent if there exists a branch between them. A simple path from nesting place xx to yy is given by a sequence s0,,sps_0, \ldots, s_p of distinct nesting places, where pp is a non-negative integer, s0=xs_0 = x , sp=ys_p = y , and si1s_{i-1} and sis_{i} are adjacent for each i=1,,pi = 1, \ldots, p . The branches of The Oak are set up in such a way that for any two pairs of nesting places xx and yy , there exists a unique simple path from xx to yy . Because of this, biologists and computer scientists agree that The Oak is in fact, a tree.

A village is a nonempty set VV of nesting places such that for any two xx and yy in VV , there exists a simple path from xx to yy whose intermediate nesting places all lie in VV .

A set of villages P\cal P is called a partition if each of the nn nesting places is contained in exactly one of the villages in P\cal P . In other words, no two villages in P\cal P share any common nesting place, and altogether, they contain all nn nesting places.

The Oak holds its annual Miss Punyverse beauty pageant. The two contestants this year are Ugly Wasp and Pretty Bee. The winner of the beauty pageant is determined by voting, which we will now explain. Suppose P\mathcal{P} is a partition of the nesting places into mm villages V1,,VmV_1, \ldots, V_m . There is a local election in each village. Each of the insects in this village vote for their favorite contestant. If there are strictly more votes for Ugly Wasp than Pretty Bee, then Ugly Wasp is said to win in that village. Otherwise, Pretty Bee wins. Whoever wins in the most number of villages wins.

As it always goes with these pageants, bees always vote for the bee (which is Pretty Bee this year) and wasps always vote for the wasp (which is Ugly Wasp this year). Unlike their general elections, no one abstains from voting for Miss Punyverse as everyone takes it very seriously.

Mayor Waspacito, and his assistant Alexwasp, wants Ugly Wasp to win. He has the power to choose how to partition The Oak into exactly mm villages. If he chooses the partition optimally, determine the maximum number of villages in which Ugly Wasp wins.

输入格式

The first line of input contains a single integer tt ( 1t1001 \le t \le 100 ) denoting the number of test cases. The next lines contain descriptions of the test cases.

The first line of each test case contains two space-separated integers nn and mm ( 1mn30001 \le m \le n \le 3000 ). The second line contains nn space-separated integers b1,b2,,bnb_1, b_2, \ldots, b_n ( 0bi1090 \le b_i \le 10^9 ). The third line contains nn space-separated integers w1,w2,,wnw_1, w_2, \ldots, w_n ( 0wi1090 \le w_i \le 10^9 ). The next n1n - 1 lines describe the pairs of adjacent nesting places. In particular, the ii -th of them contains two space-separated integers xix_i and yiy_i ( 1xi,yin1 \le x_i, y_i \le n , xiyix_i \neq y_i ) denoting the numbers of two adjacent nesting places. It is guaranteed that these pairs form a tree.

It is guaranteed that the sum of nn in a single file is at most 10510^5 .

输出格式

For each test case, output a single line containing a single integer denoting the maximum number of villages in which Ugly Wasp wins, among all partitions of The Oak into mm villages.

输入输出样例

  • 输入#1

    2
    4 3
    10 160 70 50
    70 111 111 0
    1 2
    2 3
    3 4
    2 1
    143 420
    214 349
    2 1
    

    输出#1

    2
    0
    

说明/提示

In the first test case, we need to partition the n=4n = 4 nesting places into m=3m = 3 villages. We can make Ugly Wasp win in 22 villages via the following partition: {{1,2},{3},{4}}\{\{1, 2\}, \{3\}, \{4\}\} . In this partition,

  • Ugly Wasp wins in village {1,2}\{1, 2\} , garnering 181181 votes as opposed to Pretty Bee's 170170 ;
  • Ugly Wasp also wins in village {3}\{3\} , garnering 111111 votes as opposed to Pretty Bee's 7070 ;
  • Ugly Wasp loses in the village {4}\{4\} , garnering 00 votes as opposed to Pretty Bee's 5050 .

Thus, Ugly Wasp wins in 22 villages, and it can be shown that this is the maximum possible number.

In the second test case, we need to partition the n=2n = 2 nesting places into m=1m = 1 village. There is only one way to do this: {{1,2}}\{\{1, 2\}\} . In this partition's sole village, Ugly Wasp gets 563563 votes, and Pretty Bee also gets 563563 votes. Ugly Wasp needs strictly more votes in order to win. Therefore, Ugly Wasp doesn't win in any village.

首页