[关闭]
@zhangche0526 2017-07-05T07:22:06.000000Z 字数 35457 阅读 1410

仙人掌相关问题的处理方法

讲义


如图所示:

1

仙人掌图就是长得像仙人掌的图嘛(我真没看出哪里像了)

定义:对一个无向连通图,任意一条边属于至多一个简单环。

在仙人掌上,父亲和儿子都有节点的和环的之分。

DFS 树解决仙人掌 DP 问题

仙人掌的处理是十分复杂的(本蒟蒻个人认为,神犇轻喷),这里先从简单的 DFS树开始。

大神们还有什么覆盖之类的定义,参考最后的参考文献。

也就是说环是由多条树边和一条非树边组成的,非树边起到了连接的作用。

我们看几道经典题目:

引例

一棵仙人掌,每条边有边权,求 1 号节点到每个节点的最短路径长度。节点个数

我们先 dfs 一遍的到 dfs 树,这是我们之后处理的基础。

然后从一号节点开始 dp 。假设现在已经求出 1 到 u 的距离,枚举 u 的每一个儿子,如果该儿子不在环内,加上边权继续;否则,枚举换上的我们暴力求环上的每个点到 1 的距离,然后在分别从环上的每个点继续 dp 。

[BZOJ1023]cactus仙人掌图[SHOI2008]

一棵带边权仙人掌,求直径(最短路径最长的两个点的最短路径长度)。节点个数

我们 DFS 可以得到一颗 DFS 树。这里的 DFS 与 Tarjan 比较类似(大概就是 Tarjan),对于现在访问的节点 u 我们记录下来 low[u],dfn[u],dpt[u],fa[u] (前两个含义参考 Tarjan ,dpt 是深度(deepth) , fa 是 u 在 DFS 树上的父亲),对于没访问的(即 dfn 为 0 的)节点继续递归,向上回溯时更新 low 值。

遍历每条边时如果 low[v] 大于 dfn[u] ,说明此边为树边;对于一个节点,若其某个儿子在 DFS 树上的父亲不是它(没错,它的儿子的父亲不是它,机房的小伙伴们都笑疯了),那么说明这里出现了一个环,且此节点为环的(深度最小的节点),它的那个儿子为环的(环中最后被遍历到的,也就是 DFS 树的叶子节点)

我们做以上这些的目的主要就是判断环和桥,然后分别 DP 处理。


我们定义 为以 i 为端点的最长链的长度。

对于来说十分简单(此时假设没有环):

这里我们在代码中的写法稍有不同:


现在我们考虑环的问题:

对于一个环,我们的宗旨是把它缩成一个点(即把一个环的 f 信息都存在其根上),然后就可以开心地按之前的方式 DP 啦!

由于环中更新答案的时候只转一圈不能保证答案最优(因为有可能最优的那一部分环被根分开了),又由于环中距离的定义是最短路,所以我们只要转够一圈半即可。

如图:

定义环的节点集合为 C ,记环中一点 a 在环中的遍历顺序为 aid 、环长(环中节点个数)为 L ,则在环中两个点之间的距离


我们记一个环的根为 x 、尾为 y。(我只是懒得起名)

实现的时候,我们把环中的节点的 f 依次存在一个数组(我的代码中是 a 数组懒得起名+1)里,然后将这个数组倍长(就是将其复制一遍放到尾部),遍历时动一定一(这里莫名怀念小晓笑潇),若之间相差大于 就 continue,再用单调队列优化一下, ans 就能轻松更新好了。之后再按照公式更新 f[x] 即可。

整个算法过程的时间复杂度仅为 ,还是蛮快的。


  1. /**************************************************************
  2. Problem: 1023
  3. User: zhangche0526
  4. Language: C++
  5. Result: Accepted
  6. Time:256 ms
  7. Memory:7076 kb
  8. ****************************************************************/
  9. #include<iostream>
  10. #include<cstdio>
  11. using namespace std;
  12. const int MAXN=5e4+5,MAXM=MAXN<<1;
  13. int n,m;
  14. struct E{int next,to;} e[MAXM<<1];int ecnt,G[MAXN];
  15. void addEdge(int u,int v)
  16. {
  17. e[++ecnt]=(E){G[u],v};G[u]=ecnt;
  18. e[++ecnt]=(E){G[v],u};G[v]=ecnt;
  19. }
  20. int ans;
  21. int f[MAXN];
  22. int dfn[MAXN],dcnt,low[MAXN],dpt[MAXN],fa[MAXN];
  23. int que[MAXN<<1],a[MAXN<<1];
  24. void solve(int x,int y)
  25. {
  26. int cnt=dpt[y]-dpt[x]+1,head=1,tail=1,i;
  27. for(i=y;i!=x;i=fa[i]) a[cnt--]=f[i];a[1]=f[x];
  28. cnt=dpt[y]-dpt[x]+1;
  29. for(i=1;i<=cnt;i++) a[i+cnt]=a[i];
  30. que[1]=1;
  31. for(i=2;i<=cnt+(cnt>>1);i++)
  32. {
  33. if(i-que[head]>(cnt>>1)) head++;
  34. ans=max(ans,a[i]+i+a[que[head]]-que[head]);
  35. while(head<=tail&&a[i]-i>=a[que[tail]]-que[tail]) tail--;
  36. que[++tail]=i;
  37. }
  38. for(i=2;i<=cnt;i++) f[x]=max(f[x],a[i]+min(i-1,cnt-i+1));
  39. }
  40. void dfs(int u)
  41. {
  42. int i;
  43. dfn[u]=low[u]=++dcnt;
  44. for(i=G[u];i;i=e[i].next)
  45. {
  46. int v=e[i].to;
  47. if(v==fa[u]) continue;
  48. if(!dfn[v]) {fa[v]=u;dpt[v]=dpt[u]+1;dfs(v);}
  49. low[u]=min(low[u],low[v]);
  50. if(low[v]>dfn[u]) ans=max(ans,f[u]+f[v]+1),f[u]=max(f[u],f[v]+1);
  51. //对树边的更新
  52. }
  53. for(i=G[u];i;i=e[i].next)
  54. {
  55. int v=e[i].to;
  56. if(fa[v]!=u&&dfn[u]<dfn[v])//遍历非树边,处理环
  57. solve(u,v);
  58. }
  59. }
  60. int main()
  61. {
  62. int i,j;
  63. scanf("%d%d",&n,&m);
  64. for(i=1;i<=m;i++)
  65. {
  66. int k;scanf("%d",&k);
  67. int u,v;scanf("%d",&u);
  68. for(j=2;j<=k;j++) scanf("%d",&v),addEdge(u,v),u=v;
  69. }
  70. dfs(1);printf("%d\n",ans);
  71. return 0;
  72. }

圆方树

通过前面的几道例题,我们发现:其实解决仙人掌 DP 问题不过就是参照树上的解法然后对环上的情况特殊处理一下,把环的信息记录到一个点上。

其实,神犇们很早就发现了这一点,于是他们想:既然仙人掌的许多问题在树上都有现成的解法,那么如果直接把仙人掌变成树,岂不美哉?

于是,神犇们成功的仙人掌变成树,并给这种树起了一个生动形象的名字:圆方树,它能解决大多数静态仙人掌问题

定义

仙人掌 的圆方树 为满足一下条件的无向图:

构造

性质

[BZOJ4316] 小 C 的独立集

在一个无向连通图中选出若干个点,这些点互相没有边连接,并使取出的点尽量多。数据保证图的一条边属于且仅属于一个简单环,图中没有重边和自环。点数 ,边数

    在一个无向连通图中选出若干个点,这些点互相没有边连接,并使取出的点尽量多。数据保证图的一条边属于且仅属于一个简单环,图中没有重边和自环。点数 $n\leq10^6$ ,边数 $m\leq10^6$

时间复杂度:

