[NOIP school training] City (and prefix)

The meaning of problems

Given a tree ring group, provided deleting an edge from the ring, the maximum distance between any two points is \ (Val \) , seeking \ (val_ {min} \)

Thinking

Obviously \ (val \) , there are two sources, one is a point on the ring selected sub-tree rooted at two points, the other is from a sub-tree to another, a sub-tree, the former can \ (O (n) \) traversing every single tree obtained for the latter, provided:

\ (st [i]: \ ) of the ring \ (I \) points,After omit this array, the point is to understand your own or serial number
\ (d [i]: \ ) subtree rooted at point i in the distance from the furthest point of the i
\ (DIS [i]: \) \ (ST [i +. 1] \) to \ (ST [i ] \) length of this edge

Suppose we selected point \ (I, j \) , I j is the distance and can be represented by a prefix, let \ (s [i] \) represents \ (I \) come from a clockwise, \ ( SUM \) is the loop length, then Imperial \ (i> j \)

Suppose we disconnected \ (k \) and \ (k + 1 \) this edge:

  1. \ (I, J \ Leq K \) , the maximum distance between them is \ ((D [I] D + [J] + S [I] -s [J]) \) , we \ (I \) and \ (J \) separated as \ ((d [i] + s [i]) + (d [j] -s [j]) \)

  2. \ (I, J \. 1 GEQ K + \) , supra

  3. \ (J \ Leq K, I \ GEQ. 1 K + \) , the maximum distance is \ ((D [I] D + [J] + S-SUM [I] + S [J]) \) , empathy demolition open \ ((d [i] -s [i]) + (d [j] + s [j]) + sum \)

We can be known from the need to know \ ((d [i] + s [i]) \) and \ ((d [i] -s [i]) \) two values. Maintenance \ ((d [i] + s [i]) \) prefixed maximum value, \ ((D [I] -s [I]) \) suffix maximum solve the third case; the first two cases directly maintain maximum to note here \ (I \) and \ (J \) will not be repeated. Thus enumeration can be done Erase \ (O (n-) \) .

Code

int main()
{
//  freopen("city.in","r",stdin);
//  freopen("city.out","w",stdout);
    read(n);
    for(int i=1;i<=n;++i)
    {
        int x,y;ll z;
        read(x);read(y);read(z);
        add_edge(x,y,z);
        add_edge(y,x,z); 
    }
    sc(1,0);
    for(int i=1;i<=top;++i) dfs(st[i],0);
    for(int i=2;i<=top;++i)
        for(int j=head[st[i]];j;j=edge[j].next)
            if(edge[j].to==st[i-1])
            {
                d[i]=edge[j].dis;
                //实际上这里的d[i]是上面说的dis[i]
                break;
            }
    for(int i=head[st[1]];i;i=edge[i].next) if(edge[i].to==st[top]) d[1]=edge[i].dis;
    for(int i=2;i<=top;++i) s[i]=s[i-1]+d[i];
    sum=s[top]+d[1];
    ll cur=-INF;
    for(int i=1;i<=top;++i)
    {
        if(i!=1) pre[i]=Max(pre[i-1],cur+dis[st[i]][0]+s[i]);
        cur=Max(cur,dis[st[i]][0]-s[i]);
        //dis[st[i]][0]为上面说的d[i]
    }
    cur=-INF;
    for(int i=top;i>=1;--i)
    {
        if(i!=top) suf[i]=Max(suf[i+1],cur+dis[st[i]][0]-s[i]);
        cur=Max(cur,dis[st[i]][0]+s[i]);
    }
    adpremax[0]=-INF;
    for(int i=1;i<=top;++i) adpremax[i]=Max(adpremax[i-1],dis[st[i]][0]+s[i]);
    misufmax[top+1]=-INF;
    for(int i=top;i>=1;--i) misufmax[i]=Max(misufmax[i+1],dis[st[i]][0]-s[i]);
    for(int i=1;i<top;++i)//断(i,i+1) 
    {
        maxx=-INF;
        maxx=Max(maxx,pre[i]);
        maxx=Max(maxx,suf[i+1]);
        maxx=Max(maxx,sum+adpremax[i]+misufmax[i+1]);
        anss=Min(anss,maxx);
    }
    //特判(top,1)
    anss=Min(anss,pre[top]);
    cout<<Max(anss,ans)<<endl;
    return AFO;
}

Guess you like

Origin www.cnblogs.com/Chtholly/p/11574005.html