【BZOJ】3319: 黑白树
http://www.lydsy.com/JudgeOnline/problem.php?id=3319
题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种:1、查询u到根的第一条黑边的编号。2、将u到v的路径全部染成黑色
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <set>
- #include <map>
- #include <sstream>
- using namespace std;
- typedef long long ll;
- #define pb push_back
- #define rep(i, n) for(int i=0; i<(n); ++i)
- #define for1(i,a,n) for(int i=(a);i<=(n);++i)
- #define for2(i,a,n) for(int i=(a);i<(n);++i)
- #define for3(i,a,n) for(int i=(a);i>=(n);--i)
- #define for4(i,a,n) for(int i=(a);i>(n);--i)
- #define CC(i,a) memset(i,a,sizeof(i))
- #define read(a) a=getint()
- #define print(a) printf("%d", a)
- #define dbg(x) cout << (#x) << " = " << (x) << endl
- #define error(x) (!(x)?puts("error"):0)
- #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
- 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; }
- const int N=1e6+10;
- int lca[N], fa[N], dep[N], id[N], n, del[N];
- struct Un {
- int f[N];
- void init() { for1(i, 1, n) f[i]=i; }
- int find(int x) { return x==f[x]?x:f[x]=find(f[x]); }
- void U(int x, int y) { x=find(x); y=find(y); if(dep[x]<dep[y]) swap(x, y); f[x]=y; }
- };
- struct Gr {
- int ihead[N], cnt;
- struct dat { int next, from, to, id; }e[N<<1];
- void add(int u, int v, int id) {
- e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].from=u; e[cnt].to=v; e[cnt].id=id;
- e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].from=v; e[cnt].to=u; e[cnt].id=id;
- }
- void add1(int u, int v) {
- e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
- }
- void tarjan(int x, Gr &g) {
- static bool vis[N];
- static Un p;
- p.f[x]=x;
- 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; }
- vis[x]=1;
- 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);
- }
- }G, ask, g;
- Un f, temp;
- void split(int u, int v, int goal, int now) {
- 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]));
- 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]));
- }
- void link(int u, int v, int goal, int now) {
- 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);
- }
- struct Q { int flag, u, v; }q[N];
- int m;
- int main() {
- read(n); read(m);
- for1(i, 1, n-1) G.add(getint(), getint(), i);
- for1(i, 1, m) {
- read(q[i].flag); read(q[i].v);
- if(q[i].flag==2) {
- read(q[i].u);
- ask.add(q[i].u, q[i].v, i);
- }
- }
- G.tarjan(1, ask);
- temp.init();
- f.init();
- for1(i, 1, m) if(q[i].flag==2) {
- split(q[i].u, q[i].v, lca[i], i);
- }
- // for1(i, 1, m) if(q[i].flag==2) {
- // printf("g[%d]:", i);
- // 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("");
- // }
- for1(i, 2, n) if(!del[i]) f.U(i, fa[i]);
- static int out[N], num=0;
- for3(i, m, 1) {
- 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; }
- else {
- link(q[i].u, q[i].v, lca[i], i);
- }
- }
- for3(i, num, 1) printf("%d\n", out[i]);
- return 0;
- }
好神的题辣....
很早以前写过链剖+线段树...果断tle...(【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树))
然后很早以前也看到claris大爷的题解,好神QAQ
他说是离线,那么我就顺着离线的思路想了下...
首先发现染色后对应着删边...表示边上的点的子女都不需要到这个点的祖先了...
然后发现每次查询的编号就是当前最近的祖先....
然后考虑如何删边...一开始我是直接开个并查集乱搞...可是跪了...其实只需要在已经删过的地方找到并查集的根然后向上走即可,注意要将删的边记录下来,否则待会无法合并
然后考虑如何合并...发现我们只需要将树的最终形态确定后,然后依次加入之前删过的边。那么也就是说,将询问离线后,从后向前,如果是询问就直接询问当前所在集合的根,否则合并之前在这个操作删掉的边
那么就行了QAQ
【BZOJ】3319: 黑白树的更多相关文章
- [BZOJ 3319] 黑白树
3319: 黑白树 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 557 Solved: 194[Submit][Status][Discuss] ...
- BZOJ 3319 黑白树 并查集+线段树
这这这这这这什么毒瘤题!!!!!!!!!!!!!!!!!!!!!!!!!!!! 卡LCT(优秀的LCT由于是均摊本身就带着2,3的常数在,而且这道题对于LCT标记十分难维护,又得乘上4,5然后就炸了) ...
- BZOJ 3319: 黑白树 并查集 + 离线 + 思维
Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作: 1.查询u到根路径上的第一条黑色边的标号. 2.将u到v 路径上的所有边的颜色设为黑色. Notice:这 ...
- BZOJ 3319: 黑白树 树+并查集+未调完+神题
Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char *p1,*p2,buf[10000 ...
- 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...
- uoj #139. 【UER #4】被删除的黑白树 dfs序 贪心
#139. [UER #4]被删除的黑白树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/139 Descript ...
- CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】
[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...
- BZOJ 2243 染色 | 树链剖分模板题进阶版
BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
随机推荐
- jquery优势
1.轻量 2.开源 3.选择器出色 可以支持几乎 css1到css3 的所有选择器 4.简单的修改页面 不同的浏览器对于css的支持程度是不同的,jquery通过封装javascript的代码, ...
- php中正则表达式的匹配和数据验证总结
正则表达式能匹配复杂的字符串形式,比字符串处理函数功能更加多,只不过执行效率有所降低,但是可以实现非常复杂的匹配,下面总结一下 1.简单的字符串匹配,判断指定字符串是不是在另一个字符串中,和字符串查找 ...
- canva实践小实例 —— 马赛克效果
前面给大家带来了操作像素的API,此时此刻,我觉得应该配以小实例来进行进一步的说明和演示,以便给大家带来更宽广的视野和灵感,你们看了我的那么多的文章,应该是懂我的风格,废话不多说,进入正题: 这次给大 ...
- 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 ...
- poj 2013 Symmetric Order 解题报告
题目链接:http://poj.org/problem?id=2013 设长度非递减的字串序列为s[1]...s[n].设计递归子程序print(n),其中n为字串序号,每分析1个字串,n=n-1. ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Timer&TimerTask原理分析
转载地址,请珍惜作者的劳动成果,转载请注明出处:http://www.open-open.com/lib/view/open1337176725619.html 如果你使用Java语言进行开发,对于定 ...
- ionic react-native和native开发移动app到底那个好
ionic react-native和native开发移动app那个好 ? 移动端开发如何选型?这里介绍一下我眼中的ionic,react-native,native 三种移动端开发选型对比.欢迎大家 ...
- OpenStack Swift集群部署流程与简单使用
之前介绍了<OpenStack Swift All In One安装部署流程与简单使用>,那么接下来就说一说Swift集群部署吧. 1. 简介 本文档详细描述了使用两台PC部署一个小型Sw ...
- mac 下修改Hosts文件
最近Google网站老是打不开,具体原因大家都明白,不过修改Hosts文件后,就能访问了,也算不上原创,网上一搜就能找到,自己操作记录下,希望有刚接触Mac 系统的童鞋有帮助. 第一步:打开Finde ...