nitacm 2019 school race E Ms. Layton and balanced tree (disjoint-set maintenance)

Topic links: https://ac.nowcoder.com/acm/contest/2995/E

Meaning of the questions:

The tree from u to v in the simple right to all points in the path, the difference between the maximum value and the minimum value for the balance (u, v).

T groups (T <= 10), n (1 <= n <= 5e5), n a a [i] (1 <= a [i] <= 1e9) represents the point right, n-1 th u and v, represents u, v has two connecting edges, to ensure a tree configuration

Seeking balance% 1e9 tree

Example:

The answer is 179 

answer:

Maximum and minimum values ​​were calculated.

The method of selecting the maximum value: from general to each of the dots and connected with a set of check and, while maintaining the size of each block Unicom, this point can now be clearly calculated as the maximum number of paths.

The same method of calculation of the minimum value.

 

Ideas:

Calculating the maximum weight for press point

Point: 4,5,6,8,9,3,10,7,1,2

Point right: 2,4,5,5,5,6, 6,8,9,9

④ The first point vis set to 1, have access, then access through all the points have been connected with the point ④, was not found.

Then the point ⑤ vis set to 1, have access, then access through all the points have been connected with the point ⑤, find the point ④, at this time needs to be handled as follows:

④ point communication link block (size1 total points) have a maximum weight of 2 point, and the point ⑤ block communication link (a total of size2 points) are a maximum weight of 4 points,

Take any point from the left block Unicom, China Unicom block in either take the right point, the maximum point right between two points are 4, for a total of size1 * size2 point, multiplied by 4 ⑤ the right point, this part is the max value

Then the union of two blocks, recalculate the size (with disjoint-set maintenance), the right to block the point is how many is not important, because sorting is a point right from small to large, are multiplied by a large point.

 

Right after calculating all points and max, min calculated point right in reverse order, that is, the answer subtraction.

 

Codes attached o (* ¯ ▽ ¯ *) Stab:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
const int mod=1e9+7;
int n,fa[maxn],size[maxn],vis[maxn];
pair<int,int> a[maxn];
vector<int> G[maxn];

void init()
{
    for(int i=1;i<=n;i++)fa[i]=i,size[i]=1; 
    memset(vis,0,sizeof vis);
}
int get(int x)
{
    if(fa[x]==x)return x;
    return fa[x]=get(fa[x]);
}
void merge(int a,int b)
{
    a=get(a);
    b=get(b);
    if(a!=b)
    {
        for [a] = b;
        size[b]+=size[a];
     } 
}

int main ()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].first);
            a[i].second=i;
            G[i].clear(); 
        }
        for(int i=1;i<n;i++)
        {
            you and, v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        sort(a+1,a+1+n);
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            int now=a[i].second;
            vis[now]=1;
            for(int j:G[now])
            {
                if(vis[j])
                {
                    ans=(ans+(1ll*a[i].first*size[now]%mod)*size[get(j)]%mod)%mod;
                    merge(j,now); 
                }
            }
        }
        
        init();
        for(int i=n;i>=1;i--)
        {
            int now=a[i].second;
            vis[now]=1;
            for(int j:G[now])
            {
                if(vis[j])
                {
                    ans=(ans-(1ll*a[i].first*size[now]%mod)*size[get(j)]%mod+mod)%mod;
                    merge(j,now); 
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
} 

 

wa Ryo 几发,

return fa[x]=get(fa[x]);写成return get(fa[x]);

G [i] is not write-clear ()

......

Guess you like

Origin www.cnblogs.com/myrtle/p/12046384.html