noip2015day2- transport plan

Title Description

$ 2044 $ AD, mankind entered the cosmic era.

\ (L \) state-owned \ (n \) planets, there \ (n-1 \) bidirectional channel, each channel is established between the two planets, which \ (n-1 \) waterway communication with the \ (L \) in all countries of the planet.

Small \ (P \) in charge of a logistics company, the company has a number of transportation planning, transportation plans for each of the form: there is a need for logistics spacecraft from \ (u_i \) flying along the fastest route number of aerospace planet to \ (v_i \) number to the planet. Obviously, the spacecraft passing a course it takes time for the channel \ (J \) , any ship passing of time it takes for \ (t_j \) , and does not produce any interference between any two spacecraft.

In order to encourage scientific and technological innovation, \ (L \) The king agreed to a small country \ (P \) logistics companies to participate in \ (L \) waterway construction States that allow small $ P $ to a waterway transformed into a wormhole, passing spacecraft wormhole is not time consuming.

Before the completion of construction of small wormhole \ (P \) logistics company received a pre \ (m \) a transportation plan. After the completion of the construction of the wormhole, it \ (m \) a transportation plan will also begin, starting with all the spacecraft. When it \ (m \) when a transport plan have been completed, a small \ (P \) of the logistics company's stage work is complete.

If small \ (P \) are free to choose which of course will be transformed into a wormhole and try to find small \ (P \) logistics company completed the minimum time needed for the work stage is how much?

Input

The first line includes two positive integers \ (n-, m \) , represents \ (L \) the number of countries and the small planet \ (P \) number of companies pre-contact transportation schedule, from planet \ (1 \) to \ (n \) number.

Next \ (n-1 \) construction described channel line, wherein the first \ (I \) line contains three integers \ (a_i, b_i \) and \ (t_i \) , represents the \ (I \) Article two-way channel built in the \ (a_i \) and \ (b_i \) between the two planets, the spacecraft traveled at any time that it takes for \ (t_i \) .

Next \ (m \) where row transportation schedule is described, where the first \ (J \) line contains two positive integers \ (u_j \) and \ (v_J \) , represents the \ (J \) th transport plan from \ (u_j \) No. planet to \ (v_j \) No. planet.

Data assurance \ (1≤u_i, v_i≤n, 1 < = n, m <= 300000 \)

Ensure data \ (1≤a_i, b_i≤n \) and \ (0≤t_i≤1000 \) .

Output

The output common \ (1 \) line contains \ (1 \) integer representing small \ (P \) logistics company completed the minimum time required for stage work.

Sample Input

6 3 
1 2 3 
1 6 4 
3 1 7 
4 3 6 
3 5 5 
3 6 
2 5 
4 5

Sample Output

11

First of all, it is natural to see the maximum minimum of thought half the answer.

Ah, two points are very obvious. . . .

So, how do we \ (check \) it? For path between two points is already less than the current \ (x \) path can be ignored directly out.

Then the remaining path is greater than the current \ (x \) path, the path we need to get rid of these for one side are all the paths in a maximum to a minimum.

That is, we need to subtract the path intersection biggest edge .

This operation is very evident, because only the more we can reduce leftover less.

Now, we need to do is find the intersection of all paths?

How find it? We can use a tree difference to be solved.

We need to convert to a point on the edge, then how do we convert it?

Since the node an edge node must be divided father and son nodes.

The son nodes corresponding to only one side, so we moved to the side of the child node.

When added to a path \ (a, b \) , the set \ (the LCA \) of \ (a, b \) a \ (LCA \) , then \ (Sum [a] ++, Sum [b] ++ , the Sum [the LCA] - = 2 \) .

If the answer when we count the current node \ (Sum \) the number of paths is not satisfied with the conditions, indicating that the node corresponding to the side as the intersection of all paths,

The answer can be updated. . .

Finally, look at the longest path subtracting the maximum on the path is \ (<= \) Current \ (x \) can be.

But solving the problem of data is large, common algorithms easily stuck out.

So, we need some optimization. . .

1. Optimize half of bounds:

\ (L = \) longest path length subtracting the maximum right side, \ (R & lt = \) longest path length.

Such a guaranteed number of dichotomy in \ (10 \) or less.

2. Avoid recursion:

Due to statistical answer, we need to solve the recursion, and recursion is constant,

We also found that the value of only one node and its sub-tree about , and the sub-tree (dfs \) \ ensure the child node number is greater than the number when the root node in order.

So, we can use the \ (dfs \) sequence backwards update \ (Sum \) value.

code show as below

#include <bits/stdc++.h>
 
using namespace std;
 
