Codeforces603E

Portal

Description

初始时有(n(nleq10^5))个孤立的点,依次向图中加入(m(mleq3 imes10^5))条带权无向边。使得图中每个点的度数均为奇数的边集是合法的,其权值定义为集合中的最大边权。每次加入边后,询问权值最小的合法边集的权值,不存在合法边集时输出(-1)

Solution

存在合法边集 (Leftrightarrow) 每个连通块的大小均为偶数。如果某连通块大小为奇数,那么该块的总度数是奇数,但一条无向边会提供两个度数,所以不存在合法边集。如果大小为偶数,那么可以按照如下的方式构造合法边集:

求出该连通块的一棵生成树。由深到浅观察每个点(u),如果(u)({ch_u})的边中有偶数个在边集中,那么将边((u,fa[u]))加入边集。
做到根的时候,因为除了根以外有奇数个度数为奇数的点,但总度数为偶数,所以根的度数必然是奇数。

该方法将每个偶数连通块划分为了若干个偶数连通块。对于划分后的每个连通块,其最小生成树就是其权值最小的合法边集。
那么现在我们可以进行操作了。首先开一个set<pair<int,int>>记录lct中的边,first记录边权,second记录边的编号。初始用(cnt)表示剩余奇数连通块的个数,在其减至(0)前一直输出(-1)
当存在合法边集后,加入边((u,v))时,若(u,v)已经连通则维护最小生成树,否则在lct中连接((u,v)),同时维护set
如果该边被加入lct,那么尝试获得更优答案。从set中最大的边开始,检查能否移除这条边;若能移除则移除并检查次大,不能则break。因为如果当前的最大边不能移除,答案就不会变的更优了。最后输出set中最大的边权。

时间复杂度(O(mlog(n+m)))

Code

//Pastoral Oddities
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
typedef pair<int,int> Ipair;
inline char gc()
{
    static char now[1<<16],*S,*T;
    if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
    return *S++;
}
inline int read()
{
    int x=0; char ch=gc();
    while(ch<'0'||'9'<ch) ch=gc();
    while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x;
}
int const N=4e5+10;
int const INF=0x3F3F3F3F;
int n,m;
struct edge{int u,v;} ed[N];
set<Ipair> st;
int fa[N],ch[N][2],siz[N],isiz[N];
int val[N],maxL[N]; bool rev[N];
int wh(int p) {return p==ch[fa[p]][1];}
int notRt(int p) {return p==ch[fa[p]][wh(p)];}
void update(int p)
{
    int ch0=ch[p][0],ch1=ch[p][1];
    siz[p]=siz[ch0]+1+siz[ch1]+isiz[p];
    int maxP=max(val[p],max(val[maxL[ch0]],val[maxL[ch1]]));
    if(val[p]==maxP) maxL[p]=p;
    else if(val[maxL[ch0]]==maxP) maxL[p]=maxL[ch0];
    else if(val[maxL[ch1]]==maxP) maxL[p]=maxL[ch1];
}
void pushdw(int p) {if(rev[p]) swap(ch[p][0],ch[p][1]),rev[ch[p][0]]^=1,rev[ch[p][1]]^=1,rev[p]=false;}
void rotate(int p)
{
    int q=fa[p],r=fa[q],w=wh(p);
    fa[p]=r; if(notRt(q)) ch[r][wh(q)]=p;
    fa[ch[q][w]=ch[p][w^1]]=q;
    fa[ch[p][w^1]=q]=p;
    update(q),update(p);
}
void pushRt(int p) {if(notRt(p)) pushRt(fa[p]); pushdw(p);}
void splay(int p)
{
    pushRt(p);
    for(int q=fa[p];notRt(p);rotate(p),q=fa[p]) if(notRt(q)) rotate(wh(p)^wh(q)?p:q);
}
void access(int p) {for(int q=0;p;q=p,p=fa[p]) splay(p),isiz[p]+=siz[ch[p][1]]-siz[q],ch[p][1]=q,update(p);}
void makeRt(int p) {access(p),splay(p),rev[p]^=1;}
void path(int p,int q) {makeRt(p),access(q),splay(q);}
void link(int i)
{
    int p=ed[i].u,q=ed[i].v; makeRt(p),makeRt(q);
    fa[p]=n+i,isiz[n+i]+=siz[p]; update(n+i);
    fa[n+i]=q,isiz[q]+=siz[n+i]; update(q);
}
void clear(int p) {fa[p]=ch[p][0]=ch[p][1]=0,isiz[p]=0; update(p);}
void cut(int i)
{
    int p=ed[i].u,q=ed[i].v; path(p,q);
    fa[p]=ch[p][1]=0; ch[q][0]=0;
    clear(n+i); update(p),update(q);
}
int find(int p) {access(p),splay(p); while(ch[p][0]) p=ch[p][0]; return p;}
int odd(int p) {return siz[p]+1>>1&1;}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++) val[i]=-INF,update(i);
    for(int i=1,cnt=n;i<=m;i++)
    {
        int u=read(),v=read(),w=read();
        ed[i].u=u,ed[i].v=v; val[n+i]=w,maxL[n+i]=n+i;
        if(find(u)==find(v))
        {
            path(u,v); int id=maxL[v]-n,w0=val[id+n];
            if(w<w0) cut(id),link(i),st.erase(make_pair(w0,id));
            else {printf("%d
",cnt?-1:st.rbegin()->first); continue;}
        }
        else
        {
            makeRt(u),cnt-=odd(u); makeRt(v),cnt-=odd(v);
            link(i),cnt+=odd(v);
        }
        st.insert(make_pair(val[i+n],i));
        if(cnt) {puts("-1"); continue;}
        while(true)
        {
            int id=st.rbegin()->second; u=ed[id].u,v=ed[id].v;
            path(u,v); if(odd(u)) break;
            cut(id); st.erase(*st.rbegin());
        }
        printf("%d
",st.rbegin()->first);
    }
    return 0;
}

P.S.

第一次用set,不大懂...%%%zhx

原文地址:https://www.cnblogs.com/VisJiao/p/Cf603E.html