jzoj.4596 Stree

Source: Internet
Author: User
Tags first row
problem Description

Given n points, M bar has the right edge, now for each edge, you need to answer the total edge weight of the smallest spanning tree containing this edge. Input

First row two numbers n,m
Next m line i,j,k, indicating that I and J have a weighted value of K edge Output

M-line answer Sample Input

5 7
1 2 3
1 3 1
1 4 5
2 3 2
2 5 3
3 4 2
4 5 4 Sample Output

9
8
11
8
8
8
9 Data Constraint

30% n<=1000
100% n,m<=200000 Solution

Note: MST is the smallest spanning tree.
First we will find the minimum spanning tree, then if the queried edge is in the smallest spanning tree, then the answer is the value of MST and. Otherwise, let's think about it, the tree is connected by the N-point n-1, and if this side is added, the tree becomes a figure. So we're going to delete one of the edges of this tree, plus the one we're asking. Then obviously, the edge that is going to be deleted is the maximum weight of the minimum distance of two points of the queried side. Let's just ask for LCA.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath
> #include <algorithm> #define LL Long long #define N 200010 using namespace std;
ll Deep[n],go[n*2],head[n*2],next[n*2],v[n*2],f[n],tr[n*2][2];
int I,j,k,l,n,m,t,f1,f2,tot;
ll Ans,sum,temp;
struct note{ll x, Y, Z,};
Note A[n],b[n];
    BOOL CNT (note x,note y) {return x.z<y.z;} int get (int n) {if (f[n]==n) return n;
        {F[n]=get (f[n]);
    return f[n];
    }} void lb (ll x,ll y,ll z) {go[++tot]=y;
    NEXT[TOT]=HEAD[X];
    Head[x]=tot;
V[tot]=z;
    } void bs (int x,int y,int z) {deep[x]=z;
    int i;
        for (I=head[x];i;i=next[i]) {int now=go[i];
            if (now!=y) {tr[now][0]=x;
            Tr[now][1]=v[i];
        BS (NOW,X,Z+1);
    }}} ll LCA (int x,int y) {ll mx=-2147483647;
    if (Deep[x]<deep[y]) swap (x, y); while (Deep[x]>deep[y]) {Mx=max (mx,tr[x][1]);
        X=TR[X][0];
    } if (x==y) return MX;
        while (x!=y) {Mx=max (mx,tr[x][1]);
        Mx=max (mx,tr[y][1]);
        X=TR[X][0];
    Y=TR[Y][0];
} return MX;
    } int main () {scanf ("%d%d", &n,&m);
    for (i=1;i<=m;i++) scanf ("%lld%lld%lld", &a[i].x,&a[i].y,&a[i].z);
    for (i=1;i<=n;i++) f[i]=i;
    memcpy (B,a,sizeof (a));
    Sort (a+1,a+m+1,cnt);
    k=tot=0;
        for (i=1;i<=m;i++) {f1=get (a[i].x);
        F2=get (A[I].Y);
            if (F1!=F2) {f[f2]=f1;
            Sum+=a[i].z;
            LB (a[i].x,a[i].y,a[i].z);
            LB (a[i].y,a[i].x,a[i].z);
        k++;
    } if (k==n) break;
    } BS (1,0,0);
        for (i=1;i<=m;i++) {Temp=lca (B[I].X,B[I].Y);
        Ans=sum-temp+b[i].z;
    printf ("%lld\n", ans); }
}

--2016.7.9

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.