1. //Achen
  2. #include<bits/stdc++.h>
  3. #define For(i,a,b) for(int i=(a);i<=(b);i++)
  4. #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
  5. #define Formylove return 0
  6. const int N=2e6+,mod=;
  7. typedef long long LL;
  8. typedef double db;
  9. using namespace std;
  10. int n,m,Q;
  11.  
  12. template<typename T> void read(T &x) {
  13. char ch=getchar(); T f=; x=;
  14. while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
  15. if(ch=='-') f=-,ch=getchar();
  16. for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
  17. }
  18.  
  19. LL ans,Hdis[N];
  20. int isnode[N];
  21. struct VMtree{
  22. int ecnt,fir[N],nxt[N],to[N]; LL val[N];
  23. void add(int u,int v,LL w) {
  24. nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
  25. nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
  26. //printf("%d %d %lld\n",u,v,w);
  27. }
  28. LL f[N];
  29. int que[N];
  30. void dfs(int x,int fa) {
  31. f[x]=;
  32. for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa)
  33. dfs(to[i],x);
  34. if(x<=n) {
  35. for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
  36. int y=to[i];
  37. ans=max(ans,f[y]+val[i]+f[x]); f[x]=max(f[x],f[y]+val[i]);
  38. }
  39. }
  40. else {
  41. int ql=,qr=;
  42. for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
  43. int y=to[i];
  44. f[x]=max(f[x],f[y]+val[i]);
  45. while(ql<=qr&&Hdis[y]-Hdis[que[ql]]>Hdis[x]-(Hdis[y]-Hdis[que[ql]])) ql++;
  46. if(ql<=qr) ans=max(ans,Hdis[y]-Hdis[que[ql]]+f[que[ql]]+f[y]);
  47. while(ql<=qr&&f[y]-Hdis[y]>=f[que[qr]]-Hdis[que[qr]]) qr--;
  48. que[++qr]=y;
  49. }
  50. for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
  51. int y=to[i];
  52. while(ql<=qr&&Hdis[x]-(Hdis[que[ql]]-Hdis[y])>Hdis[que[ql]]-Hdis[y]) ql++;
  53. if(ql<=qr) ans=max(ans,Hdis[x]-(Hdis[que[ql]]-Hdis[y])+f[que[ql]]+f[y]);
  54. }
  55. } if(isnode[x]) ans=max(ans,f[x]);
  56. }
  57. }V;
  58.  
  59. int tot,dfn[N],a[N];
  60. bool cmp(const int &A,const int &B) { return dfn[A]<dfn[B]; }
  61. struct Tree {
  62. int ecnt,fir[N],nxt[N],to[N]; LL val[N];
  63. void add(int u,int v,LL w) {
  64. nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
  65. nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
  66. //printf("%d %d %lld\n",u,v,w);
  67. }
  68.  
  69. int R[N],f[N][]; LL H[N];
  70. void lca(int &cc,int x,int y) {
  71. if(R[x]<R[y]) swap(x,y);
  72. Rep(i,,) if(R[f[x][i]]>=R[y])
  73. x=f[x][i];
  74. if(x==y) { a[++cc]=x; return ; }
  75. Rep(i,,) if(f[x][i]!=f[y][i])
  76. x=f[x][i],y=f[y][i];
  77. if(f[x][]>n) {
  78. a[++cc]=x; a[++cc]=y;
  79. }
  80. a[++cc]=f[x][]; return ;
  81. }
  82.  
  83. int dfk,sz[N];
  84. int dfs(int x,int fa) {
  85. sz[x]=;
  86. f[x][]=fa;
  87. R[x]=R[fa]+;
  88. dfn[x]=++dfk;
  89. For(i,,) f[x][i]=f[f[x][i-]][i-];
  90. for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
  91. H[to[i]]=H[x]+val[i];
  92. dfs(to[i],x);
  93. sz[x]+=sz[to[i]];
  94. }
  95. }
  96.  
  97. bool in(int a,int b) { return dfn[a]>=dfn[b]&&dfn[a]<dfn[b]+sz[b]; }
  98. int sta[N],top;
  99. void solve() {
  100. int cnt,sz,rt=;
  101. read(cnt); sz=cnt;
  102. For(i,,cnt) { read(a[i]); isnode[a[i]]=; }
  103. sort(a+,a+cnt+,cmp);
  104. For(i,,cnt-) {
  105. lca(sz,a[i],a[i+]);
  106. }
  107. sort(a+,a+sz+,cmp);
  108. cnt=unique(a+,a+sz+)-(a+);
  109. For(i,,cnt) {
  110. if(!rt||R[a[i]]<R[rt]) rt=a[i];
  111. while(top&&!in(a[i],sta[top])) top--;
  112. if(top) V.add(sta[top],a[i],H[a[i]]-H[sta[top]]);
  113. sta[++top]=a[i];
  114. } while(top) top--;
  115. ans=; V.dfs(rt,);
  116. printf("%lld\n",ans);
  117. For(i,,cnt) { isnode[a[i]]=; V.fir[a[i]]=; } V.ecnt=;
  118. }
  119. }T;
  120.  
  121. struct Cactus {
  122. int ecnt,fir[N],nxt[N],to[N],val[N],vis[N];
  123. void add(int u,int v,int w) {
  124. nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
  125. nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
  126. }
  127.  
  128. int dfk,dfn[N],low[N],sta[N],top;
  129. void tarjan(int x) {
  130. dfn[x]=low[x]=++dfk;
  131. for(int i=fir[x];i;i=nxt[i]) if(!vis[i]) {
  132. sta[++top]=i;
  133. vis[i]=vis[i^]=;
  134. if(!dfn[to[i]]) {
  135. tarjan(to[i]);
  136. low[x]=min(low[x],low[to[i]]);
  137. if(low[to[i]]>=dfn[x]) {
  138. LL hc=,nc=,cc=;
  139. T.add(x,++tot,);
  140. Rep(j,top,) {
  141. hc+=val[sta[j]]; cc++;
  142. if(sta[j]==i) break;
  143. }
  144. Hdis[tot]=hc;
  145. while(top) {
  146. int j=sta[top--];
  147. if(to[j]!=x) {
  148. if(cc==) T.add(tot,to[j],hc);
  149. else T.add(tot,to[j],min(hc-nc,nc));
  150. Hdis[to[j]]=nc;
  151. }
  152. nc+=val[j];
  153. if(j==i) break;
  154. }
  155. }
  156. }
  157. else low[x]=min(low[x],dfn[to[i]]);
  158. }
  159. }
  160.  
  161. void init() { ecnt=; tot=n; }
  162. }C;
  163.  
  164. struct hash {
  165. int ecnt;
  166. struct edge{ int u,v,w; }e[N];
  167. vector<int>vc[N];
  168. void ins(int u,int v,int w) {
  169. if(u>v) swap(u,v);
  170. int hs=(u*+v)%mod,up=vc[hs].size();
  171. For(i,,up-) {
  172. int ec=vc[hs][i];
  173. if(e[ec].u==u&&e[ec].v==v) { e[ec].w=min(e[ec].w,w); return; }
  174. }
  175. e[++ecnt]=(edge){u,v,w}; vc[hs].push_back(ecnt);
  176. }
  177.  
  178. void add() {
  179. For(i,,ecnt) C.add(e[i].u,e[i].v,e[i].w);
  180. }
  181. }H;
  182.  
  183. int main() {
  184. //freopen("1.in","r",stdin);
  185. //freopen("1.out","w",stdout);
  186. read(n); read(m);
  187. For(i,,m) {
  188. int u,v,w;
  189. read(u); read(v); read(w);
  190. C.add(u,v,w);
  191. H.ins(u,v,w);
  192. }
  193. C.init(); H.add();
  194. C.tarjan();
  195. T.dfs(,);
  196. read(Q);
  197. For(i,,Q) T.solve();
  198. Formylove;
  199. }

