SDOI2015 Treasure Hunt | noi.ac # 460 tree

Topic links: poke me

May know, we have the equivalent of the treasure in a place surrounded by a circle, find the smallest circle is how much.
Obviously dfs order according to traverse is minimal.

Then we first dfs sequence again, and the distance to each point out the pretreatment of the root (and we can use \ (dis [u] + dis [v] -2 * dis [lca (u, v)] \) to indicate the distance between the u, v)

How to maintain the dynamic of this thing? Balanced tree? It does not exist, open a set on the line. Every maintain this Add or delete the impact generated on the line.

Similar entitled noi.ac # 460 Tree -

Give you a tree of n points, each point has a color ci.
There m operations, each operation will change the color of a point or ask Unicom block contains the minimum of all the color points of a number of edges contained.

Communication block contains the minimum number of edges, i.e. the question ans / 2. The number of colors will not have a decisive impact on the time complexity. QAQ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
#define MAXN 1000010
#define ll long long
using namespace std;
int n,m,t,tim;
int head[MAXN];
int dfn[MAXN],low[MAXN],son[MAXN],siz[MAXN],dep[MAXN],fa[MAXN],top[MAXN];
ll ans;
ll dis[MAXN];
set<int>s;
struct Edge{int nxt,to;ll dis;}edge[MAXN<<1];
inline void add(int from,int to,ll dis)
{
    edge[++t].nxt=head[from],edge[t].to=to,edge[t].dis=dis;
    head[from]=t;
}
inline void dfs1(int x,int pre)
{
    siz[x]=1;
    fa[x]=pre;
    dep[x]=dep[pre]+1;
    int maxx=-1;
    for(int i=head[x];i;i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(v==pre) continue;
        dis[v]=dis[x]+edge[i].dis;
        dfs1(v,x);
        siz[x]+=siz[v];
        if(siz[v]>maxx) maxx=siz[v],son[x]=v;
    }
}
inline void dfs2(int x,int topf)
{
    top[x]=topf;
    dfn[x]=++tim;
    low[tim]=x;
    if(son[x]) dfs2(son[x],topf);
    for(int i=head[x];i;i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}
inline int get_lca(int u,int v)
{
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        u=fa[top[u]];
    }
    if(dep[u]<dep[v]) return u;
    else return v;
}
inline ll get_dis(int u,int v){return dis[u]+dis[v]-2*dis[get_lca(u,v)];}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w),add(v,u,w);
    }
    dfs1(1,0);
    dfs2(1,1);
    // for(int i=1;i<=n;i++) printf("dis[%d]=%lld\n",i,dis[i]); puts("");
    // for(int i=1;i<=n;i++) printf("dfn[%d]=%d\n",i,dfn[i]); puts("");
    // for(int i=1;i<=n;i++) printf("low[%d]=%d\n",i,low[i]); puts("");
    // for(int i=1;i<=n;i++) printf("dfn[%d]=%d\n",i,dfn[i]); puts("");
    // for(int i=1;i<=n;i++) printf("low[%d]=%d\n",i,low[i]); puts("");
    // for(int i=1;i<=n;i++)
    //  for(int j=i+1;j<=n;j++)
    //      printf("lca[%d %d]=%d\n",i,j,get_lca(i,j));
    set<int>::iterator it,it1,it2;
    while(m--)
    {
        int x;
        scanf("%d",&x);
        if(!s.count(dfn[x]))
        {
            s.insert(dfn[x]);
            it=it1=it2=s.find(dfn[x]);
            it1--,it2++;
            if(it!=s.begin()) ans+=get_dis(x,low[*it1]);
            if(it2!=s.end()) ans+=get_dis(x,low[*it2]);
            if(it!=s.begin()&&it2!=s.end()) ans-=get_dis(low[*it1],low[*it2]);
        }
        else
        {
            it=it1=it2=s.find(dfn[x]);
            it1--,it2++;
            if(it!=s.begin()) ans-=get_dis(x,low[*it1]);
            if(it2!=s.end()) ans-=get_dis(x,low[*it2]);
            if(it!=s.begin()&&it2!=s.end()) ans+=get_dis(low[*it1],low[*it2]);
            s.erase(dfn[x]);
        }
        if(s.size()<=1)
        {
            printf("0\n");
            continue;
        }
        it=s.end();--it;
        printf("%lld\n",ans+get_dis(low[*s.begin()],low[*it]));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fengxunling/p/11027155.html
Recommended