#define LL long long
#define reg register
#define Raed Read
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(int i=(G).Head[x]; i; i=(G).Nxt[i])
 
inline char G1() {
    static const int LEN=2000005;
    static char U[LEN],*T=U,*E=U;
    if(T==E)T=U,E=U+fread(U,1,LEN,stdin);
    return T==E?EOF:*T++;
}
 
inline int Read(void) {
    int res=0,f=1;
    char c;
    while(c=G1(),c<48||c>57)if(c=='-')f=0;
    do res=(res<<3)+(res<<1)+(c^48);
    while(c=G1(),c>=48&&c<=57);
    return f?res:-res;
}
 
template<class T>inline bool Min(T &a,T const&b) {
    return a>b ?a=b,1:0;
}
template<class T>inline bool Max(T &a,T const&b) {
    return a<b?a=b,1:0;
}
 
const int N=3e5+5,M=3e5+5,mod=1e9+7;
 
bool MOP1;
 
int n,m,A[N],B[N],TT[M];
 
struct Link_list {
    int Tot,Head[N],to[M<<1],Nxt[M<<1],cost[M<<1];
    inline void clear(void) {
        Tot=0;
        memset(Head,0,sizeof Head);
    }
    inline void AddEdgepair(int a,int b,int c) {
        to[++Tot]=b,cost[Tot]=c,Nxt[Tot]=Head[a],Head[a]=Tot;
        to[++Tot]=a,cost[Tot]=c,Nxt[Tot]=Head[b],Head[b]=Tot;
    }
} G;
 
int Fa[M],dep[M],top[M],sz[M],son[M],Dis[M],LCA[N],cnt,Id[N];
 
void dfs1(int x,int f) {
    Id[++cnt]=x;
    dep[x]=dep[f]+1,Fa[x]=f,sz[x]=1;
    erep(i,G,x) {
        int y=G.to[i];
        if(y==f)continue;
        Dis[y]=Dis[x]+G.cost[i];
        TT[y]=G.cost[i];
        dfs1(y,x),sz[x]+=sz[y];
        if(sz[y]>sz[son[x]])son[x]=y;
    }
}
 
void dfs2(int x,int f) {
    top[x]=f;
    if(son[x])dfs2(son[x],f);
    else return;
    erep(i,G,x) {
        int y=G.to[i];
        if(y==Fa[x]||y==son[x])continue;
        dfs2(y,y);
    }
}
 
inline int lca(int x,int y) {
    while(top[x]!=top[y]) {
        if(dep[top[x]]>dep[top[y]])x=Fa[top[x]];
        else y=Fa[top[y]];
    }
    return dep[x]<dep[y]?x:y;
}
 
int res,tot,Sum[N];
 
inline bool check(int x) {
    res=-1,tot=0;
    rep(i,1,n)Sum[i]=0;
    rep(i,1,m) {
        if(Dis[A[i]]+Dis[B[i]]-2*Dis[LCA[i]]<=x)continue;
        tot++,Sum[A[i]]++,Sum[B[i]]++,Sum[LCA[i]]-=2;
    }
    drep(i,n,1) {
        int u=Id[i];
        erep(i,G,u) {
            int y=G.to[i];
            if(y==Fa[u])continue;
            Sum[u]+=Sum[y];
        }
        if(Sum[u]==tot)Max(res,TT[u]);
    }
    if(res==-1)return false;
    rep(i,1,m) {
        if(Dis[A[i]]+Dis[B[i]]-2*Dis[LCA[i]]<=x)continue;
        if(Dis[A[i]]+Dis[B[i]]-2*Dis[LCA[i]]-res>x)return false;
    }
    return true;
}
 
int Ma;
 
inline void solve(void) {
    int L=0,R=0,Ans=0;
    rep(i,1,m)Max(R,Dis[A[i]]+Dis[B[i]]-2*Dis[LCA[i]]);
    L=R-Ma;
    while(L<=R) {
        int mid=(L+R)>>1;
        if(check(mid))Ans=mid,R=mid-1;
        else L=mid+1;
    }
    printf("%d\n",Ans);
}
 
 
bool MOP2;
 
inline void _main(void) {
    n=Read(),m=Read();
    int f=1;
    ret(i,1,n) {
        int a=Read(),b=Read(),c=Read();
        Max(Ma,c),G.AddEdgepair(a,b,c);
    }
    dfs1(1,0),dfs2(1,1);
    rep(i,1,m)A[i]=Read(),B[i]=Read(),LCA[i]=lca(A[i],B[i]);
    solve();
}
 
signed main() {
    _main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/dsjkafdsaf/p/11360357.html