其实我们在解这道题的时候,没有必要真正建出圆方树,我在代码里建出圆方树只是为了举例说明,是为让大家熟悉圆方树的建法。

  1. /**************************************************************
  2. Problem: 4316
  3. User: zhangche0526
  4. Language: C++
  5. Result: Accepted
  6. Time:204 ms
  7. Memory:12140 kb
  8. ****************************************************************/
  9. #include<iostream>
  10. #include<cstdio>
  11. #include<cstring>
  12. const int MAXN=2e5+5,MAXM=2e5+5,INF=~0U>>1;
  13. int n,m,newn;//newn:圆方树的点数
  14. struct CFS
  15. {
  16. struct E{int next,to;} e[MAXM];int ecnt,G[MAXN];
  17. void addEdge(int u,int v){e[++ecnt]=(E){G[u],v};G[u]=ecnt;}
  18. void addEdge2(int u,int v){addEdge(u,v);addEdge(v,u);}
  19. CFS(){ecnt=1;}
  20. } G,T;
  21. int f[MAXN][2],g[MAXN][2],gcnt;
  22. void treeDP(int u,int from)
  23. {
  24. int i;
  25. if(u<=n)
  26. {
  27. f[u][0]=0;f[u][1]=1;
  28. for(i=T.G[u];i;i=T.e[i].next)
  29. {
  30. int v=T.e[i].to;
  31. if(v==from) continue;
  32. treeDP(v,u);
  33. if(v>n) continue;
  34. f[u][0]+=std::max(f[v][0],f[v][1]);
  35. f[u][1]+=f[v][0];
  36. }
  37. }
  38. else
  39. {
  40. for(i=T.G[u];i;i=T.e[i].next)
  41. if(T.e[i].to!=from)
  42. treeDP(T.e[i].to,u);
  43. gcnt=0;
  44. for(i=T.G[u];i;i=T.e[i].next)
  45. {
  46. g[++gcnt][0]=f[T.e[i].to][0];
  47. g[gcnt][1]=f[T.e[i].to][1];
  48. }
  49. for(i=gcnt-1;i;i--)
  50. {
  51. g[i][0]+=std::max(g[i+1][0],g[i+1][1]);
  52. g[i][1]+=g[i+1][0];
  53. }
  54. f[from][0]=g[1][0];
  55. gcnt=0;
  56. for(i=T.G[u];i;i=T.e[i].next)
  57. {
  58. g[++gcnt][0]=f[T.e[i].to][0];
  59. g[gcnt][1]=f[T.e[i].to][1];
  60. }
  61. g[gcnt][1]=-INF;
  62. for(i=gcnt-1;i;i--)
  63. {
  64. g[i][0]+=std::max(g[i+1][0],g[i+1][1]);
  65. g[i][1]+=g[i+1][0];
  66. }
  67. f[from][1]=g[1][1];
  68. }
  69. }
  70. int fa[MAXN],dfn[MAXN],dcnt;
  71. bool onRing[MAXN];
  72. void dfs(int u,int la)
  73. {
  74. int i,j;dfn[u]=++dcnt;
  75. for(i=G.G[u];i;i=G.e[i].next)
  76. {
  77. int v=G.e[i].to;
  78. if(v==la) continue;
  79. if(!dfn[v])
  80. {
  81. fa[v]=u;onRing[u]=false;
  82. dfs(v,u);
  83. if(!onRing[u]) T.addEdge2(u,v);
  84. }
  85. else
  86. {
  87. if(dfn[v]>dfn[u]) continue;
  88. for(j=u,++newn;j!=fa[v];j=fa[j])
  89. T.addEdge2(newn,j),onRing[j]=true;
  90. }
  91. }
  92. }
  93. int main()
  94. {
  95. int i,u,v;
  96. scanf("%d%d",&n,&m);newn=n;
  97. for(i=1;i<=m;i++)
  98. {
  99. scanf("%d%d",&u,&v);
  100. G.addEdge2(u,v);
  101. }
  102. dfs(1,0);treeDP(1,0);
  103. printf("%d\n",std::max(f[1][0],f[1][1]));
  104. }

仙人掌最短路问题

[BZOJ2125]最短路

一个带权仙人掌图,求多源最短路。点数 ,边数 ,询问个数

对多源最短路问题,树上的经典做法是两点到根的距离减去两倍的 LCA 到根的距离,那么我们使用圆方树将仙人掌图转化为树后就可以按相同思路解决了。

然而我们发现原图的边权并没有很好地表现在我们建出来的圆方树上,所以我们首先解决边权问题:

  1. 找到换的根;
  2. 方点向环的根的连边的边权为 0 ;
  3. 环上其他点向方点连边,边权为到环的根的最短路径长度。

计算好边权后,对于两点的 LCA 是圆点的情况就可以直接按照树上的方法求距离了;而对于两点的 LCA 是方点的情况,其 LCA 是一个环,这两点一直向祖先走会到环上的两个不同位置,我们只需找到这两个位之间的到最短路。

具体方法:

  1. Tarjan 找出所有的简单环,并记录环上信息备用,建圆方树;

    这里需要注意,上一道题的图比仙人掌图还要特殊,每个节点一定属于一个环,因此我们可以对于每个节点只访问一次即可;可对于一般情况,我们在发现一个点 u 的下一个点 v 满足: 时,说明我们找到了一个环,且 u 为环首,这时我们要弹出从环尾到 v 的所有点,然而我们并不能确定是否要弹出 u (因为如果 u 还属于另外一个环,那就悲剧了),所以这是我们还有走回头路更新 low 值,判断是否弹出 v 。

    其实这里我们还有一种更好实现的处理方法:在 Tarjan 时维护一个边的栈,这样就可以免去许多细节处理问题。

  2. 维护圆方树的倍增数组,记录每个点到根的距离。

  3. 倍增法求 LCA,若果 LCA 是环,还需要把两个点倍增到这个环上,在环上查询。

第一种实现 by PoPoQQQ

  1. /**************************************************************
  2. Problem: 2125
  3. User: PoPoQQQ
  4. Language: C++
  5. Result: Accepted
  6. Time:684 ms
  7. Memory:5248 kb
  8. ****************************************************************/
  9. #include <map>
  10. #include <vector>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #define M 10100
  16. #define INF 0x3f3f3f3f
  17. using namespace std;
  18. int n,m,q,cnt;
  19. map<int,int> f[M];
  20. vector<int> belong[M];
  21. int dis[M];
  22. vector<int> rings[M];
  23. int size[M];
  24. map<int,int> dist[M];
  25. void Add(int x,int y,int z)
  26. {
  27. if( f[x].find(y)==f[x].end() )
  28. f[x][y]=INF;
  29. f[x][y]=min(f[x][y],z);
  30. }
  31. namespace Cactus_Graph{
  32. int fa[M<<1][16],dpt[M<<1];
  33. pair<int,int> second_lca;
  34. int Get_Depth(int x)
  35. {
  36. if(!fa[x][0]) dpt[x]=1;
  37. if(dpt[x]) return dpt[x];
  38. return dpt[x]=Get_Depth(fa[x][0])+1;
  39. }
  40. void Pretreatment()
  41. {
  42. int i,j;
  43. for(j=1;j<=15;j++)
  44. {
  45. for(i=1;i<=cnt;i++)
  46. fa[i][j]=fa[fa[i][j-1]][j-1];
  47. for(i=n+1;i<=n<<1;i++)
  48. fa[i][j]=fa[fa[i][j-1]][j-1];
  49. }
  50. for(i=1;i<=cnt;i++)
  51. Get_Depth(i);
  52. for(i=n+1;i<=n<<1;i++)
  53. Get_Depth(i);
  54. }
  55. int LCA(int x,int y)
  56. {
  57. int j;
  58. if(dpt[x]<dpt[y])
  59. swap(x,y);
  60. for(j=15;~j;j--)
  61. if(dpt[fa[x][j]]>=dpt[y])
  62. x=fa[x][j];
  63. if(x==y) return x;
  64. for(j=15;~j;j--)
  65. if(fa[x][j]!=fa[y][j])
  66. x=fa[x][j],y=fa[y][j];
  67. second_lca=make_pair(x,y);
  68. return fa[x][0];
  69. }
  70. }
  71. void Tarjan(int x)
  72. {
  73. static int dpt[M],low[M],T;
  74. static int stack[M],top;
  75. map<int,int>::iterator it;
  76. dpt[x]=low[x]=++T;
  77. stack[++top]=x;
  78. for(it=f[x].begin();it!=f[x].end();it++)
  79. {
  80. if(dpt[it->first])
  81. low[x]=min(low[x],dpt[it->first]);
  82. else
  83. {
  84. Tarjan(it->first);
  85. if(low[it->first]==dpt[x])
  86. {
  87. int t;
  88. rings[++cnt].push_back(x);
  89. belong[x].push_back(cnt);
  90. Cactus_Graph::fa[cnt][0]=n+x;
  91. do{
  92. t=stack[top--];
  93. rings[cnt].push_back(t);
  94. Cactus_Graph::fa[n+t][0]=cnt;
  95. }while(t!=it->first);
  96. }
  97. low[x]=min(low[x],low[it->first]);
  98. }
  99. }
  100. }
  101. void DFS(int x)
  102. {
  103. vector<int>::iterator it,_it;
  104. static int stack[M];int i,j,top=0;
  105. for(it=rings[x].begin();it!=rings[x].end();it++)
  106. stack[++top]=*it;
  107. stack[++top]=*rings[x].begin();
  108. for(i=1;i<top;i++)
  109. {
  110. int p1=stack[i],p2=stack[i+1];
  111. size[x]+=f[p1][p2];
  112. if(i!=top-1)
  113. dist[x][p2]=dist[x][p1]+f[p1][p2];
  114. }
  115. i=2;j=top-1;
  116. while(i<=j)
  117. {
  118. if(dis[stack[i-1]]+f[stack[i-1]][stack[i]]<dis[stack[j+1]]+f[stack[j+1]][stack[j]])
  119. dis[stack[i]]=dis[stack[i-1]]+f[stack[i-1]][stack[i]],i++;
  120. else
  121. dis[stack[j]]=dis[stack[j+1]]+f[stack[j+1]][stack[j]],j--;
  122. }
  123. for(_it=rings[x].begin(),_it++;_it!=rings[x].end();_it++)
  124. for(it=belong[*_it].begin();it!=belong[*_it].end();it++)
  125. DFS(*it);
  126. }
  127. int main()
  128. {
  129. using namespace Cactus_Graph;
  130. int i,x,y,z;
  131. cin>>n>>m>>q;
  132. for(i=1;i<=m;i++)
  133. {
  134. scanf("%d%d%d",&x,&y,&z);
  135. Add(x,y,z);Add(y,x,z);
  136. }
  137. Tarjan(1);
  138. Pretreatment();
  139. vector<int>::iterator it;
  140. for(it=belong[1].begin();it!=belong[1].end();it++)
  141. DFS(*it);
  142. for(i=1;i<=q;i++)
  143. {
  144. scanf("%d%d",&x,&y);
  145. int lca=LCA(n+x,n+y);
  146. if(lca>n) printf("%d\n",dis[x]+dis[y]-2*dis[lca-n]);
  147. else
  148. {
  149. int ans=dis[x]+dis[y]-dis[second_lca.first-n]-dis[second_lca.second-n];
  150. int temp=abs(dist[lca][second_lca.first-n]-dist[lca][second_lca.second-n]);
  151. ans+=min(temp,size[lca]-temp);
  152. printf("%d\n",ans);
  153. }
  154. }
  155. return 0;
  156. }