写完花了1.5h找bug发现是数组开小了。

其实是因为太长实在不想看一直各种磨蹭各种骚扰其他同学。

无脑圆方树+虚树+单调队列优化dp。

如果两个人的lca是方点要把方点的两个圆儿子加进去。

http://uoj.ac/problem/87

uoj#87. mx的仙人掌的更多相关文章

  1. UOJ.87.mx的仙人掌(圆方树 虚树)(未AC)

    题目链接 本代码10分(感觉速度还行..). 建圆方树,预处理一些东西.对询问建虚树. 对于虚树上的圆点直接做:对于方点特判,枚举其所有儿子,如果子节点不在该方点代表的环中,跳到那个点并更新其val, ...

  2. SHOI2008 cactus仙人掌图 和 UOJ87 mx的仙人掌

    cactus仙人掌图 题目描述 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一 ...

  3. Solution -「UOJ #87」mx 的仙人掌

    \(\mathcal{Description}\)   Link.   给出含 \(n\) 个结点 \(m\) 条边的仙人掌图.\(q\) 次询问,每次询问给出一个点集 \(S\),求 \(S\) 内 ...

  4. UOJ #86 mx的组合数 (数位DP+NTT+原根优化)

    题目传送门 matthew99神犇的题解讲得非常清楚明白,跪烂Orzzzzzzzzzzzzz 总结一下,本题有很多重要的突破口 1.Lucas定理 看到n,m特别大但模数特别小时,容易想到$lucas ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. UOJ#23. 【UR #1】跳蚤国王下江南 仙人掌 Tarjan 点双 圆方树 点分治 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ23.html 题目传送门 - UOJ#23 题意 给定一个有 n 个节点的仙人掌(可能有重边). 对于所有 ...

  7. UOJ#290. 【ZJOI2017】仙人掌 仙人掌,Tarjan,计数,动态规划,树形dp,递推

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ290.html 题解 真是一道好题! 首先,如果不是仙人掌直接输出 0 . 否则,显然先把环上的边删光. ...

  8. uoj#158. 【清华集训2015】静态仙人掌

    http://uoj.ac/problem/158 预处理dfs序,询问转为区间1的个数,用可持久化bitset预处理出所有可能的修改对应哪些位置,然后用一个bitset维护当前每个点的状态,修改时可 ...

  9. uoj#290. 【ZJOI2017】仙人掌(数数+仙人掌+树形dp)

    传送门 这图可以说是非常形象了2333 模拟赛的时候打了个表发现为一条链的时候答案是\(2^{n-2}\)竟然顺便过了第一个点 然后之后订正的时候强联通分量打错了调了一个上午 首先不难发现我们可以去掉 ...

