Description

You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are numbered from 1 to n.

Each node has a color, white or black. All the nodes are black initially.

We will ask you to perfrom some instructions of the following form:

  • u : ask for how many nodes are connected to u, two nodes are connected iff all the node on the path from u to v (inclusive u and v) have a same color.
  • u : toggle the color of u(that is, from black to white, or from white to black).
 

Input

The first line contains a number n denoted how many nodes in the tree(1 ≤ n ≤ 105). The next n - 1 lines, each line has two numbers (u,  v) describe a edge of the tree(1 ≤ u,  v ≤ n). The next line contains a number m denoted how many operations we are going to process(1 ≤ m ≤ 105). The next m lines, each line describe a operation (t,  u) as we mentioned above(0 ≤ t ≤ 1, 1 ≤ u ≤ n).

 

Output

 

For each query operation, output the corresponding result.

Sample Input

5
1 2
1 3
1 4
1 5
3
0 1
1 1
0 1

Sample Output

5
1

题解:

先将树定个根,然后建立黑白两棵lct

对于黑树,时刻满足如果有边u-v,则v一定是黑点(u不一定是黑点)

且对于黑树上的某个点,记录一下通过虚边和它相连的黑点有多少,lct上维护这条链的总和,白树同理

对于单点改色,假如它是黑点,要在黑树上把它和它父亲节点断开,然后再在白树上把它和它父亲节点连起来,白点同理

对于单点查询,假如它是黑点,把它access上去后要判断这条链最上面的那个点是不是黑点,不是就要输出最上面那个点的儿子节点,白点同理

code:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
const int maxm=;
int n,q,op,a,b,tot,now[maxn],son[maxm],pre[maxm],col[maxn],fa[maxn];
bool flag[maxn];
struct LCT{
int id,fa[maxn],son[maxn][],tot[maxn],sum[maxn];
void init(int x,int op){tot[x]+=op,sum[x]+=op;}
int which(int x){return son[fa[x]][]==x;}
bool isroot(int x){return son[fa[x]][]!=x&&son[fa[x]][]!=x;}
void update(int x){
sum[x]=tot[x];
if (son[x][]) sum[x]+=sum[son[x][]];
if (son[x][]) sum[x]+=sum[son[x][]];
}
void rotate(int x){
int y=fa[x],z=fa[y],d=which(x),dd=which(y);
fa[son[x][d^]]=y,son[y][d]=son[x][d^],fa[x]=fa[y];
if (!isroot(y)) son[z][dd]=x;
fa[y]=x,son[x][d^]=y,update(y),update(x);
}
void splay(int x){
while (!isroot(x)){
if (isroot(fa[x])) rotate(x);
else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
else rotate(x),rotate(x);
}
}
void access(int x){
for (int p=;x;x=fa[x]){
splay(x);
if (son[x][]) tot[x]+=sum[son[x][]];
if (p) tot[x]-=sum[p];
son[x][]=p,update(p=x);
}
}
void cut(int x,int y){
if (!y) return;
access(x),splay(x),fa[son[x][]]=,son[x][]=,update(x);
}
void link(int x,int y){
if (!y) return;
access(y),splay(y),splay(x),fa[x]=y,son[y][]=x,update(y);
}
int find_left(int x){for (access(x),splay(x);son[x][];x=son[x][]); return x;}
void query(int x){
int t=find_left(x); splay(t);
printf("%d\n",col[t]==id?sum[t]:sum[son[t][]]);
}
}T[];
void put(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b;}
void add(int a,int b){put(a,b),put(b,a);}
void dfs(int u){
for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if (v!=fa[u]) fa[v]=u,T[].link(v,u),dfs(v);
}
int main(){
read(n),T[].id=,T[].id=;
for (int i=;i<n;i++) read(a),read(b),add(a,b);
for (int i=;i<=n;i++) col[i]=;
for (int i=;i<=n;i++) T[].init(i,);
dfs();
for (read(q);q;q--){
read(op),read(a);
if (op) T[col[a]].cut(a,fa[a]),T[col[a]].init(a,-),col[a]^=,T[col[a]].init(a,),T[col[a]].link(a,fa[a]);
else T[col[a]].query(a);
}
return ;
}