第二种实现 by virgil

  1. /**************************************************************
  2. Problem: 2125
  3. User: zhangche0526
  4. Language: C++
  5. Result: Accepted
  6. Time:432 ms
  7. Memory:21816 kb
  8. ****************************************************************/
  9. #include<iostream>
  10. #include<cstdio>
  11. #include<cstring>
  12. #include<cmath>
  13. #include<stack>
  14. #include<queue>
  15. const int MAXN=1e4+5;
  16. int N,M,Q;
  17. int abs(int x){return (x<0)?(-x):x;}
  18. struct E{int next,to,val;} e[MAXN<<3];int ecnt,G[MAXN];
  19. void addEdge(int u,int v,int w){e[++ecnt]=(E){G[u],v,w};G[u]=ecnt;}
  20. void addEdge2(int u,int v,int w){addEdge(u,v,w);addEdge(v,u,w);}
  21. void initCFS(){memset(G,0,sizeof(G));ecnt=0;}
  22. struct A{int u,v,w;A(int u=0,int v=0,int w=0):u(u),v(v),w(w){};};
  23. int dis[MAXN];bool inQ[MAXN];
  24. std::queue<int> que;
  25. void SPFA(int S)
  26. {
  27. int i;
  28. memset(dis,0x3f,sizeof(dis));
  29. que.push(S);dis[S]=0;inQ[S]=true;
  30. while(!que.empty())
  31. {
  32. int u=que.front();que.pop();
  33. inQ[u]=false;
  34. for(i=G[u];i;i=e[i].next)
  35. {
  36. int v=e[i].to;
  37. if(dis[v]>dis[u]+e[i].val)
  38. {
  39. dis[v]=dis[u]+e[i].val;
  40. if(!inQ[v]) que.push(v),inQ[v]=true;
  41. }
  42. }
  43. }
  44. }
  45. std::stack<A> st;
  46. int ringLen[MAXN],rcnt;
  47. int belong[MAXN],ringRtDis[MAXN];
  48. int anc[MAXN][20];
  49. void addRing(int u,int v)
  50. {
  51. rcnt++;
  52. while(st.top().u!=u&&st.top().v!=v)
  53. {
  54. A a=st.top();st.pop();
  55. ringRtDis[a.u]=ringRtDis[a.v]+a.w;
  56. ringLen[rcnt]+=a.w;
  57. if(a.u!=u) belong[a.u]=rcnt,anc[a.u][0]=u;
  58. if(a.v!=u) belong[a.v]=rcnt,anc[a.v][0]=u;
  59. }
  60. A a=st.top();st.pop();
  61. ringRtDis[a.u]=ringRtDis[a.v]+a.w;
  62. ringLen[rcnt]+=a.w;
  63. anc[a.v][0]=a.u;
  64. }
  65. int dfn[MAXN],low[MAXN],dcnt;
  66. void tarjan(int u,int la)
  67. {
  68. dfn[u]=low[u]=++dcnt;
  69. for(int i=G[u];i;i=e[i].next)
  70. {
  71. int v=e[i].to;
  72. if(v==la) continue;
  73. if(!dfn[v])
  74. {
  75. st.push(A(u,v,e[i].val));
  76. tarjan(v,u);
  77. low[u]=std::min(low[u],low[v]);
  78. if(low[v]>=dfn[u])
  79. addRing(u,v);
  80. }else if(dfn[v]<low[u]) low[u]=dfn[v],st.push(A(u,v,e[i].val));
  81. }
  82. }
  83. int dpt[MAXN];
  84. int rebuild(int u,int la)
  85. {
  86. dpt[u]=dpt[la]+1;
  87. for(int i=G[u];i;i=e[i].next)
  88. rebuild(e[i].to,u);
  89. }
  90. inline void initLCA()
  91. {
  92. for(int i=1;(1<<i)<=N;i++)
  93. for(int j=1;j<=N;j++)
  94. anc[j][i]=anc[anc[j][i-1]][i-1];
  95. }
  96. int calDis(int x,int y)
  97. {
  98. int i;
  99. if(dpt[x]<dpt[y]) std::swap(x,y);
  100. int xDis=dis[x],yDis=dis[y];
  101. int maxlogn=std::floor(std::log(N)/std::log(2));
  102. for(i=maxlogn;i>=0;i--)
  103. if(dpt[x]-(1<<i)>=dpt[y])
  104. x=anc[x][i];
  105. if(x==y) return xDis-dis[x];
  106. for(i=maxlogn;i>=0;i--)
  107. if(anc[x][i]!=anc[y][i])
  108. x=anc[x][i],y=anc[y][i];
  109. if(belong[x]&&belong[x]==belong[y])
  110. {
  111. int xyDis=abs(ringRtDis[x]-ringRtDis[y]);
  112. int minDis=std::min(xyDis,ringLen[belong[x]]-xyDis);
  113. return xDis+yDis-dis[x]-dis[y]+minDis;
  114. }else return xDis+yDis-2*dis[anc[x][0]];
  115. }
  116. int main()
  117. {
  118. int i;
  119. scanf("%d%d%d",&N,&M,&Q);
  120. for(i=1;i<=M;i++)
  121. {
  122. int u,v,w;scanf("%d%d%d",&u,&v,&w);
  123. addEdge2(u,v,w);
  124. }
  125. SPFA(1);
  126. tarjan(1,0);
  127. initLCA();
  128. initCFS();
  129. for(i=2;i<=N;i++)
  130. addEdge(anc[i][0],i,0);
  131. rebuild(1,0);
  132. while(Q--)
  133. {
  134. int x,y;scanf("%d%d",&x,&y);
  135. printf("%d\n",calDis(x,y));
  136. }
  137. return 0;
  138. }

仙人掌剖分

[UOJ158]静态仙人掌

给出一棵根为 1 的仙人掌,每个点是黑色或者白色,保证所有环都是奇环,要求支持三种操作:

  1. 把一个点到根的最短路径上的所有点颜色取反;
  2. 把一个点到根的最长简单路径上的所有点颜色取反;
  3. 询问一个点的子仙人掌里面有多少个黑点。

树上的做法肯定是树剖,对于仙人掌,我们对圆方树进行树剖。

根据圆方树的性质,子仙人掌就是圆方树的子树,因此我们可以对树剖的方法加以扩充解决本问题。

我们回想树剖,其精髓就是支持树上重链的快速操作,而对于仙人掌来说,我们同样需要支持对重链的快速操作:就是支持对一条重链的一个前缀的最长路或最短路进行操作。

那么我们就可以把点进行分类。考虑每一条重链,将点分成 3 类:

  1. 割点
  2. 在重链上作为最短路径出现的点
  3. 在重链上作为最长路径出现的点。

那么显然对于第 1 类点,直接在树上修改即可。然而对于后两种点,树上路径不一定会扫过需要更改的点。

按照传统的剖分方法的话是不支持考虑方点连出的圆点的,因此我们考虑在 DFS 序中,如果一个点是方点,那么我们先访问它的所有儿子,然后再按照重边先行的顺序访问儿子的子树(不包括那些儿子了),这样的话,我们修改一段区间的时候,就会扫过这个环上所有点;而我们已经把点分类了,因此也可以选择只修改最短路径或最长路径上的点。

