AGC 014 E Blue and Red Tree [树链剖分]

传送门


思路

官方题解是倒推,这里提供一种正推的做法。

不知道你们是怎么想到倒推的……感觉正推更好想啊QwQ就是不好码

把每一条红边,将其转化为蓝树上的一条路径。为了连这条红边,需要保证这条路径仍然完整。

考虑连完之后要删掉的那条蓝边,显然它只能被当前连的红路径覆盖而没有被其他路径覆盖,否则就玩不下去了。

即:每次只能删掉一条被覆盖一次的蓝边。

有没有可能一条蓝边没有被覆盖过呢?显然是不可能的,因为每条蓝边最终都要删去,若没有覆盖过则无法删去。

那么做法就很显然了:树剖,线段树维护最小值和覆盖了这个区间的红边编号。每次找一个只被覆盖了一次的蓝边,删掉它(将它的值赋为\(\infty\)),也去除那条红边的影响。如果能做到最后就是合法,否则不合法。

然而我太久没写树剖,而且\(n\)和\(n-1\)总分不清,结果贡献了一页的评测记录……

而且在毒瘤的洛谷上选c++11交到AtCoder上就变成了c++14……对AtCoder不熟,各种奇怪的错误都来了。

代码巨丑,凑合着看吧QwQ


代码

#include<bits/stdc++.h>
namespace my_std{
    using namespace std;
    #define pii pair<int,int>
    #define fir first
    #define sec second
    #define MP make_pair
    #define rep(i,x,y) for (int i=(x);i<=(y);i++)
    #define drep(i,x,y) for (int i=(x);i>=(y);i--)
    #define go(x) for (int i=head[x];i;i=edge[i].nxt)
    #define sz 101010
    typedef long long ll;
    template<typename T>
    inline void read(T& t)
    {
        t=0;char f=0,ch=getchar();
        double d=0.1;
        while(ch>‘9‘||ch<‘0‘) f|=(ch==‘-‘),ch=getchar();
        while(ch<=‘9‘&&ch>=‘0‘) t=t*10+ch-48,ch=getchar();
        if(ch==‘.‘)
        {
            ch=getchar();
            while(ch<=‘9‘&&ch>=‘0‘) t+=d*(ch^48),d*=0.1,ch=getchar();
        }
        t=(f?-t:t);
    }
    template<typename T,typename... Args>
    inline void read(T& t,Args&... args){read(t); read(args...);}
//  void file()
//  {
//      #ifndef ONLINE_JUDGE
//      freopen("a.txt","r",stdin);
//      #endif
//  }
//  inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
//  clock_t t=clock();

int n;
struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
    edge[++ecnt]=(hh){t,head[f]};
    head[f]=ecnt;
    edge[++ecnt]=(hh){f,head[t]};
    head[t]=ecnt;
}
int size[sz],son[sz],top[sz],fa[sz],dep[sz],dfn[sz],cnt=-1;
#define v edge[i].t
void dfs1(int x,int fa)
{
    dep[x]=dep[::fa[x]=fa]+1;size[x]=1;
    go(x) if (v!=fa)
    {
        dfs1(v,x);
        size[x]+=size[v];
        if (size[v]>size[son[x]]) son[x]=v;
    }
}
void dfs2(int x,int fa,int tp)
{
    top[x]=tp;dfn[x]=++cnt;
    if (son[x]) dfs2(son[x],x,tp);
    go(x) if (v!=fa&&v!=son[x]) dfs2(v,x,v);
}
#undef v

bool del[sz];
int F[sz],T[sz];
int mn[sz<<2],tag[sz<<2];
vector<int>tr[sz<<2];
#define ls k<<1
#define rs k<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
void Add(int k,int t){tag[k]+=t;mn[k]+=t;}
void pushdown(int k){int &t=tag[k]; if (!t) return; Add(ls,t); Add(rs,t); t=0; return;}
void pushup(int k){mn[k]=min(mn[ls],mn[rs]);}
void add(int k,int l,int r,int x,int y,int t)
{
    if (x<=l&&r<=y) return tr[k].push_back(t),Add(k,1);
    pushdown(k);
    int mid=(l+r)>>1;
    if (x<=mid) add(lson,x,y,t);
    if (y>mid) add(rson,x,y,t);
    pushup(k);
}
void change(int k,int l,int r,int x,int y,int s)
{
    if (x<=l&&r<=y) return Add(k,s);
    pushdown(k);
    int mid=(l+r)>>1;
    if (x<=mid) change(lson,x,y,s);
    if (y>mid) change(rson,x,y,s);
    pushup(k);
}
int K;
int getmn(int k,int l,int r)
{
    if (mn[k]>1) return -1; // -1 : don‘t have 1
    int ret;
    while (!tr[k].empty()&&del[tr[k].back()]) tr[k].pop_back();
    ret=tr[k].empty()?0:tr[k].back(); // 0 : don‘t know which edge
    if (l==r) return K=l,ret;
    pushdown(k);
    int mid=(l+r)>>1,cur;
    cur=getmn(lson); if (cur!=-1) { if (cur) return cur; if (ret) return ret; }
    cur=getmn(rson); if (cur!=-1) { if (cur) return cur; if (ret) return ret; }
    return ret;
}

void add(int x,int y,int t)
{
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        add(1,1,n-1,dfn[top[x]],dfn[x],t);
        x=fa[top[x]];
    }
    if (dfn[x]>dfn[y]) swap(x,y);
    if (x!=y) add(1,1,n-1,dfn[x]+1,dfn[y],t);
}
void erase(int x,int y)
{
    while (top[x]!=top[y])
    {
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        change(1,1,n-1,dfn[top[x]],dfn[x],-1);
        x=fa[top[x]];
    }
    if (dfn[x]>dfn[y]) swap(x,y);
    if (x!=y) change(1,1,n-1,dfn[x]+1,dfn[y],-1);
}
void del_mn()
{
    if (mn[1]!=1) puts("NO"),exit(0);
    int e=getmn(1,1,n-1);
    del[e]=1;
    change(1,1,n-1,K,K,sz);
    erase(F[e],T[e]);
}

int main()
{
//  file();
    int x,y;
    scanf("%d",&n);
    rep(i,1,n-1) scanf("%d %d",&x,&y),make_edge(x,y);
    dfs1(1,0);dfs2(1,0,1);
    rep(i,1,n-1) scanf("%d %d",&x,&y),add(x,y,i),F[i]=x,T[i]=y;
    rep(i,1,n-1) del_mn();
    puts("YES");
//  cout<<1.0*(clock()-t)/CLOCKS_PER_SEC;
    return 0;
}

原文地址:https://www.cnblogs.com/p-b-p-b/p/10355976.html

时间: 2024-11-01 14:39:05

AGC 014 E Blue and Red Tree [树链剖分]的相关文章

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

spoj 375 Query on a tree (树链剖分)

Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to ti or Q

SPOJ Query on a tree 树链剖分 水题

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

poj 3237 Tree 树链剖分+线段树

Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions

spoj Query on a tree(树链剖分模板题)

375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

【BZOJ-4353】Play with tree 树链剖分

4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][Discuss] Description 给你一棵包含N个节点的树,设每条边一开始的边权为0,现在有两种操作: 1)给出参数U,V,C,表示把U与V之间的路径上的边权变成C(保证C≥0) 2)给出参数U,V,C,表示把U与V之间的路径上的边权加上C.但是如果U至V之间路径某条边的边权加上C小于0,那