http://www.lydsy.com/JudgeOnline/problem.php?id=3319

题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种:1、查询u到根的第一条黑边的编号。2、将u到v的路径全部染成黑色

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. #include <sstream>
  11. using namespace std;
  12. typedef long long ll;
  13. #define pb push_back
  14. #define rep(i, n) for(int i=0; i<(n); ++i)
  15. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  16. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  17. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  18. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  19. #define CC(i,a) memset(i,a,sizeof(i))
  20. #define read(a) a=getint()
  21. #define print(a) printf("%d", a)
  22. #define dbg(x) cout << (#x) << " = " << (x) << endl
  23. #define error(x) (!(x)?puts("error"):0)
  24. #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
  25. inline int getint() { static int r, k; r=0,k=1; static char c; c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  26.  
  27. const int N=1e6+10;
  28.  
  29. int lca[N], fa[N], dep[N], id[N], n, del[N];
  30. struct Un {
  31. int f[N];
  32. void init() { for1(i, 1, n) f[i]=i; }
  33. int find(int x) { return x==f[x]?x:f[x]=find(f[x]); }
  34. void U(int x, int y) { x=find(x); y=find(y); if(dep[x]<dep[y]) swap(x, y); f[x]=y; }
  35. };
  36. struct Gr {
  37. int ihead[N], cnt;
  38. struct dat { int next, from, to, id; }e[N<<1];
  39. void add(int u, int v, int id) {
  40. e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].from=u; e[cnt].to=v; e[cnt].id=id;
  41. e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].from=v; e[cnt].to=u; e[cnt].id=id;
  42. }
  43. void add1(int u, int v) {
  44. e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
  45. }
  46. void tarjan(int x, Gr &g) {
  47. static bool vis[N];
  48. static Un p;
  49. p.f[x]=x;
  50. rdm(x, i) if(e[i].to!=fa[x]) { dep[e[i].to]=dep[x]+1; fa[e[i].to]=x; id[e[i].to]=i; tarjan(e[i].to, g); p.f[e[i].to]=x; }
  51. vis[x]=1;
  52. for(int i=g.ihead[x]; i; i=g.e[i].next) if(vis[g.e[i].to]) lca[g.e[i].id]=p.find(g.e[i].to);
  53. }
  54. }G, ask, g;
  55.  
  56. Un f, temp;
  57.  
  58. void split(int u, int v, int goal, int now) {
  59. for(u=temp.find(u); dep[u]>dep[goal]; del[u]=1, temp.U(u, fa[u]), g.add1(now, id[u]), u=temp.find(fa[u]));
  60. for(v=temp.find(v); dep[v]>dep[goal]; del[v]=1, temp.U(v, fa[v]), g.add1(now, id[v]), v=temp.find(fa[v]));
  61. }
  62. void link(int u, int v, int goal, int now) {
  63. for(int i=g.ihead[now]; i; i=g.e[i].next) f.U(G.e[g.e[i].to].from, G.e[g.e[i].to].to);
  64. }
  65. struct Q { int flag, u, v; }q[N];
  66. int m;
  67. int main() {
  68. read(n); read(m);
  69. for1(i, 1, n-1) G.add(getint(), getint(), i);
  70.  
  71. for1(i, 1, m) {
  72. read(q[i].flag); read(q[i].v);
  73. if(q[i].flag==2) {
  74. read(q[i].u);
  75. ask.add(q[i].u, q[i].v, i);
  76. }
  77. }
  78. G.tarjan(1, ask);
  79. temp.init();
  80. f.init();
  81.  
  82. for1(i, 1, m) if(q[i].flag==2) {
  83. split(q[i].u, q[i].v, lca[i], i);
  84. }
  85. // for1(i, 1, m) if(q[i].flag==2) {
  86. // printf("g[%d]:", i);
  87. // for(int j=g.ihead[i]; j; j=g.e[j].next) printf(" (%d<->%d), ", G.e[g.e[j].to].from, G.e[g.e[j].to].to); puts("");
  88. // }
  89. for1(i, 2, n) if(!del[i]) f.U(i, fa[i]);
  90. static int out[N], num=0;
  91. for3(i, m, 1) {
  92. if(q[i].flag==1) { int ans=f.find(q[i].v); if(ans==1) out[++num]=0; else out[++num]=G.e[id[ans]].id; }
  93. else {
  94. link(q[i].u, q[i].v, lca[i], i);
  95. }
  96. }
  97. for3(i, num, 1) printf("%d\n", out[i]);
  98. return 0;
  99. }

  