代码 by laofu

  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. #include<cstdlib>
  8. #include<ctime>
  9. #include<queue>
  10. #include<set>
  11. #define md double
  12. #define LL long long
  13. #define LLD "%lld"
  14. using namespace std;
  15. const int N=2e5+100;
  16. const int inf=2147483647;
  17. int gi()
  18. {
  19. int w=0;
  20. bool q=1;
  21. char c=getchar();
  22. while ((c<'0'||c>'9') && c!='-') c=getchar();
  23. if (c=='-') q=0,c=getchar();
  24. while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
  25. return q? w:-w;
  26. }
  27. int head[N],next[N<<1],to[N<<1],tot;
  28. int fa[N],n,dis[N],rt[N],scc,in[N],col[N];
  29. int siz[N],son[N],top[N],l[N],r[N],dfn[N],sa[N],R[N],L[N];
  30. int all[N<<2][3],c[N<<2][3];bool tag[N<<2][3];
  31. #define lc (i<<1)
  32. #define rc (i<<1|1)
  33. inline void build(int i,int l,int r) {
  34. if (l==r) { if (sa[l]<=n) all[i][col[sa[l]]]++; }
  35. else {
  36. int m=(l+r)>>1;
  37. build(lc,l,m);build(rc,m+1,r);
  38. all[i][0]=all[lc][0]+all[rc][0];all[i][1]=all[lc][1]+all[rc][1];all[i][2]=all[lc][2]+all[rc][2];
  39. }
  40. c[i][0]=all[i][0],c[i][1]=all[i][1],c[i][2]=all[i][2];
  41. }
  42. #define pushdown(i) for (m=0;m<3;m++) if (tag[i][m]) tag[lc][m]^=1,c[lc][m]=all[lc][m]-c[lc][m],tag[rc][m]^=1,c[rc][m]=all[rc][m]-c[rc][m],tag[i][m]=0;
  43. inline void modify(int i,int l,int r,int L,int R,int p) {
  44. if (L<=l&&r<=R) {
  45. tag[i][0]^=1;c[i][0]=all[i][0]-c[i][0];
  46. if (p!=2) tag[i][1]^=1,c[i][1]=all[i][1]-c[i][1];
  47. if (p!=1) tag[i][2]^=1,c[i][2]=all[i][2]-c[i][2];
  48. return;
  49. }
  50. int m;pushdown(i);m=(l+r)>>1;
  51. if (L<=m) modify(lc,l,m,L,R,p);
  52. if (m<R) modify(rc,m+1,r,L,R,p);
  53. c[i][0]=c[lc][0]+c[rc][0];c[i][1]=c[lc][1]+c[rc][1];c[i][2]=c[lc][2]+c[rc][2];
  54. }
  55. inline int query(int i,int l,int r,int L,int R) {
  56. if (L<=l&&r<=R) return c[i][0]+c[i][1]+c[i][2];
  57. int m;pushdown(i);m=(l+r)>>1;
  58. if (R<=m) return query(lc,l,m,L,R);
  59. if (m<L) return query(rc,m+1,r,L,R);
  60. return query(lc,l,m,L,R)+query(rc,m+1,r,L,R);
  61. }
  62. namespace tree{
  63. int head[N],next[N<<1],to[N<<1],tot;
  64. inline void link(int a,int b) { to[++tot]=b,next[tot]=head[a],head[a]=tot; }
  65. inline void dfs1(int k) {
  66. siz[k]=1;
  67. for (int i=head[k];i;i=next[i]) {
  68. dfs1(to[i]);siz[k]+=siz[to[i]];
  69. if (siz[to[i]]>siz[son[k]]) son[k]=to[i];
  70. }
  71. }
  72. inline void dfs2(int k) {
  73. if (!dfn[k]) sa[dfn[k]=++tot]=k; else if (k<=n) L[k]=tot+1; if (!son[k]) { R[k]=tot; return; }int i,tp;
  74. if (k>n) {
  75. for (i=head[k];i;i=next[i]) dis[to[i]]=++dis[k];L[k]=tot+1;
  76. for (i=head[k],tp=1+(dis[son[k]]>dis[k]>>1);i;i=next[i]) if (to[i]==son[k]) tp^=3; else col[to[i]]=tp,sa[dfn[to[i]]=++tot]=to[i];R[k]=tot;
  77. sa[dfn[son[k]]=++tot]=son[k];
  78. }
  79. top[son[k]]=top[k];dfs2(son[k]);
  80. for (i=head[k];i;i=next[i]) if (to[i]!=son[k]) top[to[i]]=to[i],dfs2(to[i]);
  81. if (k<=n) R[k]=tot;
  82. }
  83. inline void rev(int k,int p) {
  84. while (k)
  85. if (top[k]==k)
  86. if (rt[k]) {
  87. if ((dis[k]<=dis[rt[k]]>>1)==(p==1)) {
  88. modify(1,1,scc,L[rt[k]],dfn[k],0);
  89. if (dis[son[rt[k]]]<dis[k]) modify(1,1,scc,dfn[son[rt[k]]],dfn[son[rt[k]]],0);
  90. }
  91. else {
  92. modify(1,1,scc,dfn[k],R[rt[k]],0);
  93. if (dis[son[rt[k]]]>dis[k]) modify(1,1,scc,dfn[son[rt[k]]],dfn[son[rt[k]]],0);
  94. }
  95. k=rt[rt[k]];
  96. }
  97. else modify(1,1,scc,dfn[k],dfn[k],p),k=fa[k];
  98. else if (top[k]<=n&&rt[top[k]]) modify(1,1,scc,dfn[son[top[k]]],dfn[k],p),k=top[k];
  99. else modify(1,1,scc,top[k]<=n?dfn[top[k]]:L[top[k]],dfn[k],p),k=fa[top[k]];
  100. }
  101. inline int ask(int k) { return !rt[k]||k==son[rt[k]]?query(1,1,scc,dfn[k],R[k]):query(1,1,scc,dfn[k],dfn[k])+(L[k]<=R[k]?query(1,1,scc,L[k],R[k]):0); }
  102. }
  103. inline void dfs(int k) {
  104. dfn[k]=++tot;
  105. for (int i=head[k],t,p;i;i=next[i])
  106. if (!dfn[to[i]]) {
  107. fa[to[i]]=k,dfs(to[i]);
  108. if (!rt[to[i]]) tree::link(k,to[i]);
  109. }
  110. else if (to[i]!=fa[k]&&dfn[to[i]]<dfn[k]) {
  111. rt[p=++scc]=to[i];tree::link(to[i],scc);fa[scc]=to[i];
  112. for (t=k;t!=to[i];t=fa[p=t]) l[t]=fa[t],r[t]=p,tree::link(scc,t),rt[t]=scc;
  113. l[scc]=p,r[scc]=k;
  114. }
  115. }
  116. int main()
  117. {
  118. //freopen("cactus.in","r",stdin);
  119. //freopen("cactus.out","w",stdout);
  120. n=scc=gi();int m=gi(),Q=gi(),k,tot=0,a,b;
  121. while (m--) {
  122. a=gi(),b=gi();
  123. to[++tot]=b,next[tot]=head[a],head[a]=tot;
  124. to[++tot]=a,next[tot]=head[b],head[b]=tot;
  125. }
  126. dfs(1);for (k=1;k<=n;dfn[k++]=0) if (rt[k]) fa[k]=rt[k];
  127. top[1]=1;tree::tot=0;tree::dfs1(1);
  128. tree::dfs2(1);
  129. build(1,1,scc);
  130. while (Q--) {
  131. if ((k=gi())==3) printf("%d\n",tree::ask(gi()));
  132. else tree::rev(gi(),k);
  133. }
  134. return 0;
  135. }

仙人掌分治问题

[UOJ23]跳蚤国王下江南

一个仙人掌,求对 输出从 1 出发的长度为 的简单路径有多少条。答案对 998244353 取模。点数

我们先考虑暴力 DP 算法:

为从节点 u 开始,只能向远离根的方向走,长度为 的路径条数。

那么对于 u 的每个儿子 v ,如果 v 是一个节点,就把 加到 里;如果是一个环,那么枚举这个换上的每个儿子 z ,把 加到 里(其中 表示 x 到 z 的两条路径的长度 )。

不过上述算法是 A 不掉的,我们发现, 可以看做一个多项式,每次从 u 的一个儿子 v 注意过来时,相当于是把 乘上 或乘上 并加到 中。

首先是分治,每次分治时,找到重心 u 以后,分治每个连通块,然后将根到 u 的多项式求出来,记为 ,再将 u 的所有儿子的答案求出来相加,记为 。 我们只要将 和每个连同快当然答案加起来即可。

我们在求 时,如果将重心到根的多项式用分治+ FFT 乘起来起来,总的时间复杂度为 (这里点分治 ,分治 , FFT ),然而这样的话我们并没有充分利用信息,而导致了多余的计算:分治是不必要的,我们只需在点分治的时候记录一下根到重心的的多项式,然后一个一个乘回去,这样就可以在 的时间复杂度内完美地解决问题。