bzoj3637: Query on a tree VI的更多相关文章

  1. BZOJ3637 Query on a tree VI(树链剖分+线段树)

    考虑对于每一个点维护子树内与其连通的点的信息.为了换色需要,记录每个点黑白两种情况下子树内连通块的大小. 查询时,找到深度最浅的同色祖先即可,这可以比较简单的树剖+线段树乱搞一下(似乎就是qtree3 ...

  2. bzoj 3637: Query on a tree VI 树链剖分 && AC600

    3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Sta ...

  3. QTREE6 - Query on a tree VI 解题报告

    QTREE6 - Query on a tree VI 题目描述 给你一棵\(n\)个点的树,编号\(1\)~\(n\).每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我 ...

  4. bzoj3637 CodeChef SPOJ - QTREE6 Query on a tree VI 题解

    题意: 一棵n个节点的树,节点有黑白两种颜色,初始均为白色.两种操作:1.更改一个节点的颜色;2.询问一个节点所处的颜色相同的联通块的大小. 思路: 1.每个节点记录仅考虑其子树时,假设其为黑色时所处 ...

  5. SPOJ QTREE Query on a tree VI

    You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...

  6. [BZOJ 3637]Query on a tree VI

    偶然看见了这题,觉得自己 QTREE.COT 什么的都没有刷过的真是弱爆了…… 一道思路很巧妙的题,终于是在约大爷的耐心教导下会了,真是太感谢约大爷了. 这题显然是树链剖分,但是链上维护的东西很恶心. ...

  7. QTREE6&&7 - Query on a tree VI &&VII

    树上连通块 不用具体距离,只询问连通块大小或者最大权值 可以类比Qtree5的方法,但是记录东西很多,例如子树有无0/1颜色等 一个trick,两个LCT分离颜色 每个颜色在边上. 仅保留连通块顶部不 ...

  8. 2019.02.17 spoj Query on a tree VI(链分治)

    传送门 题意简述:给你一棵nnn个黑白点的树,支持改一个点的颜色,询问跟某个点颜色相同的连通块大小. 思路: 还是链分治 233 记fi,0/1f_{i,0/1}fi,0/1​表示iii的所有颜色为0 ...

  9. [CodeChef-QTREE6]Query on a tree VI

    题目大意: 给你一棵黑白树,每个点默认是白色,要求支持以下两种操作: 1.改变一个点的颜色: 2.除去连接不同颜色的点的边,求某个点连通块的大小. 思路: 对原树维护两个树链剖分, 一棵维护当点x为白 ...

随机推荐

  1. win7限制登录时间的设置方法

    win7使用Net User命令行语句限制登录时间的方法: 1.单击“开始”,然后单击“运行”. 2.在“打开”框中,键入cmd,然后单击“确定”. 3..键入 net user username / ...

  2. php操作Memcache示例

    <?php //==============================实例化============================ $mem=new Memcache; //====== ...

  3. 简单的访客IP获取类-IPHelper.cs

    public class IPHelper { public static string GetVisitorsIPAddress() { string result = String.Empty; ...

  4. mxGraph实现按住ctrl键盘拖动图形实现复制图形功能

    实现这个功能很easy,仅仅须要重写moveCells方法就能够了.以下是源文件里的代码: mxGraph.prototype.moveCells = function(cells, dx, dy, ...

  5. JS浮点类型计算

    /* ---------------- JS浮点数运算重置 ---------------- */ //加法函数 //调用:accAdd(arg1,arg2) //返回值:arg1加上arg2的精确结 ...

  6. Qt 学习之路 2(79):QML 组件

    前面我们简单介绍了几种 QML 的基本元素.QML 可以由这些基本元素组合成一个复杂的元素,方便以后我们的重用.这种组合元素就被称为组件.组件就是一种可重用的元素.QML 提供了很多方法来创建组件.不 ...

  7. linux下MySQL安装登录及操作

    linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...

  8. Not enough free space on disks! linux

    Not enough free space on disks!100多G未分配空间也装不了Linux ------------------------------------------------- ...

  9. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  10. log4Net配置详解

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...