好神的题辣....

很早以前写过链剖+线段树...果断tle...(【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)

然后很早以前也看到claris大爷的题解,好神QAQ

他说是离线,那么我就顺着离线的思路想了下...

首先发现染色后对应着删边...表示边上的点的子女都不需要到这个点的祖先了...

然后发现每次查询的编号就是当前最近的祖先....

然后考虑如何删边...一开始我是直接开个并查集乱搞...可是跪了...其实只需要在已经删过的地方找到并查集的根然后向上走即可,注意要将删的边记录下来,否则待会无法合并

然后考虑如何合并...发现我们只需要将树的最终形态确定后,然后依次加入之前删过的边。那么也就是说,将询问离线后,从后向前,如果是询问就直接询问当前所在集合的根,否则合并之前在这个操作删掉的边

那么就行了QAQ

【BZOJ】3319: 黑白树的更多相关文章

  1. [BZOJ 3319] 黑白树

    3319: 黑白树 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 557  Solved: 194[Submit][Status][Discuss] ...

  2. BZOJ 3319 黑白树 并查集+线段树

    这这这这这这什么毒瘤题!!!!!!!!!!!!!!!!!!!!!!!!!!!! 卡LCT(优秀的LCT由于是均摊本身就带着2,3的常数在,而且这道题对于LCT标记十分难维护,又得乘上4,5然后就炸了) ...

  3. BZOJ 3319: 黑白树 并查集 + 离线 + 思维

    Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作: 1.查询u到根路径上的第一条黑色边的标号. 2.将u到v    路径上的所有边的颜色设为黑色. Notice:这 ...

  4. BZOJ 3319: 黑白树 树+并查集+未调完+神题

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char *p1,*p2,buf[10000 ...

  5. 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...

  6. uoj #139. 【UER #4】被删除的黑白树 dfs序 贪心

    #139. [UER #4]被删除的黑白树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/139 Descript ...

  7. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  8. BZOJ 2243 染色 | 树链剖分模板题进阶版

    BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...

  9. BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)

    BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...

随机推荐

  1. jquery优势

    1.轻量 2.开源 3.选择器出色 可以支持几乎 css1到css3 的所有选择器 4.简单的修改页面    不同的浏览器对于css的支持程度是不同的,jquery通过封装javascript的代码, ...

  2. php中正则表达式的匹配和数据验证总结

    正则表达式能匹配复杂的字符串形式,比字符串处理函数功能更加多,只不过执行效率有所降低,但是可以实现非常复杂的匹配,下面总结一下 1.简单的字符串匹配,判断指定字符串是不是在另一个字符串中,和字符串查找 ...

  3. canva实践小实例 —— 马赛克效果

    前面给大家带来了操作像素的API,此时此刻,我觉得应该配以小实例来进行进一步的说明和演示,以便给大家带来更宽广的视野和灵感,你们看了我的那么多的文章,应该是懂我的风格,废话不多说,进入正题: 这次给大 ...

  4. Java for LeetCode 160 Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. poj 2013 Symmetric Order 解题报告

    题目链接:http://poj.org/problem?id=2013 设长度非递减的字串序列为s[1]...s[n].设计递归子程序print(n),其中n为字串序号,每分析1个字串,n=n-1. ...

  6. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  7. Timer&TimerTask原理分析

    转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...

  8. ionic react-native和native开发移动app到底那个好

    ionic react-native和native开发移动app那个好 ? 移动端开发如何选型?这里介绍一下我眼中的ionic,react-native,native 三种移动端开发选型对比.欢迎大家 ...

  9. OpenStack Swift集群部署流程与简单使用

    之前介绍了<OpenStack Swift All In One安装部署流程与简单使用>,那么接下来就说一说Swift集群部署吧. 1. 简介 本文档详细描述了使用两台PC部署一个小型Sw ...

  10. mac 下修改Hosts文件

    最近Google网站老是打不开,具体原因大家都明白,不过修改Hosts文件后,就能访问了,也算不上原创,网上一搜就能找到,自己操作记录下,希望有刚接触Mac 系统的童鞋有帮助. 第一步:打开Finde ...