代码 by Vfleaking

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cassert>
  4. #include <cstdlib>
  5. #include <climits>
  6. #include <algorithm>
  7. #include <map>
  8. #include <vector>
  9. using namespace std;
  10. typedef long long s64;
  11. const int P = 998244353;
  12. const int MaxN = 100000;
  13. const int MaxM = MaxN * 2;
  14. const int FFT_G = 3;
  15. const int MaxFFTN = 262144;
  16. template <class T>
  17. inline void relax(T &a, const T &b)
  18. {
  19. if (b > a)
  20. a = b;
  21. }
  22. inline int &modaddto(int &a, const int &b)
  23. {
  24. a += b;
  25. if (a >= P)
  26. a -= P;
  27. return a;
  28. }
  29. inline int modpow(int a, const int &n)
  30. {
  31. int res = 1;
  32. int t = a;
  33. for (int i = n; i > 0; i >>= 1)
  34. {
  35. if (i & 1)
  36. res = (s64)res * t % P;
  37. t = (s64)t * t % P;
  38. }
  39. return res;
  40. }
  41. struct polyarray
  42. {
  43. int *a;
  44. int n;
  45. inline polyarray()
  46. {
  47. n = 0;
  48. a = NULL;
  49. }
  50. inline polyarray(const polyarray &rhs)
  51. {
  52. n = rhs.n;
  53. a = new int[n];
  54. copy(rhs.a, rhs.a + n, a);
  55. }
  56. inline ~polyarray()
  57. {
  58. dispose();
  59. }
  60. inline void dispose()
  61. {
  62. if (a)
  63. {
  64. n = 0;
  65. delete []a;
  66. a = NULL;
  67. }
  68. }
  69. int getDegree() const
  70. {
  71. int d = n - 1;
  72. while (d >= 0 && a[d] == 0)
  73. d--;
  74. return d;
  75. }
  76. void resize(int l)
  77. {
  78. if (l == 0)
  79. {
  80. dispose();
  81. return;
  82. }
  83. int new_n = 1;
  84. while (new_n < l)
  85. new_n <<= 1;
  86. if (new_n == n)
  87. {
  88. fill(a + new_n, a + n, 0);
  89. return;
  90. }
  91. int *new_a = new int[new_n];
  92. int tl = min(n, new_n);
  93. copy(a, a + tl, new_a);
  94. fill(new_a + tl, new_a + new_n, 0);
  95. if (a != NULL)
  96. delete []a;
  97. n = new_n;
  98. a = new_a;
  99. }
  100. inline polyarray& operator=(const polyarray &rhs)
  101. {
  102. dispose();
  103. n = rhs.n;
  104. a = new int[n];
  105. copy(rhs.a, rhs.a + n, a);
  106. return *this;
  107. }
  108. inline void print()
  109. {
  110. for (int i = 0; i < n; i++)
  111. cout << a[i] << " ";
  112. cout << endl;
  113. }
  114. };
  115. int preGPow[MaxFFTN];
  116. void fft(int *a, int n, int s, int *out)
  117. {
  118. if (n == 1)
  119. {
  120. out[0] = a[0];
  121. return;
  122. }
  123. int m = n >> 1;
  124. fft(a, m, s + 1, out);
  125. fft(a + (1 << s), m, s + 1, out + m);
  126. for (int i = 0, *po = out, *pe = out + m; i < m; i++, pe++, po++)
  127. {
  128. int o = *po, e = (s64)*pe * preGPow[i << s] % P;
  129. *po = (o + e) % P;
  130. *pe = (o + P - e) % P;
  131. }
  132. }
  133. inline polyarray &polymulto(polyarray &a, const polyarray &b)
  134. {
  135. int a_d = a.getDegree(), b_d = b.getDegree();
  136. if (a_d == -1 || b_d == -1)
  137. {
  138. a.dispose();
  139. return a;
  140. }
  141. int l = a_d + b_d + 1;
  142. static int da[MaxFFTN];
  143. static int db[MaxFFTN];
  144. int b1_n = 0;
  145. for (int i = 0; i <= b_d; i++)
  146. b1_n += b.a[i] != 0;
  147. if (b1_n <= 2)
  148. {
  149. for (int i = 0; i < l; i++)
  150. da[i] = 0;
  151. for (int j = 0; j <= b_d; j++)
  152. if (b.a[j] != 0)
  153. for (int i = 0; i <= a_d; i++)
  154. da[i + j] = (da[i + j] + (s64)a.a[i] * b.a[j]) % P;
  155. a.resize(l);
  156. copy(da, da + l, a.a);
  157. return a;
  158. }
  159. int a1_n = 0;
  160. for (int i = 0; i <= a_d; i++)
  161. a1_n += a.a[i] != 0;
  162. if (a1_n <= 2)
  163. {
  164. for (int i = 0; i < l; i++)
  165. da[i] = 0;
  166. for (int i = 0; i <= a_d; i++)
  167. if (a.a[i] != 0)
  168. for (int j = 0; j <= b_d; j++)
  169. da[i + j] = (da[i + j] + (s64)a.a[i] * b.a[j]) % P;
  170. a.resize(l);
  171. copy(da, da + l, a.a);
  172. return a;
  173. }
  174. int tn = 1;
  175. while (tn < l)
  176. tn <<= 1;
  177. int curG = modpow(FFT_G, (P - 1) / tn);
  178. preGPow[0] = 1;
  179. for (int i = 1; i < tn; i++)
  180. preGPow[i] = (s64)preGPow[i - 1] * curG % P;
  181. a.resize(tn);
  182. fft(a.a, tn, 0, da);
  183. copy(b.a, b.a + b_d + 1, a.a);
  184. fill(a.a + b_d + 1, a.a + tn, 0);
  185. fft(a.a, tn, 0, db);
  186. for (int i = 0; i < tn; i++)
  187. da[i] = (s64)da[i] * db[i] % P;
  188. reverse(preGPow + 1, preGPow + tn);
  189. fft(da, tn, 0, a.a);
  190. int revTN = modpow(tn, P - 2);
  191. for (int i = 0; i < tn; i++)
  192. a.a[i] = (s64)a.a[i] * revTN % P;
  193. return a;
  194. }
  195. inline polyarray &polyaddto(polyarray &a, const polyarray &b, int off = 0)
  196. {
  197. int b_d = b.getDegree();
  198. if (a.n <= b_d + off)
  199. a.resize(b_d + off + 1);
  200. for (int i = b_d; i >= 0; i--)
  201. modaddto(a.a[i + off], b.a[i]);
  202. return a;
  203. }
  204. struct halfEdge
  205. {
  206. int u;
  207. halfEdge *next;
  208. };
  209. halfEdge adj_pool[MaxM * 2], *adj_tail = adj_pool;
  210. int n, m;
  211. halfEdge *adj[MaxN + 1];
  212. inline void addEdge(int v, int u)
  213. {
  214. adj_tail->u = u, adj_tail->next = adj[v], adj[v] = adj_tail++;
  215. }
  216. typedef pair<halfEdge**, halfEdge*> recoverEdgeType;
  217. inline recoverEdgeType delEdge(int v, int u)
  218. {
  219. for (halfEdge *e = adj[v], **prev = &adj[v]; e; prev = &e->next, e = e->next)
  220. if (e->u == u)
  221. {
  222. *prev = e->next;
  223. return recoverEdgeType(prev, e);
  224. }
  225. assert(false);
  226. }
  227. inline void recoverEdge(const recoverEdgeType &r)
  228. {
  229. *r.first = r.second;
  230. }
  231. inline halfEdge *oppoE(halfEdge *e)
  232. {
  233. return adj_pool + ((e - adj_pool) ^ 1);
  234. }
  235. int dfsCnt;
  236. int dfn[MaxN + 1];
  237. halfEdge *faE[MaxN + 1], *moE[MaxN + 1];
  238. void dfs(int v)
  239. {
  240. dfn[v] = ++dfsCnt;
  241. for (halfEdge *e = adj[v]; e; e = e->next)
  242. if (e != faE[v] && e != moE[v])
  243. {
  244. if (!dfn[e->u])
  245. {
  246. faE[e->u] = oppoE(e), moE[e->u] = NULL;
  247. dfs(e->u);
  248. }
  249. else if (dfn[e->u] < dfn[v])
  250. {
  251. assert(moE[v] == NULL);
  252. int u = v;
  253. halfEdge *lastE = e;
  254. while (u != e->u)
  255. {
  256. moE[u] = lastE;
  257. lastE = oppoE(faE[u]);
  258. u = faE[u]->u;
  259. }
  260. }
  261. }
  262. }
  263. #define cactus_children_for_each(v, e) for (halfEdge *e = adj[v]; e; e = e->next) if (e != faE[v] && e != moE[v] && faE[e->u] == oppoE(e))
  264. #define cir_for_each(v, vu) for (int vu = v; vu != faE[v]->u; vu = moE[vu]->u)
  265. inline int cir_beg(int v)
  266. {
  267. int u = v;
  268. while (oppoE(faE[u]) == moE[faE[u]->u])
  269. u = faE[u]->u;
  270. return u;
  271. }
  272. inline int cir_end(int v)
  273. {
  274. int u = v;
  275. while (oppoE(moE[u]) == faE[moE[u]->u])
  276. u = moE[u]->u;
  277. return u;
  278. }
  279. inline int cir_len(int v)
  280. {
  281. int l = 1;
  282. cir_for_each(v, u)
  283. l++;
  284. return l;
  285. }
  286. int up_root[MaxN + 1];
  287. int up_vC[MaxN + 1];
  288. int up_vS[MaxN + 1];
  289. int up_inh_l[MaxN + 1];
  290. polyarray up_poly[MaxN + 1];
  291. polyarray calc(int root, int last_vC);
  292. polyarray calc_under(int v, int last_vC)
  293. {
  294. polyarray under;
  295. if (moE[v])
  296. {
  297. recoverEdgeType r1 = delEdge(v, faE[v]->u);
  298. recoverEdgeType r2 = delEdge(v, moE[v]->u);
  299. faE[v] = moE[v] = NULL;
  300. under = calc(v, last_vC);
  301. faE[v] = r1.second;
  302. moE[v] = r2.second;
  303. recoverEdge(r2);
  304. recoverEdge(r1);
  305. }
  306. else
  307. {
  308. recoverEdgeType r = delEdge(v, faE[v]->u);
  309. faE[v] = NULL;
  310. under = calc(v, last_vC);
  311. faE[v] = r.second;
  312. recoverEdge(r);
  313. }
  314. return under;
  315. }
  316. polyarray calc_children_under(int v, int last_vC)
  317. {
  318. polyarray under;
  319. under.resize(1);
  320. under.a[0] = 1;
  321. cactus_children_for_each(v, e)
  322. {
  323. if (moE[e->u])
  324. {
  325. int totL = cir_len(e->u);
  326. int curL = 1;
  327. cir_for_each(e->u, u)
  328. {
  329. polyarray cur = calc_under(u, last_vC);
  330. polyaddto(under, cur, curL);
  331. polyaddto(under, cur, totL - curL);
  332. curL++;
  333. }
  334. }
  335. else
  336. {
  337. polyarray cur = calc_under(e->u, last_vC);
  338. polyaddto(under, cur, 1);
  339. }
  340. }
  341. return under;
  342. }
  343. polyarray calc(int root, int last_vC)
  344. {
  345. int q_n = 0;
  346. static int q[MaxN];
  347. q[q_n++] = root;
  348. for (int i = 0; i < q_n; i++)
  349. {
  350. int v = q[i];
  351. cactus_children_for_each(v, e)
  352. {
  353. if (moE[e->u])
  354. {
  355. cir_for_each(e->u, u)
  356. q[q_n++] = u;
  357. }
  358. else
  359. q[q_n++] = e->u;
  360. }
  361. }
  362. int vC = -1, wC = INT_MAX;
  363. static int wei[MaxN + 1];
  364. for (int i = q_n - 1; i >= 0; i--)
  365. {
  366. int v = q[i];
  367. int wV = 0;
  368. wei[v] = 1;
  369. cactus_children_for_each(v, e)
  370. {
  371. if (moE[e->u])
  372. {
  373. int curW = 0;
  374. int vU = -1, wU = 0;
  375. cir_for_each(e->u, u)
  376. {
  377. curW += wei[u];
  378. if (vU == -1 || wei[u] > wei[vU])
  379. vU = u;
  380. }
  381. wei[v] += curW;
  382. relax(wV, wei[vU]);
  383. relax(wU, q_n - curW);
  384. cir_for_each(e->u, u)
  385. {
  386. if (e->u != vU)
  387. relax(wU, wei[e->u]);
  388. }
  389. cactus_children_for_each(vU, eu)
  390. {
  391. if (moE[eu->u])
  392. {
  393. cir_for_each(eu->u, k)
  394. relax(wU, wei[k]);
  395. }
  396. else
  397. relax(wU, wei[eu->u]);
  398. }
  399. if (wU < wC)
  400. vC = vU, wC = wU;
  401. }
  402. else
  403. {
  404. wei[v] += wei[e->u];
  405. relax(wV, wei[e->u]);
  406. }
  407. }
  408. relax(wV, q_n - wei[v]);
  409. if (!moE[v])
  410. {
  411. if (wV < wC)
  412. vC = v, wC = wV;
  413. }
  414. }
  415. int vB = moE[vC] ? cir_beg(vC) : vC;
  416. polyarray res;
  417. if (moE[vC])
  418. {
  419. recoverEdgeType r1 = delEdge(faE[vB]->u, cir_end(vB));
  420. recoverEdgeType r2 = delEdge(faE[vB]->u, vB);
  421. res = calc(root, last_vC);
  422. recoverEdge(r2);
  423. recoverEdge(r1);
  424. }
  425. else if (vC != root)
  426. {
  427. recoverEdgeType r = delEdge(faE[vC]->u, vC);
  428. res = calc(root, last_vC);
  429. recoverEdge(r);
  430. }
  431. int inh_l = 0;
  432. polyarray above;
  433. above.resize(1);
  434. above.a[0] = 1;
  435. int vS = moE[vC] ? faE[vB]->u : vC;
  436. if (vS != root)
  437. {
  438. int vT = vS;
  439. if (vT == vC)
  440. {
  441. inh_l++;
  442. vT = faE[vT]->u;
  443. }
  444. for (int v = vT, p = vT; p != root; v = up_vC[v])
  445. {
  446. while (p != up_vS[v])
  447. {
  448. if (moE[p])
  449. {
  450. int s = faE[cir_beg(p)]->u;
  451. int le_l = 0, ri_l = 0;
  452. for (int u = p; u != s; u = faE[u]->u)
  453. le_l++;
  454. for (int u = p; u != s; u = moE[u]->u)
  455. ri_l++;
  456. inh_l += min(le_l, ri_l);
  457. polyaddto(above, above, abs(le_l - ri_l));
  458. p = s;
  459. }
  460. else
  461. {
  462. inh_l++;
  463. p = faE[p]->u;
  464. }
  465. }
  466. inh_l += up_inh_l[v];
  467. polymulto(above, up_poly[v]);
  468. p = up_root[v];
  469. }
  470. }
  471. up_root[vC] = root;
  472. up_vS[vC] = vS;
  473. up_vC[vC] = last_vC;
  474. up_inh_l[vC] = inh_l;
  475. up_poly[vC] = above;
  476. polyarray under;
  477. if (moE[vB])
  478. {
  479. int totL = cir_len(vB);
  480. int curL = 1;
  481. cir_for_each(vB, u)
  482. {
  483. polyarray cur = u != vC ? calc_under(u, vC) : calc_children_under(u, vC);
  484. polyaddto(under, cur, curL);
  485. polyaddto(under, cur, totL - curL);
  486. curL++;
  487. }
  488. }
  489. else
  490. {
  491. under = calc_children_under(vC, vC);
  492. up_inh_l[vC] = inh_l;
  493. up_poly[vC] = above;
  494. }
  495. polymulto(under, above);
  496. polyaddto(res, under, inh_l);
  497. return res;
  498. }
  499. int main()
  500. {
  501. cin >> n >> m;
  502. for (int i = 0; i < m; i++)
  503. {
  504. int v, u;
  505. scanf("%d %d", &v, &u);
  506. addEdge(v, u), addEdge(u, v);
  507. }
  508. dfsCnt = 0;
  509. faE[1] = moE[1] = NULL;
  510. dfs(1);
  511. polyarray res = calc(1, 0);
  512. int res_d = res.getDegree();
  513. for (int k = 1; k < n; k++)
  514. printf("%d\n", k <= res_d ? res.a[k] : 0);
  515. return 0;
  516. }

