1. 程式人生 > >BZOJ 1977 嚴格次小生成樹

BZOJ 1977 嚴格次小生成樹

題目連結

題意:

求一個圖的嚴格次小生成樹,n<=100000,m<=300000,邊權非負且<=1e9

思路:

按照正常次小生成樹思路,先生成一顆最小生成樹,並記錄任意兩點路徑上的最長邊,然後列舉非最小樹邊來求得答案,但現在要求的是嚴格次小生成樹所以我們還得知道兩點路徑上的嚴格次長邊

我們考慮使用LCA維護任意兩點路徑上的最長邊和次長邊,至於怎麼維護看程式碼了

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100010;
const int maxm = 300010;
const LL  inf  = 1LL<<60;
int pre[maxn];
LL Max( LL a , LL b ){ return a>b?a:b; }
LL Min( LL a , LL b ){ return a<b?a:b; }
int Max( int a , int b ){ return a>b?a:b; }
int Min( int a , int b ){ return a<b?a:b; }
int Find( int x ){ return x==pre[x]?pre[x]:pre[x]=Find(pre[x]); }
int n,m,tol,head[maxn];
struct edge
{
    int to,cost,next;
}es[maxm];
void addedge( int u , int v , int w )
{
    es[tol].to = v;
    es[tol].cost = w;
    es[tol].next = head[u];
    head[u] = tol++;
}
struct node
{
    int u,v,w,used;
    friend bool operator<( const node&a , const node&b )
    {
        return a.w<b.w;
    }
}data[maxm];
int dep[maxn],fa[maxn][20],maxx[maxn][20],minx[maxn][20];
void dfs( int u , int f )
{
    for ( int i=head[u] ; i!=-1 ; i=es[i].next )
    {
        int v = es[i].to;
        if ( v!=f )
        {
            dep[v] = dep[u]+1;
            fa[v][0] = u;
            maxx[v][0] = es[i].cost;
            minx[v][0] = -1;
            dfs( v , u );
        }
    }
}
int Lca( int u , int v )
{
    if ( dep[u]<dep[v] ) swap ( u , v );
    int d = dep[u]-dep[v];
    for ( int i=19 ; i>=0 ; i-- )
        if ( d&(1<<i) ) u = fa[u][i];
    if ( u==v ) return u;
    for ( int i=19 ; i>=0 ; i-- )
        if ( fa[u][i]!=fa[v][i] )
        {
            u = fa[u][i];
            v = fa[v][i];
        }
    return fa[u][0];
}
int main()
{
    scanf ( "%d%d" , &n , &m );
    for ( int i=1 ; i<=m ; i++ )
        scanf ( "%d%d%d" , &data[i].u , &data[i].v , &data[i].w );
    sort ( data+1 , data+m+1 );
    LL ans = inf,tot = 0;
    for ( int i=1 ; i<=n ; i++ )
        pre[i] = i;
    tol = 0; memset ( head , -1 , sizeof(head) );
    for ( int i=1 ; i<=m ; i++ )
    {
        int fx = Find(data[i].u);
        int fy = Find(data[i].v);
        if ( fx!=fy )
        {
            tot += data[i].w;
            pre[fy] = fx;
            addedge( data[i].u , data[i].v , data[i].w );
            addedge( data[i].v , data[i].u , data[i].w );
            data[i].used = 1;
        }
        else
            data[i].used = 0;
    }
    dep[1] = 1,fa[1][0] = 0,maxx[1][0] = -1,minx[1][0] = -1;
    dfs( 1 , 0 );
    for ( int j=1 ; j<20 ; j++ )
        for ( int i=1 ; i<=n ; i++ )
        {
            fa[i][j] = fa[fa[i][j-1]][j-1];
            maxx[i][j] = Max( maxx[i][j-1] , maxx[fa[i][j-1]][j-1] );
            minx[i][j] = Max( minx[i][j-1] , minx[fa[i][j-1]][j-1] );
            if ( maxx[i][j-1]>maxx[fa[i][j-1]][j-1]&&maxx[fa[i][j-1]][j-1]>minx[i][j] ) minx[i][j] = maxx[fa[i][j-1]][j-1];
            if ( maxx[fa[i][j-1]][j-1]>maxx[i][j-1]&&maxx[i][j-1]>minx[i][j] ) minx[i][j] = maxx[i][j-1];
        }
    for ( int i=1 ; i<=m ; i++ )
    {
        if ( data[i].used ) continue;
        int u = data[i].u,v = data[i].v,w = data[i].w;
        int lca = Lca( u , v );
        int lmax = -1,rmax = -1;
        int ld = dep[u]-dep[lca];
        int rd = dep[v]-dep[lca];
        for ( int i=19 ; i>=0 ; i-- )
            if ( ld&(1<<i) )
            {
                if ( maxx[u][i]==w ) lmax = Max( lmax , minx[u][i] );
                else lmax = Max( lmax , maxx[u][i] );
                u = fa[u][i];
            }
        for ( int i=19 ; i>=0 ; i-- )
            if ( rd&(1<<i) )
            {
                if ( maxx[v][i]==w ) rmax = Max( rmax , minx[v][i] );
                else rmax = Max( rmax , maxx[v][i] );
                v = fa[v][i];
            }
        ans = Min ( ans , tot-Max(lmax,rmax)+w );
    }
    printf ( "%lld\n" , ans );
    return 0;
}