P14411 [JOISC 2015] 道路建设 / Road Development
part0 前言
- 这篇代码码风可能不太良好,但注释很详细,欢迎提出建议!
- 作者很懒,所以不想提供部分分思路(
作者找打)
part1 解题思路
不难发现,在任意时刻,当前的图一定是一片森林。考虑建立一个超级源点和森林相连,这样就变成了一颗树。
于是, 操作等价于建边/区间推平, 操作等价于区间查询,可以通过树链剖分用线段树维护。
柯学家请注意
本题使用珂朵莉树无法通过。详情见此提交记录
本题需要判断两点是否在同一颗树上,考虑用并查集维护联通树。
由于树剖需要先建完图再实现,我们可以将操作离线后统一处理。
part2 AC 代码
细节在代码中有注释。
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=j;i<=k;i++)
#define dFor(i,j,k) for(int i=j;i>=k;i--)
#define FIO(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout);
#define int long long
const int inf=1e18;
const int Mmod=1e9+7;
const int MAXN=100005;
using namespace std;
namespace DEBUG{
template<class T> void _debug(const char *s, T x) {cout<<s<<'='<<x<<endl;}
template<class F, class... Nxt> void _debug(const char *s, F x, Nxt... nxt){
int d=0;
while(*s!=',' || d) d+=*s=='(', d-=*s==')', cout<<*s++;
cout<<'='<<x<<',';
_debug(s+1, nxt...);
}
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
}
using namespace DEBUG;
int n,Q,fat[MAXN];
bitset<MAXN>vis;
vector<int>e[MAXN];
int getf(int x){return x==fat[x]?x:fat[x]=getf(fat[x]);} //路径压缩并查集
inline void add(int u,int v){e[u].emplace_back(v),e[v].emplace_back(u);} //建边
struct Query{int t,a,b;bool op;}q[MAXN*3];
int fa[MAXN],depth[MAXN],dfn[MAXN],top[MAXN],son[MAXN],Size[MAXN],Time=0;
void dfs(int u){vis[u]=true;for(int v:e[u]) if(v!=fa[u]) fa[v]=u,dfs(v);} //遍历树
void dfs1(int u){ //重链剖分预处理
Size[u]=1,depth[u]=depth[fa[u]]+1;
for(int v:e[u]){
if(v==fa[u]) continue;
dfs1(v),Size[u]+=Size[v];
if(Size[v]>Size[son[u]]) son[u]=v;
}
}
void dfs2(int u,int Top){
top[u]=Top,dfn[u]=++Time;
if(!son[u]) return ;
dfs2(son[u],Top);
for(int v:e[u]) if(v!=son[u]&&v!=fa[u]) dfs2(v,v);
}
struct SegmentTree{int l,r,v;bool c;}tree[MAXN<<2];
#define tl tree[now].l
#define tr tree[now].r
#define tv tree[now].v
#define tc tree[now].c
#define lson now<<1
#define rson now<<1|1
inline void push_up(int now){tv=tree[lson].v+tree[rson].v;}
inline void cover(int now){tv=0,tc=true;}
inline void push_down(int now){if(tc==true) cover(lson),cover(rson),tc=false;}
void build(int now,int l,int r){
tl=l,tr=r;
if(l==r) return tv=1,void();
int mid=l+r>>1;
build(lson,l,mid),build(rson,mid+1,r);
push_up(now);
}
void assign(int now,int l,int r){
if(tl==l&&tr==r) return cover(now),void();
push_down(now);
int mid=tl+tr>>1;
if(r<=mid) assign(lson,l,r);
else if(l>=mid+1) assign(rson,l,r);
else assign(lson,l,mid),assign(rson,mid+1,r);
push_up(now);
}
int query(int now,int l,int r){
if(tl==l&&tr==r) return tv;
push_down(now);
int mid=tl+tr>>1;
if(r<=mid) return query(lson,l,r);
else if(l>=mid+1) return query(rson,l,r);
else return query(lson,l,mid)+query(rson,mid+1,r);
}
inline void modify_range(int x,int y,int w=1){
while(top[x]!=top[y]){
if(depth[top[x]]<depth[top[y]]) swap(x,y);
assign(1,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if(dfn[x]>dfn[y]) swap(x,y);
if(dfn[x]!=dfn[y]) assign(1,dfn[x]+1,dfn[y]);
}
inline int query_range(int x,int y){
int res=0;
while(top[x]!=top[y]){
if(depth[top[x]]<depth[top[y]]) swap(x,y);
res+=query(1,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if(dfn[x]>dfn[y]) swap(x,y);
if(dfn[x]!=dfn[y]) res+=query(1,dfn[x]+1,dfn[y]);
return res;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//FIO("");
cin>>n>>Q;
For(i,1,n) fat[i]=i;
For(i,1,Q){
auto&[t,a,b,op]=q[i]; //c++17的auto 简化代码
cin>>t>>a>>b;int fx=getf(a),fy=getf(b);
switch(t){
case 1:{ //计划被实施
if(fx==fy) op=true; //铺设路面
else op=false,fat[fx]=fy,add(a,b); //建边
break;
}
case 2:{ //计划未被实施
if(fx==fy) op=true; //会导致铺设路面,合法
else op=false; //会导致新建道路,不合法
break;
}
}
}
For(i,1,n) if(!vis[i]) add(n+1,i),fa[i]=n+1,dfs(i); //建立超级源点,把森林变成树
dfs1(n+1),dfs2(n+1,n+1),build(1,1,n+1); //初始化
For(i,1,Q){
auto[t,a,b,op]=q[i];
switch(t){
case 1:{ //修改操作
if(op==true) modify_range(a,b); //铺设路面
break;
}
case 2:{ //查询操作
if(op==true) cout<<query_range(a,b)<<'\n'; //合法查询
else cout<<-1<<'\n'; //不合法查询
break;
}
}
}
cerr<<"\n\n用时:"<<1.0*clock()/CLOCKS_PER_SEC<<'\n';
return 0;
}完结撒花~(樱花庄万岁)如果有写的不好的地方,欢迎私信作者或在下方留言,谢谢各位阅读与支持!