动态仙人掌

介绍

动态仙人掌问题
一类在仙人掌上动态维护信息的问题,动态维护包含修改形态和修改相关信息两种。

没错,最后我们要讲的就是我们前一段学的 Link-Cut Tree 在仙人掌上的迁移: Link-Cut Cactus.

结构

那么首先面临的一个难题是,什么是实边,什么是虚边?
我们分三种情况进行讨论:
4

树上的情况

LCT 维护的是熟的一条链,于是我们可以维护仙人掌的一条链。

没有环的话,就跟 LCT 一样,每个结点有一个偏爱孩子,用实边连起来,其它儿子用虚边连起来。

环上的情况

对于一个环,我们定义它的父亲为环上离仙人掌的根最近的节点,记为 A ,我们定义换上单的偏爱孩子为环上最后一次 access 到的节点,记为 B 。

那么根据定义,我们将 A 和 B 的最短路用实边连起来。

对于环上的其他结点构成的链,也用实边连起来,我们称这条链为\textbf{额外链}。

特殊情况

如前图中的二逼情况所示,如果我们 access 一个环的跟的时候,就会出现这种情况,我们需要对此加个特判。

注意:
以下三种情况也是不被允许的:
5

环上信息的处理

为了更为方便地操作环,我们需要对环上的信息进行处理。

需要注意的是:在没有额外链的情况下,会出现有一条黑边没存的情况。

7
我们只需要开个 来记录这条边即可。

换根操作

我们在仙人掌上换根是不能直接套用树上的方法,因为这样会导致额外链以及 的方向不正确。

不过问题是可解的,我们对于 可以比较其在辅助树中的先后顺序,如果反了就将环信息中的 指针对调。

之后的步骤就显而易见了:
10

实现

伪代码 by Vfleaking

  1. void access(x)
  2. {
  3. for (p = x, q = NULL; p; q = p, p = p->fa)
  4. {
  5. splay(p);
  6. if (p->prevE && p->prevE->cir) // 判断p是否在环上。注意环的根不算作在这个环上。
  7. {
  8. isTogether = false; // 判断是否是2B情况。
  9. cir = p->prevE->cir; // 获取p->prevE所在的环的信息
  10. // 由于p可能在额外链上而之前很狗血地splay了,会导致记录的pEx不正确。
  11. if (cir->pEx && !cir->pEx->isRoot())
  12. cir->pEx = p;
  13. splay(cir->pB);
  14. splay(cir->pA);
  15. if (cir->pB->isRoot()) // 2B情况
  16. {
  17. if (cir->pB->fa != cir->pA) // 如果pA、pB顺序不对则进行调整
  18. {
  19. swap(cir->pA, cir->pB);
  20. if (cir->pEx)
  21. cir->pEx->tag_rev(); // 打上翻转标记
  22. }
  23. }
  24. else // 文艺情况
  25. {
  26. isTogether = true;
  27. splay_until(cir->pB, cir->pA); // 把pB splay到pA下面
  28. if (cir->pA->lc == cir->pB) // 如果pA、pB顺序不对则进行调整
  29. {
  30. rotate(cir->pB); // 一次旋转把pB转成根
  31. swap(cir->pA, cir->pB);
  32. if (cir->pEx)
  33. cir->pEx->tag_rev(); // 打上翻转标记
  34. }
  35. cir->pA->rc = NULL;
  36. cir->pA->nextE = NULL; // 暂时断开pA与下面部分的链接转化为2B情况
  37. }
  38. cir->pB->rc = cir->pEx;
  39. // pEx为空的情况,用missingE补上
  40. cir->pB->nextE = cir->pEx ? cir->pEx->msg.firstE : cir->missingE;
  41. if (cir->pEx)
  42. cir->pEx->fa = cir->pB;
  43. // 这样环就被整个地接了起来成为了一棵splay。
  44. p->splay();
  45. // 比较哪边走比较短,如果不是往左走短就调整一下
  46. if (p->getLcTotL() > p->getRcTotL())
  47. {
  48. p->tag_rev();
  49. p->tag_down();
  50. }
  51. cir->pB = p;
  52. cir->pEx = p->rc; // 把较长的那条变为额外链
  53. cir->missingE = p->rc ? NULL : p->nextE; // pEx为空的情况,用missingE补上
  54. if (cir->pEx)
  55. cir->pEx->fa = NULL;
  56. p->rc = q;
  57. p->nextE = q ? q->msg.firstE : NULL;
  58. p->update();
  59. if (isTogether) // 如果是文艺情况还得把pA接回来
  60. {
  61. cir->pA->rc = p;
  62. cir->pA->nextE = p->msg.firstE;
  63. p->splay();
  64. }
  65. }
  66. else // 普通情况
  67. {
  68. p->rc = q;
  69. p->nextE = q ? q->msg.firstE : NULL;
  70. p->update();
  71. }
  72. }
  73. }

[UOJ65]动态仙人掌 III

