Description
Mitt is a very mysterious substance on planet D, and it contains a tremendous amount of energy. The transportation and storage of the Mitte energy has been a major problem in the D-Star, which is the main energy source of Mitt.
There are n cities on the D star, and we numbered them in 1 to N and 1th cities as capitals. The N cities are connected by a one-way high-speed channel of N-1, forming a tree with the roots of city 1th, the direction of which is directed by the son of the tree pointing to his father. The tree is layered by depth: The root node depth is 0, belongs to the 1th layer, the node depth of the root node is 1, belongs to the 2nd layer, and so on, the node with the depth of I belongs to the I+L layer.
After the high-speed channel was built, the D-Stars began to consider how to store and transfer the resources of the Mitte in detail. Because of the varying degrees of development, the capacity of each city to store the mitt varies, with the city of I having a Mitte storage capacity of a[i]. In addition to the storage function, this mitt has the ability to automatically collect the mitt. If six o'clock in the evening, there is a storage
When the register is not full, it automatically collects the energy contained in the atmosphere, and collects it before six o'clock in the morning, but it is safe to start the Automatic collection program only when the storage is completely empty, but the boot may have a security risk if it is not full and not empty. Between six and seven in the morning, the root node city (city 1th) consumes the mitt in its storage.
The root node does not collect the mitt automatically, it only accepts the mitt that the child node transmits. Seven in the morning, between the cities to initiate the transfer process of the Mitte, the transmission process is progressive: first, the 2nd node city to the 1th layer (the root node city, that is, 1th City) transmission, until the 1th layer of the storage full or the 2nd layer of the storage is empty, and then the 3rd layer to the 2nd layer, until each node, The storage for which the storage is full or its nodes (on the 3rd level) is empty, and so on, until the last layer of transmission is complete. The transfer process must be completed before six o'clock in the evening.
For technical reasons, the following conditions must be met for the transport programme:
(1) cannot let a storage device at the end of the six o'clock transmission is still in a non-empty but not full state, this time the storage will still start to automatically collect the Mitt program, and to the storage has been stored in the mitt of the store can be dangerous to start the collector, that is, to make the storage at six o'clock at night is either empty or full;
(2) With regard to the capital-the special case of City 1th, the Mitte in the Mitte in the 1th city from six to seven in the morning is automatically exhausted, i.e. the transport plan does not need to consider how the capital's Mitte is transported;
(3) In addition to city 1th, each node must transport the Mitte of the city's Mitte storage to the parent node before it is transported to it in its sub-node city, without allowing the remaining mitt in the storage to mix with the foreign mitt;
(4) The number of Mitte to be transported to a particular city must be exactly the same, otherwise the risk may occur when the different Mitte of these sources are mixed at different proportions.
Now the D star has established high-speed access, and each city also has a certain storage capacity of the Mitte storage. In order to meet the above constraints, it may be necessary to rebuild the Mitt storage in some cities. You can, and can only, destroy a certain city (including the capital) of the excrement to exist in the Mitt storage, and then create a new arbitrary capacity of the Mitt storage, the capacity can be a decimal (in the input data, the original storage capacity is a positive integer, but can be a decimal after reconstruction), can not be negative or 0, The number of Mitt storage that needs to be rebuilt is minimal.
Input
The first line is a positive integer n, which represents the number of cities.
The next n lines, one positive integer per line, where line I represents the capacity of the Mitte storage that existed in the first city.
Next is the N-i line, with two positive integers per line, a, a, or city B to city A that has a high-speed channel (A≠B).
Output
The output file contains only one row, an integer that represents the minimum number of Mitt storage that has been rebuilt (that is, modifying the storage capacity).
Sample Input
5
5
4
3
2
I
12
13
24
25
Sample Output3HINT
"Sample Interpretation"
An optimal solution is to change a[1] to 8,a[3] to 4,a[5] to 2. In this way, 2 and 3 are shipped to 1 of the same amount, 4 and 5 are shipped
Give 2 the same amount, and every night at six o'clock, full, 3,4,5 empty, meet all the restrictions.
For 100% of data to meet n<500000,a[j]<10^8
Source
Others say read the topic is to water, but I do not imagine that water ah, I still turned the problem ... Qaq, I must be too weak ...
In fact, the practice is very well understood. The magnification relationship of any two points is deterministic (if A is a B child, B has a K-child, then A[b]=k*a[a], the relationship can be used to introduce any two points of the relationship. The head was foolish to do, and did not think of it. Set Dis[i] for the modified a[1]=dis[i]*a[i], then test instructions when A does not modify B does not modify when and only when a[a]*dis[a]=a[b]*dis[b], so we can think in turn. The least modification is not to ask the most do not modify it??? We know all the a[i]*dis[i], see which value is the most.
But the DIS array value is likely to be large, and we have two strategies to deal with--(take the logarithm and sort (take the logarithm is a very important processing method), hash).
1#include <algorithm>2#include <cmath>3#include <vector>4#include <iostream>5#include <cstdio>6#include <cstdlib>7 using namespacestd;8 9typedefLong Doubleld;Ten #defineMAXN (500010) One #defineINF (1 << 30) A #defineEPS (1E-11) - intn,next[maxn<<1],toit[maxn<<1],FA[MAXN]; - intSIDE[MAXN],CNT,TEAM[MAXN],D[MAXN]; theLD A[MAXN],DIS[MAXN],BAC[MAXN];BOOL inch[MAXN]; - -InlineBOOLEqual (LD A,ld b) {returnFabs (A-B) <=EPS;} - +InlinevoidAddintAintb) {next[++cnt] = Side[a]; Side[a] = cnt; TOIT[CNT] =b;} - +InlinevoidInsintAintb) {Add (a, B); add (b,a);} A atInlinevoidBFS () - { - inth =0, t =0; - inch[1] =true; TEAM[++T] =1; - while(H! =t) - { in intnow = team[++h]; - for(inti = Side[now];i;i =Next[i]) to if(!inch[Toit[i]]) + inch[Toit[i]] =true, team[++t] = toit[i],fa[toit[i]] = now,++D[now]; - } the for(inti =2; I <= n;++i) *Dis[team[i]] = Dis[fa[team[i]] +log (D[fa[team[i]]); $ }Panax Notoginseng -InlinevoidWork () the { + intres =1, CNT; LD last =-1; A for(inti =1; I <= n;++i) bac[i] = Dis[i] +A[i]; theSort (bac+1, bac+n+1); + for(inti =1; I <= n;++i) - { $ if(Equal (bac[i],last)) res = max (res,++CNT); $ Elselast = Bac[i],cnt =1; - } -printf"%d", N-res); the } - Wuyi intMain () the { -Freopen ("3573.in","R", stdin); WuFreopen ("3573.out","W", stdout); - intb; scanf"%d",&n); About for(inti =1; I <= n;++i) scanf ("%d", &a), a[i] =log (a); $ for(inti =1; i < n;++i) scanf ("%d%d",&a,&b), ins (A, b); - BFS (); work (); - fclose (stdin); fclose (stdout); - return 0; A}View Code
Bzoj 3573 M Special transport