UVA 12655 Trucks [LCA] (tree chain split +mst)

Source: Internet
Author: User
Tags min printf split

The Subtle Balloons Company (SBC) are the main balloon provider for programming contests; It has
Huge factories and warehouses, as well as a extensive truck fleet to ensure the contestants ' happiness.
There is lots of competition sites in Nlogonia, and all of them hired SBCS for supplying balloons for
Their contests. Nlogonia is a archipelago connected by several bridges. Every island of Nlogonia
There are several regional sites and may also house several SBC warehouses.
When planning the routes for balloon deliveries, SBC faced a problem:for safety issues, every
Bridge in Nlogonia have some maximum weight limit for vehicles which cross it. And because of the
Great net weight of the transported merchandise, SBC operations ' chief asked you to write a program to
Determine the maximum weight allowed to be transported between warehouses and competition sites.
Input
The input contains several test cases. The first line of a test case contains three integers N, M and S
which indicate, respectively, the number of islands, the number of bridges that connect the islands and
The number of sites. The islands is numbered from 1 to N.
Each of the next M lines describes a bridge. The description of a bridge consists in a line with
Three integers A, B and W, indicating respectively the islands connected by the bridge and the
Maximum weight allowed in the bridge, in tons.
All bridges is two-way roads; Every pair of islands is connected by at the most one bridge; And it is
possible to reach every and the archipelago using only bridges (naturally it could be needed
To pass through and other islands to does so).
Each of the next S lines describe a competition site and contains the integers L and H indicating,
respectively, the number of the island where this site is and the number of the
Wharehouse which'll be used to deliver, the balloons to the site is.
Output
For each site in a test case, in the order they were given, your program must produce a,
Containing a single integer, the biggest weight which can is transported by truck from the warehouse
to the site.
Restrictions
2≤n≤2x104
1≤m≤105
1≤s≤5x104
1≤a, B, L, h≤n, a̸= B, l̸= H
0≤w≤105
Sample Input
4 5 4
1 2 9
1 3 0
2 3 8
2 4 7
3 4 4
1 4
2 1
3 1
4 3
4 5 2
1 2 30
2 3 20
3 4 10
4 1 40
2 4 50
1 3
1 2
Sample Output
7
9
8
7
20
40

#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std;
#define Lson l,m,rt<<1 #define RSON m+1,r,rt<<1|1 const int maxn=2e5+9;
struct Edge {int u,v,w;} E[MAXN],EE[MAXN];

int cmp (Edge A,edge b) {return a.w>b.w;} int n,m,s,a,b;
int SZ[MAXN],FA[MAXN],TP[MAXN],ID[MAXN],DEP[MAXN],SON[MAXN],IDX,V[MAXN];
int val[maxn<<2],pre[maxn],eused=0;
Vector<int> MP[MAXN];
    int ff (int x) {return x==pre[x]?x:pre[x]=ff (pre[x]);} void MST () {sort (e+1,e+m+1,cmp);
        for (int i=1; i<=m; i++) {int u=ff (E[I].U);
        int v=ff (E[I].V);
            if (u!=v) {pre[u]=v;
            Ee[++eused]=e[i];
            Mp[e[i].u].push_back (E[I].V);
         Mp[e[i].v].push_back (E[I].U);
        printf ("------%d%d\n", E[I].U,E[I].V);
    }}} void dfs1 (int u,int f,int d) {sz[u]=1;
    Fa[u]=f;
    Dep[u]=d;
    son[u]=0; for (int i=0; i<mp[u].sizE ();
        i++) {int v=mp[u][i];
        if (V==fa[u]) continue;
        DFS1 (v,u,d+1);
        SZ[U]+=SZ[V]; if (son[u]==0| |
    SZ[SON[U]]&LT;SZ[V]) Son[u]=v;
    }} void dfs2 (int u,int TPP) {TP[U]=TPP;
    Id[u]=++idx;
    if (Son[u]) DFS2 (SON[U],TPP);
        for (int i=0; i<mp[u].size (); i++) {int v=mp[u][i]; if (v==fa[u]| |
        V==son[u]) continue;
    DFS2 (V,V); }} void Pushup (int rt) {val[rt]=min (val[rt<<1],val[rt<<1|1]),} void Build (int l,int R,int RT) {if (
        L==R) {val[rt]=v[l];
    Return
    } int m=l+r>>1;
    Build (Lson);
    Build (Rson);
Pushup (RT);
    } int query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) return VAL[RT];
    int m=l+r>>1;
    int ans=0x3f3f3f3f;
    if (l<=m) ans=min (Ans,query (L,r,lson));
    if (m<r) ans=min (Ans,query (L,r,rson));
return ans;
   } int qq (int u,int v) {//printf ("---%d%d\n", u,v); int T1=TP[U],T2=TP[V];
    int ans=0x3f3f3f3f;
            while (T1!=T2) {if (Dep[t1]<dep[t2]) {swap (U,V);
        Swap (T1,T2);
        } ans=min (Ans,query (id[t1],id[u],1,idx,1));
        U=FA[T1];
    T1=tp[u];
        } if (U!=v) {if (Dep[u]>dep[v]) swap (U,V);
    Ans=min (Ans,query (id[son[u]],id[v],1,idx,1));
} return ans;
        } int main () {while (~scanf ("%d%d%d", &n,&m,&s)) {idx=eused=0;
            for (int i=1; i<=n; i++) {pre[i]=i;
        Mp[i].clear ();

        } for (int i=1; i<=m; i++) scanf ("%d%d%d", &AMP;E[I].U,&AMP;E[I].V,&AMP;E[I].W);
        MST ();
        DFS1 (1,0,1);

        DFS2 (a);
            for (int i=1; i<=eused; i++) {if (DEP[EE[I].U]&LT;DEP[EE[I].V]) swap (EE[I].U,EE[I].V);
        V[ID[EE[I].U]]=EE[I].W;
        } build (1,idx,1); while (s--) {scanf ("%d%d", &a,&AMP;B);
        printf ("%d\n", QQ (A, b));
}} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.