标程 by Vfleaking

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <climits>
  8. using namespace std;
  9. const int INF = INT_MAX;
  10. const int MaxN = 100000;
  11. inline int getint()
  12. {
  13. char c;
  14. while (c = getchar(), ('0' > c || c > '9') && c != '-');
  15. if (c != '-')
  16. {
  17. int res = c - '0';
  18. while (c = getchar(), '0' <= c && c <= '9')
  19. res = res * 10 + c - '0';
  20. return res;
  21. }
  22. else
  23. {
  24. int res = 0;
  25. while (c = getchar(), '0' <= c && c <= '9')
  26. res = res * 10 + c - '0';
  27. return -res;
  28. }
  29. }
  30. template <class T>
  31. class BlockAllocator
  32. {
  33. private:
  34. static const int BlockL = 10000;
  35. union TItem
  36. {
  37. char rt[sizeof(T)];
  38. TItem *next;
  39. };
  40. TItem *pool, *tail;
  41. TItem *unused;
  42. public:
  43. BlockAllocator()
  44. {
  45. pool = NULL;
  46. unused = NULL;
  47. }
  48. T *allocate()
  49. {
  50. TItem *p;
  51. if (unused)
  52. {
  53. p = unused;
  54. unused = unused->next;
  55. }
  56. else
  57. {
  58. if (pool == NULL)
  59. pool = new TItem[BlockL], tail = pool;
  60. p = tail++;
  61. if (tail == pool + BlockL)
  62. pool = NULL;
  63. }
  64. return (T*)p;
  65. }
  66. void deallocate(T *pt)
  67. {
  68. TItem *p = (TItem*)pt;
  69. p->next = unused, unused = p;
  70. }
  71. };
  72. struct edgeWeight;
  73. struct path_message;
  74. struct lcc_circle;
  75. struct lcc_edge;
  76. struct lcc_message;
  77. struct lcc_node;
  78. struct edgeWeight
  79. {
  80. int wA, wB;
  81. edgeWeight(){}
  82. edgeWeight(const int &_wA, const int &_wB)
  83. : wA(_wA), wB(_wB){}
  84. friend inline bool operator==(const edgeWeight &lhs, const edgeWeight &rhs)
  85. {
  86. return lhs.wA == rhs.wA && lhs.wB == rhs.wB;
  87. }
  88. friend inline bool operator!=(const edgeWeight &lhs, const edgeWeight &rhs)
  89. {
  90. return lhs.wA != rhs.wA || lhs.wB != rhs.wB;
  91. }
  92. };
  93. struct path_message
  94. {
  95. int minLA;
  96. int minWB;
  97. path_message(){}
  98. path_message(const edgeWeight &ew)
  99. : minLA(ew.wA), minWB(ew.wB){}
  100. path_message(const int &_minLA, const int &_minWB)
  101. : minLA(_minLA), minWB(_minWB){}
  102. void setEmpty()
  103. {
  104. minLA = 0;
  105. minWB = INF;
  106. }
  107. void setInvalid()
  108. {
  109. minLA = -1;
  110. minWB = -1;
  111. }
  112. bool valid() const
  113. {
  114. return minLA != -1;
  115. }
  116. void setMultiple()
  117. {
  118. minWB = -1;
  119. }
  120. friend inline path_message operator+(const path_message &lhs, const path_message &rhs)
  121. {
  122. if (lhs.minLA < rhs.minLA)
  123. return lhs;
  124. else if (rhs.minLA < lhs.minLA)
  125. return rhs;
  126. else
  127. return path_message(lhs.minLA, -1);
  128. }
  129. friend inline path_message operator*(const path_message &lhs, const path_message &rhs)
  130. {
  131. return path_message(lhs.minLA + rhs.minLA, min(lhs.minWB, rhs.minWB));
  132. }
  133. };
  134. struct lcc_circle
  135. {
  136. lcc_node *pA, *pB;
  137. lcc_node *pEx;
  138. lcc_edge *missingE;
  139. bool equalL;
  140. };
  141. struct lcc_edge
  142. {
  143. edgeWeight ew;
  144. lcc_circle *cir;
  145. inline lcc_circle *getCir()
  146. {
  147. return this ? this->cir : NULL;
  148. }
  149. };
  150. struct lcc_message
  151. {
  152. path_message pathMsg;
  153. lcc_edge *firstE, *lastE;
  154. bool hasCir;
  155. bool hasMultiplePath;
  156. void rev()
  157. {
  158. swap(firstE, lastE);
  159. }
  160. void coverCir(lcc_circle *cir, bool isSingle)
  161. {
  162. hasCir = !isSingle && cir != NULL;
  163. hasMultiplePath = false;
  164. if (cir && firstE->getCir() != cir && lastE->getCir() != cir)
  165. {
  166. if (cir->equalL)
  167. hasMultiplePath = true;
  168. }
  169. }
  170. void addWB(int delta, bool isSingle)
  171. {
  172. if (!isSingle)
  173. pathMsg.minWB += delta;
  174. }
  175. friend inline lcc_message operator+(const lcc_message &lhs, const lcc_message &rhs)
  176. {
  177. lcc_message res;
  178. assert(lhs.lastE == rhs.firstE);
  179. lcc_edge *e = lhs.lastE;
  180. res.pathMsg = lhs.pathMsg * path_message(e->ew) * rhs.pathMsg;
  181. res.hasMultiplePath = lhs.hasMultiplePath || rhs.hasMultiplePath;
  182. if (e->cir && lhs.firstE->getCir() != e->cir && rhs.lastE->getCir() != e->cir)
  183. {
  184. if (e->cir->equalL)
  185. res.hasMultiplePath = true;
  186. }
  187. res.firstE = lhs.firstE, res.lastE = rhs.lastE;
  188. res.hasCir = lhs.hasCir || e->cir || rhs.hasCir;
  189. return res;
  190. }
  191. };
  192. struct lcc_node
  193. {
  194. lcc_node *fa, *lc, *rc;
  195. lcc_edge *prevE, *nextE;
  196. lcc_message msg;
  197. bool hasRev;
  198. bool hasCoveredCir;
  199. lcc_circle *coveredCir;
  200. int wBDelta;
  201. bool isRoot()
  202. {
  203. return !fa || (fa->lc != this && fa->rc != this);
  204. }
  205. void rotate()
  206. {
  207. lcc_node *x = this, *y = x->fa, *z = y->fa;
  208. lcc_node *b = x == y->lc ? x->rc : x->lc;
  209. x->fa = z, y->fa = x;
  210. if (b)
  211. b->fa = y;
  212. if (z)
  213. {
  214. if (z->lc == y)
  215. z->lc = x;
  216. else if (z->rc == y)
  217. z->rc = x;
  218. }
  219. if (y->lc == x)
  220. x->rc = y, y->lc = b;
  221. else
  222. x->lc = y, y->rc = b;
  223. y->update();
  224. }
  225. void allFaTagDown()
  226. {
  227. int anc_n = 0;
  228. static lcc_node *anc[MaxN];
  229. anc[anc_n++] = this;
  230. for (int i = 0; !anc[i]->isRoot(); i++)
  231. anc[anc_n++] = anc[i]->fa;
  232. for (int i = anc_n - 1; i >= 0; i--)
  233. anc[i]->tag_down();
  234. }
  235. void splay()
  236. {
  237. allFaTagDown();
  238. while (!this->isRoot())
  239. {
  240. if (!fa->isRoot())
  241. {
  242. if ((fa->lc == this) == (fa->fa->lc == fa))
  243. fa->rotate();
  244. else
  245. this->rotate();
  246. }
  247. this->rotate();
  248. }
  249. this->update();
  250. }
  251. void splay_until(lcc_node *target)
  252. {
  253. allFaTagDown();
  254. while (this->fa != target)
  255. {
  256. if (fa->fa != target)
  257. {
  258. if ((fa->lc == this) == (fa->fa->lc == fa))
  259. fa->rotate();
  260. else
  261. this->rotate();
  262. }
  263. this->rotate();
  264. }
  265. this->update();
  266. }
  267. int getLcTotL()
  268. {
  269. if (!prevE)
  270. return 0;
  271. int totL = prevE->ew.wA;
  272. if (lc)
  273. totL += lc->msg.pathMsg.minLA + msg.firstE->ew.wA;
  274. return totL;
  275. }
  276. int getRcTotL()
  277. {
  278. if (!nextE)
  279. return 0;
  280. int totL = nextE->ew.wA;
  281. if (rc)
  282. totL += rc->msg.pathMsg.minLA + msg.lastE->ew.wA;
  283. return totL;
  284. }
  285. void access()
  286. {
  287. for (lcc_node *p = this, *q = NULL; p; q = p, p = p->fa)
  288. {
  289. p->splay();
  290. if (p->prevE && p->prevE->cir)
  291. {
  292. bool isTogether = false;
  293. lcc_circle *cir = p->prevE->cir;
  294. if (cir->pEx && !cir->pEx->isRoot())
  295. cir->pEx = p;
  296. cir->pB->splay(), cir->pA->splay();
  297. if (cir->pB->isRoot())
  298. {
  299. if (cir->pB->fa != cir->pA)
  300. {
  301. swap(cir->pA, cir->pB);
  302. if (cir->pEx)
  303. cir->pEx->tag_rev();
  304. }
  305. }
  306. else
  307. {
  308. isTogether = true;
  309. cir->pB->splay_until(cir->pA);
  310. if (cir->pA->lc == cir->pB)
  311. {
  312. cir->pB->rotate();
  313. swap(cir->pA, cir->pB);
  314. if (cir->pEx)
  315. cir->pEx->tag_rev();
  316. }
  317. cir->pA->rc = NULL, cir->pA->nextE = NULL;
  318. }
  319. cir->pB->rc = cir->pEx, cir->pB->nextE = cir->pEx ? cir->pEx->msg.firstE : cir->missingE;
  320. if (cir->pEx)
  321. cir->pEx->fa = cir->pB;
  322. p->splay();
  323. if (p->getLcTotL() > p->getRcTotL())
  324. p->tag_rev(), p->tag_down();
  325. cir->pB = p;
  326. cir->pEx = p->rc, cir->missingE = p->rc ? NULL : p->nextE;
  327. cir->equalL = p->getLcTotL() == p->getRcTotL();
  328. if (cir->pEx)
  329. cir->pEx->fa = NULL;
  330. p->rc = q, p->nextE = q ? q->msg.firstE : NULL;
  331. p->update();
  332. if (isTogether)
  333. {
  334. cir->pA->rc = p, cir->pA->nextE = p->msg.firstE;
  335. p->splay();
  336. }
  337. }
  338. else
  339. {
  340. p->rc = q, p->nextE = q ? q->msg.firstE : NULL;
  341. p->update();
  342. }
  343. }
  344. this->splay();
  345. }
  346. void makeRoot()
  347. {
  348. this->access();
  349. this->tag_rev(), this->tag_down();
  350. }
  351. lcc_node *findRoot()
  352. {
  353. lcc_node *p = this;
  354. p->access();
  355. while (p->tag_down(), p->lc)
  356. p = p->lc;
  357. p->splay();
  358. return p;
  359. }
  360. void tag_rev()
  361. {
  362. hasRev = !hasRev;
  363. msg.rev();
  364. }
  365. void tag_coverCir(lcc_circle *cir)
  366. {
  367. hasCoveredCir = true;
  368. coveredCir = cir;
  369. msg.coverCir(cir, !lc && !rc);
  370. }
  371. void tag_addWB(int delta)
  372. {
  373. wBDelta += delta;
  374. msg.addWB(delta, !lc && !rc);
  375. }
  376. void tag_down()
  377. {
  378. if (hasRev)
  379. {
  380. swap(lc, rc);
  381. swap(prevE, nextE);
  382. if (lc)
  383. lc->tag_rev();
  384. if (rc)
  385. rc->tag_rev();
  386. hasRev = false;
  387. }
  388. if (hasCoveredCir)
  389. {
  390. if (lc)
  391. {
  392. prevE->cir = coveredCir;
  393. lc->tag_coverCir(coveredCir);
  394. }
  395. if (rc)
  396. {
  397. nextE->cir = coveredCir;
  398. rc->tag_coverCir(coveredCir);
  399. }
  400. hasCoveredCir = false;
  401. }
  402. if (wBDelta != 0)
  403. {
  404. if (lc)
  405. {
  406. prevE->ew.wB += wBDelta;
  407. lc->tag_addWB(wBDelta);
  408. }
  409. if (rc)
  410. {
  411. nextE->ew.wB += wBDelta;
  412. rc->tag_addWB(wBDelta);
  413. }
  414. wBDelta = 0;
  415. }
  416. }
  417. void update()
  418. {
  419. msg.pathMsg.setEmpty();
  420. msg.firstE = prevE, msg.lastE = nextE;
  421. msg.hasCir = false;
  422. msg.hasMultiplePath = false;
  423. if (lc)
  424. msg = lc->msg + msg;
  425. if (rc)
  426. msg = msg + rc->msg;
  427. }
  428. };
  429. int n;
  430. lcc_node lccVer[MaxN + 1];
  431. BlockAllocator<lcc_edge> lccEAllocator;
  432. BlockAllocator<lcc_circle> lccCirAllocator;
  433. void cactus_init()
  434. {
  435. for (int v = 1; v <= n; v++)
  436. {
  437. lcc_node *x = lccVer + v;
  438. x->fa = x->lc = x->rc = NULL;
  439. x->prevE = x->nextE = NULL;
  440. x->hasRev = false;
  441. x->hasCoveredCir = false;
  442. x->wBDelta = 0;
  443. x->update();
  444. }
  445. }
  446. bool cactus_link(int v, int u, int wA, int wB)
  447. {
  448. if (v == u)
  449. return false;
  450. edgeWeight ew(wA, wB);
  451. lcc_node *x = lccVer + v, *y = lccVer + u;
  452. x->makeRoot(), y->makeRoot();
  453. if (x->fa)
  454. {
  455. x->access();
  456. if (x->msg.hasCir)
  457. return false;
  458. lcc_circle *cir = lccCirAllocator.allocate();
  459. lcc_edge *e = lccEAllocator.allocate();
  460. e->ew = ew, e->cir = cir;
  461. cir->pA = y, cir->pB = x, cir->pEx = NULL;
  462. cir->missingE = e;
  463. x->tag_coverCir(cir);
  464. x->access();
  465. }
  466. else
  467. {
  468. lcc_edge *e = lccEAllocator.allocate();
  469. e->ew = ew, e->cir = NULL;
  470. x->fa = y, x->prevE = e, x->update();
  471. }
  472. return true;
  473. }
  474. bool cactus_cut(int v, int u, int wA, int wB)
  475. {
  476. if (v == u)
  477. return false;
  478. edgeWeight ew(wA, wB);
  479. lcc_node *x = lccVer + v, *y = lccVer + u;
  480. if (x->findRoot() != y->findRoot())
  481. return false;
  482. y->makeRoot(), x->access();
  483. y->splay_until(x);
  484. lcc_circle *cir = x->prevE->cir;
  485. if (cir && cir->pA == y && !cir->pEx && cir->missingE->ew == ew)
  486. {
  487. lcc_edge *e = cir->missingE;
  488. x->tag_coverCir(NULL);
  489. lccCirAllocator.deallocate(cir);
  490. lccEAllocator.deallocate(e);
  491. return true;
  492. }
  493. if (!y->rc && x->prevE->ew == ew)
  494. {
  495. lcc_edge *e = x->prevE;
  496. lccEAllocator.deallocate(e);
  497. if (cir)
  498. {
  499. if (cir->pEx)
  500. {
  501. cir->pEx->tag_rev();
  502. cir->pEx->fa = y, y->rc = cir->pEx;
  503. y->nextE = cir->pEx->msg.firstE;
  504. x->prevE = cir->pEx->msg.lastE;
  505. }
  506. else
  507. y->nextE = x->prevE = cir->missingE;
  508. y->update(), x->update();
  509. x->tag_coverCir(NULL);
  510. lccCirAllocator.deallocate(cir);
  511. }
  512. else
  513. {
  514. y->fa = NULL, y->nextE = NULL, y->update();
  515. x->lc = NULL, x->prevE = NULL, x->update();
  516. }
  517. return true;
  518. }
  519. return false;
  520. }
  521. bool cactus_add(int qv, int qu, int delta)
  522. {
  523. lcc_node *x = lccVer + qv, *y = lccVer + qu;
  524. if (x->findRoot() != y->findRoot())
  525. return false;
  526. x->makeRoot(), y->access();
  527. if (y->msg.hasMultiplePath)
  528. return false;
  529. y->tag_addWB(delta);
  530. return true;
  531. }
  532. path_message cactus_query(int qv, int qu)
  533. {
  534. path_message res;
  535. lcc_node *x = lccVer + qv, *y = lccVer + qu;
  536. if (x->findRoot() != y->findRoot())
  537. {
  538. res.setInvalid();
  539. return res;
  540. }
  541. x->makeRoot(), y->access();
  542. res = y->msg.pathMsg;
  543. if (y->msg.hasMultiplePath)
  544. res.setMultiple();
  545. return res;
  546. }
  547. int main()
  548. {
  549. int nQ;
  550. cin >> n >> nQ;
  551. cactus_init();
  552. while (nQ--)
  553. {
  554. char type;
  555. while (type = getchar(), type != 'l' && type != 'c' && type != 'a' && type != 'd');
  556. if (type == 'l')
  557. {
  558. int v = getint(), u = getint(), wA = getint(), wB = getint();
  559. if (cactus_link(v, u, wA, wB))
  560. printf("ok\n");
  561. else
  562. printf("failed\n");
  563. }
  564. else if (type == 'c')
  565. {
  566. int v = getint(), u = getint(), wA = getint(), wB = getint();
  567. if (cactus_cut(v, u, wA, wB))
  568. printf("ok\n");
  569. else
  570. printf("failed\n");
  571. }
  572. else if (type == 'a')
  573. {
  574. int v = getint(), u = getint(), delta = getint();
  575. if (cactus_add(v, u, delta))
  576. printf("ok\n");
  577. else
  578. printf("failed\n");
  579. }
  580. else if (type == 'd')
  581. {
  582. int v = getint(), u = getint();
  583. path_message ret = cactus_query(v, u);
  584. printf("%d %d\n", ret.minLA, ret.minWB);
  585. }
  586. else
  587. {
  588. puts("error!");
  589. }
  590. }
  591. return 0;
  592. }

