3573: [Hnoi2014] Mitte transport
Time limit:20 Sec Memory limit:128 MB
submit:618 solved:359
[Submit] [Status] [Discuss]
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
第一行是一个正整数N,表示城市的数目。接下来N行,每行一个正整数,其中的第i行表示第i个城市原来存在的米特储存器的容量。再接下来是N-I行,每行两个正整数a,b表示城市b到城市a有一条高速通道(a≠b)。
Output
输出文件仅包含一行,一个整数,表示最少的被重建(即修改储存器容量)的米特储存器的数目。
Sample Input
5
5
4
3
2
I
12
13
24
25
Sample Output
3
HINT
"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
Thinking questions.
After reading the test instructions, you will know the value of each point as long as you determine the root of the tree.
To think backwards, to determine one of these points, you know the value of the root.
To make the least change, we need to make the most constant, then we just know how many points can be in the same tree.
Therefore, the BFS to find the current point and the root node of the multiple relationship, and then to find out that the point is not changed in the case of the root nodes is how much (Note Because this value is large, you need to take l o g To do), and then arrange the order to find the maximum number of occurrences.
#include <cstring>#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <vector>#include <algorithm>#include <queue>#define M 500005#define PB Push_back#define EPS 1e-9using namespace STD;intN,V[M]; vector<int>S[M];DoubleA[M],C[M];voidBFS () {intnow=0; Queue<int>Q Q.push (1); c[1]=Log((Double)1); a[++now]=c[1]+Log((Double) v[1]); while(!q.empty ()) {intX=q.front (); Q.pop ();Doublek=Log(S[x].size ()); for(intI=0; I<s[x].size (); i++) {intY=s[x][i]; C[Y]=K+C[X]; a[++now]=Log((Double) V[y]) +c[y]; Q.push (y); } } }intMain () {scanf("%d", &n); for(intI=1; i<=n;i++)scanf("%d", &v[i]); for(intI=1; i<n;i++) {intx, y;scanf("%d%d", &x,&y); S[X].PB (y); } BFS (); Sort (A +1, A +1+N);intL=1, ans=1; for(intI=2; i<=n;i++)if(fabs(a[i]-a[i-1]) >eps) Ans=max (ans,i-l), l=i; Ans=max (ans,n+1-L);printf("%d\n", N-ans);return 0;}
Sentiment:
1. Thinking
2. When no precise value is required, the value is too large to take l o g
"Bzoj 3573" [Hnoi2014] Mitte transport