[AHOI 2005] 航线规划
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AHOI 2005] 航线规划相关的知识,希望对你有一定的参考价值。
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1969
[算法]
首先离线 , 将删边操作转化为加边操作
不妨首先将这张图按边-双连通分量(e-DCC)缩点 , 缩点后形成了一棵树
树链剖分 + 线段树即可
时间复杂度 : O(NlogN ^ 2)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 200010 struct query { int type , u , v; } que[MAXN]; struct edge { int to , nxt; } e[MAXN << 1] , ec[MAXN << 1]; int n , m , timer , cnt , tot , q , len; int head[MAXN] , chead[MAXN] , low[MAXN] , dfn[MAXN] , belong[MAXN] , size[MAXN] , fa[MAXN] , son[MAXN] , top[MAXN] , depth[MAXN] , u[MAXN] , v[MAXN] , ans[MAXN]; map< pair<int , int> , int> mp; bool is_bridge[MAXN << 1] , des[MAXN << 1]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } struct Segment_Tree { struct Node { int l , r , sum; int tag; } Tree[MAXN << 2]; inline void build(int index , int l , int r) { Tree[index].l = l; Tree[index].r = r; Tree[index].tag = 0; if (l == r) { if (l != 1) Tree[index].sum = 1; return; } int mid = (l + r) >> 1; build(index << 1 , l , mid); build(index << 1 | 1 , mid + 1 , r); update(index); } inline void pushdown(int index) { Tree[index << 1].sum = Tree[index << 1 | 1].sum = 0; Tree[index << 1].tag = Tree[index << 1 | 1].tag = 1; Tree[index].tag = 0; } inline void update(int index) { Tree[index].sum = Tree[index << 1].sum + Tree[index << 1 | 1].sum; } inline void modify(int index , int l , int r) { if (Tree[index].l == l && Tree[index].r == r) { Tree[index].sum = 0; Tree[index].tag = 1; return; } if (Tree[index].tag) pushdown(index); int mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= r) modify(index << 1 , l , r); else if (mid + 1 <= l) modify(index << 1 | 1 , l , r); else { modify(index << 1 , l , mid); modify(index << 1 | 1 , mid + 1 , r); } update(index); } inline int query(int index , int l , int r) { if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum; if (Tree[index].tag) pushdown(index); int mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= r) return query(index << 1 , l , r); else if (mid + 1 <= l) return query(index << 1 | 1 , l , r); else return query(index << 1 , l , mid) + query(index << 1 | 1 , mid + 1 , r); } } SGT; inline void addedge(int u , int v) { ++tot; e[tot] = (edge){v , head[u]}; head[u] = tot; } inline void addcedge(int u , int v) { ++tot; ec[tot] = (edge){v , chead[u]}; chead[u] = tot; } inline void tarjan(int u , int t) { low[u] = dfn[u] = ++timer; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (!dfn[v]) { tarjan(v , i); chkmin(low[u] , low[v]); if (low[v] > dfn[u]) is_bridge[i] = is_bridge[i ^ 1] = true; } else if (i != (t ^ 1)) chkmin(low[u] , dfn[v]); } } inline void dfs(int u , int id) { belong[u] = id; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (!belong[v] && !is_bridge[i]) dfs(v , id); } } inline void dfs1(int u) { size[u] = 1; son[u] = 0; for (int i = chead[u]; i; i = ec[i].nxt) { int v = ec[i].to; if (v == fa[u]) continue; depth[v] = depth[u] + 1; fa[v] = u; dfs1(v); size[u] += size[v]; if (son[u] == 0 || size[v] > size[son[u]]) son[u] = v; } } inline void dfs2(int u , int tp) { dfn[u] = ++timer; top[u] = tp; if (son[u]) dfs2(son[u] , tp); for (int i = chead[u]; i; i = ec[i].nxt) { int v = ec[i].to; if (v == fa[u] || v == son[u]) continue; dfs2(v , v); } } inline void modify(int u , int v) { u = belong[u] , v = belong[v]; int tu = top[u] , tv = top[v]; while (tu != tv) { if (depth[tu] > depth[tv]) { swap(u , v); swap(tu , tv); } SGT.modify(1 , dfn[tv] , dfn[v]); v = fa[tv]; tv = top[v]; } if (dfn[u] > dfn[v]) swap(u , v); if (dfn[u] + 1 <= dfn[v]) SGT.modify(1 , dfn[u] + 1 , dfn[v]); } inline int query(int u , int v) { u = belong[u] , v = belong[v]; int tu = top[u] , tv = top[v]; int ret = 0; while (tu != tv) { if (depth[tu] > depth[tv]) { swap(u , v); swap(tu , tv); } ret += SGT.query(1 , dfn[tv] , dfn[v]); v = fa[tv]; tv = top[v]; } if (dfn[u] > dfn[v]) swap(u , v); if (dfn[u] + 1 <= dfn[v]) ret += SGT.query(1 , dfn[u] + 1 , dfn[v]); return ret; } int main() { read(n); read(m); for (int i = 1; i <= m; i++) { read(u[i]); read(v[i]); mp[make_pair(u[i] , v[i])] = mp[make_pair(v[i] , u[i])] = i; } while (true) { int C , A , B; read(C); if (C == -1) break; read(A); read(B); if (C == 0) des[mp[make_pair(A , B)]] = true; que[++q].type = C; que[q].u = A; que[q].v = B; } tot = 1; for (int i = 1; i <= m; i++) { if (!des[i]) { addedge(u[i] , v[i]); addedge(v[i] , u[i]); } } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i , 0); for (int i = 1; i <= n; i++) if (!belong[i]) dfs(i , ++cnt); tot = 0; for (int i = 1; i <= m; i++) { if (des[i]) continue; if (belong[u[i]] != belong[v[i]]) { addcedge(belong[u[i]] , belong[v[i]]); addcedge(belong[v[i]] , belong[u[i]]); } } timer = 0; memset(dfn , 0 , sizeof(dfn)); dfs1(1); dfs2(1 , 1); SGT.build(1 , 1 , timer); for (int i = q; i >= 1; i--) { if (que[i].type == 0) modify(que[i].u, que[i].v); else ans[++len] = query(que[i].u , que[i].v); } reverse(ans + 1 , ans + len + 1); for (int i = 1; i <= len; i++) printf("%d " , ans[i]); return 0; }
以上是关于[AHOI 2005] 航线规划的主要内容,如果未能解决你的问题,请参考以下文章
刷题BZOJ 1969 [Ahoi2005]LANE 航线规划
BZOJ1969[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树