$ \color{#0066ff}{ 题目描述 }$

给定一棵树,有m次操作。

1 x 把第x条边染成黑色

2 x 把第x条边染成白色

3 x y 查询x~y之间的黑边数,存在白边输出-1

\(\color{#0066ff}{输入格式}\)

第一行一个正整数N (1 ≤ N ≤ 100000),节点总数

接下来N − 1行,每行两个整数a,b 表示一条边

接下来是一个正整数m(1 ≤ m ≤ 300000),表示共有m次操作。

后面跟着m行,是操作

\(\color{#0066ff}{输出格式}\)

对于每一个询问,输出一行答案

\(\color{#0066ff}{输入样例}\)

  1. 3
  2. 1 2
  3. 2 3
  4. 7
  5. 3 1 2
  6. 3 1 3
  7. 3 2 3
  8. 2 2
  9. 3 1 2
  10. 3 1 3
  11. 3 2 3
  12. 6
  13. 1 5
  14. 6 4
  15. 2 3
  16. 3 5
  17. 5 6
  18. 6
  19. 3 3 4
  20. 2 5
  21. 3 2 6
  22. 3 1 2
  23. 2 3
  24. 3 3 1

\(\color{#0066ff}{输出样例}\)

  1. 1
  2. 2
  3. 1
  4. 1
  5. -1
  6. -1
  7. 3
  8. -1
  9. 3
  10. 2

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{题解}\)

只有两种颜色,维护两个LCT就行了

LCT上维护siz,询问时siz-1就是答案

无解就是不联通qwq

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. LL in() {
  4. char ch; LL x = 0, f = 1;
  5. while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
  6. for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
  7. return x * f;
  8. }
  9. const int maxn = 1e5 + 10;
  10. struct LCT {
  11. protected:
  12. struct node {
  13. node *ch[2], *fa;
  14. int siz, rev;
  15. node(int siz = 0, int rev = 0): siz(siz), rev(rev) {
  16. fa = ch[0] = ch[1] = NULL;
  17. }
  18. void trn() { std::swap(ch[0], ch[1]), rev ^= 1; }
  19. void dwn() {
  20. if(!rev) return;
  21. if(ch[0]) ch[0]->trn();
  22. if(ch[1]) ch[1]->trn();
  23. rev = 0;
  24. }
  25. void upd() { siz = (ch[0]? ch[0]->siz : 0) + (ch[1]? ch[1]->siz : 0) + 1; }
  26. bool isr() { return fa->ch[1] == this; }
  27. bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
  28. }pool[maxn];
  29. void rot(node *x) {
  30. node *y = x->fa, *z = y->fa;
  31. bool k = x->isr(); node *w = x->ch[!k];
  32. if(y->ntr()) z->ch[y->isr()] = x;
  33. (x->ch[!k] = y)->ch[k] = w;
  34. (y->fa = x)->fa = z;
  35. if(w) w->fa = y;
  36. y->upd(), x->upd();
  37. }
  38. void splay(node *o) {
  39. static node *st[maxn];
  40. int top;
  41. st[top = 1] = o;
  42. while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
  43. while(top) st[top--]->dwn();
  44. while(o->ntr()) {
  45. if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
  46. rot(o);
  47. }
  48. }
  49. void access(node *x) {
  50. for(node *y = NULL; x; x = (y = x)->fa)
  51. splay(x), x->ch[1] = y, x->upd();
  52. }
  53. void makeroot(node *x) { access(x), splay(x), x->trn(); }
  54. node *findroot(node *x) {
  55. access(x), splay(x);
  56. while(x->dwn(), x->ch[0]) x = x->ch[0];
  57. return x;
  58. }
  59. public:
  60. void link(int l, int r) {
  61. node *x = pool + l, *y = pool + r;
  62. makeroot(x), x->fa = y;
  63. }
  64. void cut(int l, int r) {
  65. node *x = pool + l, *y = pool + r;
  66. makeroot(x), access(y), splay(y);
  67. if(y->ch[0] == x) y->ch[0] = x->fa = NULL;
  68. }
  69. int query(int l, int r) {
  70. node *x = pool + l, *y = pool + r;
  71. if(findroot(x) != findroot(y)) return -1;
  72. makeroot(x), access(y), splay(y);
  73. return y->siz - 1;
  74. }
  75. }s[2];
  76. std::pair<int, int> mp[maxn];
  77. int col[maxn];
  78. int main() {
  79. int n = in();
  80. for(int i = 1; i < n; i++) s[1].link(mp[i].first = in(), mp[i].second = in()), col[i] = 1;
  81. int x, y, p;
  82. for(int T = in(); T --> 0;) {
  83. p = in();
  84. if(p == 1) {
  85. if(col[x = in()]) continue;
  86. s[0].cut(mp[x].first, mp[x].second);
  87. s[col[x] = 1].link(mp[x].first, mp[x].second);
  88. }
  89. if(p == 2) {
  90. if(!col[x = in()]) continue;
  91. s[1].cut(mp[x].first, mp[x].second);
  92. s[col[x] = 0].link(mp[x].first, mp[x].second);
  93. }
  94. if(p == 3) x = in(), y = in(), printf("%d\n", s[1].query(x, y));
  95. }
  96. return 0;
  97. }

CF165D Beard Graph的更多相关文章

  1. CF165D Beard Graph(dfs序+树状数组)

    题面 题解 乍一看,单点修改,单链查询,用树链剖分维护每条链上白边的数量就完了, 还是--得写树链剖分吗?--3e5,乘两个log会T吗-- (双手颤抖) (纠结) 不!绝不写树链剖分! 这题如果能维 ...

  2. Codeforces Round #112 (Div. 2) D. Beard Graph

    地址:http://codeforces.com/problemset/problem/165/D 题目: D. Beard Graph time limit per test 4 seconds m ...

  3. 树链剖分【CF165D】Beard Graph

    Description 给定一棵树,有m次操作. 1 x 把第x条边染成黑色 2 x 把第x条边染成白色 3 x y 查询x~y之间的黑边数,存在白边输出-1 Input 第1行为一个整数\(n\), ...

  4. 题解 CF165D 【Beard Graph】

    思路:将黑边标记为1,白边标记为100000,树链剖分 如果查询时ans超过100000,那就有白边,输出-1,不然直接输出ans #include<bits/stdc++.h> #def ...

  5. codeforces 165D.Beard Graph 解题报告

    题意: 给一棵树,树的每条边有一种颜色,黑色或白色,一开始所有边均为黑色,有两个操作: 操作1:将第i条边变成白色或将第i条边变成黑色. 操作2 :询问u,v两点之间仅经过黑色变的最短距离. 树链剖分 ...

  6. Codeforces Round #112 (Div. 2)

    Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...

  7. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  8. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  9. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

随机推荐

  1. leetcode766

    本题经过一下午的思考,终于解出来了.使用的是层次遍历的思想. class Solution { public: bool isToeplitzMatrix(vector<vector<in ...

  2. leetcode343

    public class Solution { public int IntegerBreak(int n) { ) { ; } ) { ; } var max = int.MinValue; ; i ...

  3. Java发送邮件Utils

    /** * 类文件说明 * */ public class SendMail { Logger log = Logger.getLogger(SendMail.class); /** * 发送邮件 * ...

  4. IDEA启动缓慢且运行卡顿

    最近在自己的机器上用IDEA时启动竟然要半分钟,且启动后索引操作居然还需要等待很久.并且每次通过IDEA执行JAVA项目在启动和关闭时都会发生卡顿.明明机器的配置不错,这是为啥呢? 这是因为为IDEA ...

  5. css一div内文字居中

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  6. Log4php使用指南

      一.Log4php简介 Log4php是Log4xx系列日志组件之一,是Log4j迁移到php的版本,主要用来记录日志信息,支持多种输入目的地,包括:日志文件.日志回滚文件.数据库.日志服务器等等 ...

  7. mybatis 框架 的应用之三(操作两张没有关联的表,存在主键和外键关系)

    #注意:要配置开启多条语句操作,否则会报错( org.apache.ibatis.exceptions.PersistenceException) lf-driver=com.mysql.jdbc.D ...

  8. 执行CUnit测试出错

    est/test_fifo.test: error while loading shared libraries: libcunit.so.1: cannot open shared object f ...

  9. web大文件上传控件-监控f_create流程-Xproer.HttpUploader6

    监控f_create流程 1.打开ie,f12 2.启动网络监控 点击开始捕获 上传文件,然后查看监控 将监控信息转到详细视图 向f_create提交的数据 f_create返回值

  10. 一张图看懂ASP.NET MVC5认证和授权过滤器的执行顺序

    IAuthenticationFilter是MVC5中的新特性,它有2个关键方法: OnAuthentication OnAuthenticationChallenge 当IAuthenticatio ...