树剖35pts,剩下全RE求助,码风极度良好(确信
查看原帖
树剖35pts,剩下全RE求助,码风极度良好(确信
357237
Unknown__楼主2024/8/13 15:42
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#define debug(x) cout<<x<<endl;
#define ls (s << 1)
#define rs (s << 1 | 1)
using namespace std;
typedef long long ll;
const ll N = 401010;
struct EDGE{
    ll from,to,val;
    bool operator < (const EDGE& X)const
    {
        return val < X.val;
    }
}A[N];
struct node{
    ll l,r,val,se;
}T[N << 1];
set<pair<ll,ll> > book1;
vector<pair<ll,ll> > v[N],v1[N];
bool book[N],vis[N];
ll n,m,val[N],f[N];
ll siz[N],ze[N],top[N],dep[N],fa[N],id[N],rev[N],tot,now,minn;
ll find(ll x){return f[x] == x ? x : f[x] = find(f[x]);}
void dfs1(ll p,ll fat,ll depth)
{
    fa[p] = fat,dep[p] = depth,siz[p]++;
    for(auto tmp : v[p])
    {
        ll k = tmp.first;
        if(k == fat)continue;
        val[k] = tmp.second;
        dfs1(k,p,depth + 1);
        siz[p] += siz[k];
        if(siz[ze[p]] < siz[k])ze[p] = k;
    }
}
void dfs2(ll p,ll fat,ll rt)
{
    top[p] = rt,id[p] = ++tot,rev[tot] = p;
    if(ze[p])dfs2(ze[p],p,rt);
    for(auto tmp : v[p])
    {
        ll k = tmp.first;
        if(k == fat || k == ze[p])continue;
        dfs2(k,p,k);
    }
}
void pushup(node& A,node B,node C)
{
    ll tmp[4] = {};
    tmp[0] = B.val,tmp[1] = B.se,tmp[2] = C.val,tmp[3] = C.se;
    sort(tmp,tmp + 4);
    A.val = tmp[3];
    ll opt = 2;
    while(opt >= 0 && A.val == tmp[opt])opt--;
    if(opt == -1) A.se = -1e17;
    else A.se = tmp[opt];
}
void build(ll s,ll l,ll r)
{
    T[s] = (node){l,r,0,(ll)-1e17};
    if(l == r)
    {
        T[s].val = val[rev[l]];
        return;
    }
    ll mid = (l + r) >> 1;
    build(ls,l,mid);
    build(rs,mid + 1,r);
    pushup(T[s],T[ls],T[rs]);
}
node query(ll s,ll l,ll r)
{
    if(T[s].l == l && T[s].r == r)return T[s];
    ll mid = (T[s].l + T[s].r) >> 1;
    if(l > mid)return query(rs,l,r);
    else if(r <= mid)return query(ls,l,r);
    else
    {
        node ml,mr,ms;
        ml = query(ls,l,mid),mr = query(rs,mid + 1,r);
        pushup(ms,ml,mr);
        return ms;
    }
}
node Tquery(ll x,ll y)
{
    node ans;
    ans = (node){0,0,0,(ll)-1e17};
    while(dep[top[x]] != dep[top[y]])
    {
        if(dep[top[x]] > dep[top[y]])
        {
            pushup(ans,ans,query(1,id[top[x]],id[x]));
            x = fa[top[x]];
        }
        else
        {
            pushup(ans,ans,query(1,id[top[y]],id[y]));
            y = fa[top[y]];
        }       
    }
    if(dep[x] > dep[y]) pushup(ans,ans,query(1,id[y] + 1,id[x]));
    else if(dep[x] < dep[y]) pushup(ans,ans,query(1,id[x] + 1,id[y]));
    return ans;
}
int main()
{
    cin>>n>>m;
    ll i;
    for(i = 1;i <= n;i++)f[i] = i;
    for(i = 1;i <= m;i++)
    {
        ll a,b,c;cin>>a>>b>>c;
        A[i] = (EDGE){a,b,c};
    }
    sort(A + 1,A + m + 1);
    ll cnt = 0;
    for(i = 1;i <= m;i++)
    {
        ll nx = find(A[i].from),ny = find(A[i].to);
        if(nx == ny)continue;
        book[i] = 1;
        minn += A[i].val;
        f[nx] = ny;
        cnt++;
        v[A[i].from].push_back(make_pair(A[i].to,A[i].val));
        v[A[i].to].push_back(make_pair(A[i].from,A[i].val));
        if(cnt == n - 1)break;
    }
    dfs1(1,0,0);
    dfs2(1,0,1);
    build(1,1,n);
    now = 1e17;
    for(i = 1;i <= m;i++)
        if(!book[i] && A[i].from != A[i].to)
        {
            node mi = Tquery(A[i].from,A[i].to);
            ll sum = minn - mi.val + A[i].val;
            if(sum == minn)sum = minn - mi.se + A[i].val;
            if(sum > minn)now = min(now,sum);
        }
    cout<<now;
    return 0;
}
2024/8/13 15:42
加载中...