随机推荐

  1. Windows读写文件的猫腻

    这里主要涉及对于回车换行的讨论. 回车:\r 换行:\n Windows读写文件分为普通文件读写和二进制文件读写. 如果以二进制的方式读写文件(如rb, wb),将会完全的把文件内容读出来,不做任何处 ...

  2. /usr/lib/update-notifier/updates-available

     /usrb/update-notifier/update-motd-updates-available: 49: /usrb/update-notifier/update-motd-updates- ...

  3. 【Head First Servlets and JSP】笔记9:属性的作用域、线程安全

    什么是属性? 属性和参数 属性的3个作用域 属性API 属性不好的一面 1.到底什么是属性(Attribute)? 属性就是一个对象,可以被设置(bound,也可以叫绑定)到另外三个servlet A ...

  4. 跨平台移动开发_PhoneGap API 事件类型

    PhoneGap API Events backbuttondevicereadymenubuttonpauseresumeonlineofflinebatterycriticalbatterylow ...

  5. 生信基础概念之unique reads VS multi-mapping reads

    unique reads:在参考组上只有一个匹配点 multi-mapping reads:在参考组上有多个匹配点 下面是tophat的一个结果案例: Reads: Input : Mapped : ...

  6. 记录python面试题

    闲来无事,记录一下曾经以及深刻的面试题 记录一下我记忆比较深的面试题,以后若用到python相关还能细细把玩 搜狐面试题: 一.写一个缓存优化策略 解答:这个题主要考察对lru_cache的理解,所以 ...

  7. PHP实现链式操作

    什么是链式操作 我们经常会在一些应用框架中看到如下代码: $db = new Database; $db->where('cid = 9')->order('aid desc')-> ...

  8. 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减

    主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...

  9. FIND_IN_SET的简单使用

    FIND_IN_SET(str,strlist)函数 str 要查询的字符串 strlist 字段名 参数以”,”分隔 如 (1,2,6,8) 查询字段(strlist)中包含(str)的结果,返回结 ...

  10. Python日期时间函数

    所有日期.时间的api都在datetime模块内. 1. 日期输出格式化 datetime => string import datetime now = datetime.datetime.n ...