广义圆方树

经过前面各类仙人掌题目的讲解,大家恐怕已经对仙人掌产生了恐惧,而事实上,有难度的仙人掌题在近几年也只是在国家集训队水平的比赛里才会出现。

不过,这不是说仙人掌对国集水平以下的选手意义不大:

根据圆方树和仙人掌等价性定理,我们可以为一张一般的图建立一棵等效仙人掌:一个环代表一个点双。

这样的一种思想可能在思路枯竭时解救我们。

Tourists

据说,这是一道 NOIP 难度的仙人掌题。

题目

给出一张带点权图,每次询问两点之间的简单路径中,权值最小的权值是多少。点数 、边数 、询问数

由于路径太多,直接考虑点双不方便,我们考虑建立圆方树、转化为等效仙人掌;

这样问题就转化为:两点所有简单路径的并集中权值的最小值;

只需将每个方点上的权值设为其连出所有圆点的最小值,这样问题就转化为链上的最小值。

之后只需并查集求 LCA 时顺便求一下最小值即可,本题是 NOIP 难度得证。

参考文献

[1] immortalCO. Making Graph into Trees[R]. 绍兴:中国计算机学会, 2017.
[2] Gintoki. 浅谈仙人掌问题[R]. 绍兴:中国计算机学会, 2017.
[3] 王逸松. 仙人掌相关算法及其应用[R]. 杭州:中国计算机学会, 2015.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注