【Luogu P3237】[HNOI2014] Mitre Transport

topic link

Topic description

Mitre is a very mysterious substance on planet D that contains enormous energy. On the D star, which uses mites as the main energy source, the transportation and storage of this miter energy has always been a big problem.

There are N cities on the D star, and we number them sequentially from 1 to N, with city 1 being the capital. These N cities are connected by N-1 one-way highways, forming a tree with the No. 1 city (head) as the root, and the direction of the highway is from the son in the tree to the father. The tree is layered by depth: the root node has a depth of 0 and belongs to the first layer; the child nodes of the root node have a depth of 1 and belong to the second layer; and so on, a node with a depth of i belongs to the i+lth layer.

After the high-speed channel was built, the D star began to think about how to store and transmit Mitre resources specifically. Due to the different levels of development, the ability of each city to store mites is different, and the i-th city has a mit storage with a capacity of A[i]. In addition to the storage function, this mit store also has the ability to automatically collect mites.

If at 6 o'clock in the evening, there is a storage tank that is not full, it will automatically collect the miter energy contained in the atmosphere, and it will be full before six in the morning; however, only when the storage tank is completely empty It is safe to start the automatic collection program under the following conditions. If it is not full but not empty, it may have security risks.

Between 6:00 and 7:00 in the morning, the root node city (city 1) will deplete its storage of mites. The root node does not automatically collect mites, it only accepts mites transmitted by child nodes.

At 7:00 in the morning, the Miter transmission process is started between cities, and the transmission process progresses layer by layer: first, the second layer node city transmits to the first layer (the root node city, that is, the first city), until the storage of the first layer is full. or tier 2's storage is all empty; then tier 3 transfers to tier 2 until for each node at tier 2 its storage is full or its pre-node (at tier 3)'s storage is full Empty; and so on until the last layer transfer is complete. The transfer process must be completed by 6pm.

For technical reasons, the transport scheme needs to meet the following conditions:

(1) It is not possible to let a certain storage be in a non-empty but not full state at the end of the transmission at 6 o'clock in the evening. At this time, the storage will still start the program of automatically collecting the meter, and give the storage that has stored the meter. It can be dangerous to start the collection process with the storage device, which means that the storage is either empty or full by 6:00 pm;

(2) Regarding the special case of the capital city, namely City No. 1, every day from 6:00 am to 7:00 am, the mites in the mit store in No. 1 city will be automatically exhausted, that is, the transportation plan does not need to consider the capital. how the mitt is transported;

(3) Except for city No. 1, each node must transport all the mites originally stored in the mit storage of this city to the parent node before its child node city transports mit to it, and storage is not allowed The remaining mites are mixed with foreign mites;

(4) The quantity of mites from several sources transported to a certain city must be exactly the same, otherwise, it may be dangerous after the mites from different sources are mixed in different proportions.

Now the D star people have established a high-speed channel, and each city also has a mit storage with a certain storage capacity. In order to meet the above constraints, it may be necessary to rebuild the Miter storage in some cities. You can, and can only, destroy the existing mit storage in a certain city (including the capital), and create a new mit storage of any capacity, and its capacity can be a decimal (in the input data, The original capacity of the memory is a positive integer, but it can be a decimal after reconstruction), and cannot be a negative number or zero, so that the number of mit memory that needs to be reconstructed is as small as possible.

answer

It is important to understand the meaning of the question!
It is worth noting that if a point has some child nodes that are leaf nodes, it seems that the child nodes that are leaf nodes do not transport mites to the parent node (other child nodes that are not leaves fill the storage of the parent node), so still It can satisfy every condition that the storage is full or empty, but this is not possible (maybe I am too scumbag to understand the meaning of the question).

Then we can easily know that there is such a conclusion:
if the number of sons of the parent node of a certain point is k, then the weight of the point should be one-kth of the parent node, that is, the weight of the parent node should be k times that of the point. .
So if the weight of a point is determined, then the whole tree is determined. Assuming that the weight of a certain point is unchanged, then the weight of the root node is the point on the path to the root of the weight of the point (not Multiply the number of sons including yourself). And for each point, if the calculated root node weights are the same, it is the same legal scheme.

Then we will consider how to count.
I am too weak to hash, so I can only brute force multiple numbers modulo + sorting to count... (simple and rude)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#define hash HASH
using namespace std;
inline int read()
{
    int x=0;char ch=getchar();int t=1;
    for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-') t=-1;
    for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch-48);
    return x*t;
}

const int N=5e5+100;
struct edge{
    int to,next;
}a[N<<1];
int head[N];int cnt;
inline void add(int x,int y){a[++cnt]=(edge){y,head[x]};head[x]=cnt;}
typedef unsigned long long ll;
int n;
const ll mod[3]={998244353,14233333,1000000007};
int w[N];
struct Number{
    ll x[3];
    inline void clear(){x[0]=x[1]=x[2]=0;return ;}
    inline Number operator *(const ll y) {
        Number c;c.clear();
        for(int i=0;i<3;i++) c.x[i]=x[i]*y%mod[i];
        return c;
    }
    inline bool operator <(Number b)const{return x[0]<b.x[0];}
    inline bool operator ==(Number b){return (x[0]=b.x[0]&&x[1]==b.x[1]&&x[2]==b.x[2]);}
}fac[N];
ll k[N];
inline void dfs(int u,int fa)
{
    for(register int v,i=head[u];i;i=a[i].next){v=a[i].to;if(v==fa) continue;k[u]++;}
    for(register int v,i=head[u];i;i=a[i].next)
    {
        v=a[i].to;
        if(v==fa) continue;
        fac[v]=fac[u]*k[u];
        dfs(v,u);
    }
}
Number st[N];
int main()
{
    n=read();
    for(register int i=1;i<=n;i++) w[i]=read();
    register int x,y;
    for(register int i=1;i<n;i++){
        x=read();y=read();
        add(x,y);add(y,x);
    }
    register int ans=1;
    fac[1]=(Number){1,1,1};
    dfs(1,0);int head=0;
    for(register int i=1;i<=n;i++) {
        register Number res=fac[i]*w[i];
        st[++head]=res;
    }
    sort(st+1,st+1+n);
    for(head=1;head<n;head++)
    {
        int sum=1;
        while(st[head]==st[head+1]&&head<n) sum++,head++;
        ans=max(ans,sum);
    }
    printf("%d\n",n-ans);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812513&siteId=291194637