# (DP + tree inclusion and exclusion) Rockwell valley P3047 [USACO12FEB] Nearby bovine close Cows

Title Description

Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nearby fields.

Specifically, FJ's farm consists of N fields (1 <= N <= 100,000), where some pairs of fields are connected with bi-directional trails (N-1 of them in total). FJ has designed the farm so that between any two fields i and j, there is a unique path made up of trails connecting between i and j. Field i is home to C(i) cows, although cows sometimes move to a different field by crossing up to K trails (1 <= K <= 20).

FJ wants to plant enough grass in each field i to feed the maximum number of cows, M(i), that could possibly end up in that field -- that is, the number of cows that can potentially reach field i by following at most K trails. Given the structure of FJ's farm and the value of C(i) for each field i, please help FJ compute M(i) for every field i.

Farmer John has noticed his cows often move between fields nearby. With that in mind, he wanted to plant the grass enough in every land, not only for the initial cow on this land, but also for grazing cows go from a nearby field.

Specifically, FJ farm field constituted by the N blocks (1 <= n <= 100,000), there is no connection to a side (a total of n-1 edges) between each of the two fields. FJ design farm, between any two fields i and j, there is only one path connecting i and j. Field block i is C (i) residence cattle, cows although sometimes arrive by way of various other fields k (1 <= k <= 20).

FJ want planted on each field be M (i) cows eating grass. M (i) refers to the number from other points can be reached after k steps up to this point cows.

FJ number of cows are now given for each field, please help FJ calculated for each field of an M (i).

Input Format

* Line 1: Two space-separated integers, N and K.

* Lines 2..N: Each line contains two space-separated integers, i and j (1 <= i,j <= N) indicating that fields i and j are directly connected by a trail.

* Lines N+1..2N: Line N+i contains the integer C(i). (0 <= C(i) <= 1000)

First line: n and K;

N-1 row behind the: i and j (two fields);

After the line n: 1..n each block of C (i);

Output Format

* Lines 1..N: Line i should contain the value of M(i).

n lines: each line M (i); // i: 1..2

Sample input and output

Input # 1
6 2 
5 1 
3 6 
2 4 
2 1 
3 2 
1 
2 
3 
4 
5 
6 
Output # 1
15 
21 
16 
10 
8 
11 

Description / Tips

There are 6 fields, with trails connecting (5,1), (3,6), (2,4), (2,1), and (3,2). Field i has C(i) = i cows.

Field 1 has M(1) = 15 cows within a distance of 2 trails, etc.

DESCRIPTION Title: given a tree of n points, each point has C_i cows, cow asked how each point within the range of each step k.

Thank @Slager_Z translation

This question seems to be violence? ? ? (* ╹ ▽ ╹ *)

Positive solutions: movable Regulation tree (〃 '▽' 〃)

analysis:

Topic Description: give a little right of the tree, and asked all the right point range and node k steps.

State design: Let f [i] [j] i is represented by the destination node, all nodes k-outward expansion can reach and!

The focus is to consider the state transition equation. Since the total of the n-1 to the free edges, it is clear that constitute the tree structure. Then consider a node x, and all his nodes connected to it ~

Consider f [x] [k] to x as the target node, starting within k steps can point to the right and then the state of x must be transferred from his adjacent nodes

Consider x connection points x1, x2, x3 .... xn;

X k solving the extension step in the direction x1, k-1 can be extended further outwardly transferred from a state according x1!

But there is also a problem, that is f [x1] [k-1] only have contributed in the direction of x to x1, extending the point right after all distances x1 and not greater than k;

After the other side, that is, from the direction x1-> x's, x1 went to location x, there is still a distance k-2 can continue to spread!

That is, f [x1] [k-1] calculated the repeated f [x] [k-2] to remove all the values ​​of X --- x1 direction (it is the root node of the subtree x1) of!

So, this is so, the state point x by transfer over all of its nodes Unicom, the direction (sub-tree) where each son has a k-2 deletion calculation step,

Finally, it will be more out Son [x] -1 times f [x] [k-2] of arithmetic overflow, each can be obtained by subtracting a positive solution!

In fact, the inclusion-exclusion principle used here QAQ ~

Briefly, the presence of A, B, C, three sets

Then A∪B∪C = A + B + CA∩BA∩CB∩C + A∩B∩C;

(Hand animated a Venn diagram to understand it (* ^ ▽ ^ *))

State transition equation is: k> when 1 f [x] [k] = Σf [xi] [k-1] - (Son [x] -1) * f [x] [k-2];

        When k = 1: f [x] [k] = Σf [xi] [k-1] + f [x] [0]; (At this time k-2 <0, absence of f [x] [k-2 ] overflow!, and if k = 2, the presence of f [x] [0] is double counting)

#include <the iostream>
#include <cstdio>
the using namespace STD;
int n-, K, F [100001] [21 is]; //
int A, B, sonnum [100001]; // meaning of each element in the above
struct edge { //
int Next; // adjacency table storing tree
int to;
};
Edge E [200000]; //
int head [200001], NUM; //
void addedge (int from, int to) //
{
E [+ NUM +] = .next head [from]; //
E [NUM] .to = to; // adjacency table
head [from] = NUM; //
}
int main ()
{
Scanf ( "% D% D", & n-, & K); //
for (int I =. 1; I <= n--. 1; I ++) //
{
Scanf ( "% D% D", & A, & B); //
addedge (A, B); / / no are kept to FIG, two directions once
addedge (B, a); //
sonnum [B] ++; // each point of the link points + 1'd
sonnum [a] ++; //
}
for (int i = 1; i <= n; i ++) // initialize to go to zero steps, that is, each node point in itself right
Scanf ( "% D", & F [I] [0]);
// DP
for (int i = 1; i < = k; i ++) // dp stage! Number of steps to go to be a DP stage!
for (int j = 1; j <= n; j ++) // for each node enumeration dp
{
for (int head T = [J]; T; T = E [T] .next) node i // Enumeration All nodes Unicom
{
F [J] [I] + = F [E [T] .to] [-I. 1]; // accumulated contribution Unicom points, duplicate
}
IF (I>. 1)
F [J ] [i] - = f [ j] [i-2] * (sonnum [j] -1); // if there arithmetic overflow, repeated portion lose
the else
F [J] [. 1] + = F [J ] [0]; // If no overflow, then just add its own right point
}
for (int. 1 = I; I <= n-; I ++) //
the printf ( "% D \ n-", F [I] [K ]); // output
return 0;
}

-END-

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11403267.html