CF165D Beard Graph
$ \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}{输入样例}\)
3
1 2
2 3
7
3 1 2
3 1 3
3 2 3
2 2
3 1 2
3 1 3
3 2 3
6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 3
3 3 1
\(\color{#0066ff}{输出样例}\)
1
2
1
1
-1
-1
3
-1
3
2
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{题解}\)
只有两种颜色,维护两个LCT就行了
LCT上维护siz,询问时siz-1就是答案
无解就是不联通qwq
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e5 + 10;
struct LCT {
protected:
struct node {
node *ch[2], *fa;
int siz, rev;
node(int siz = 0, int rev = 0): siz(siz), rev(rev) {
fa = ch[0] = ch[1] = NULL;
}
void trn() { std::swap(ch[0], ch[1]), rev ^= 1; }
void dwn() {
if(!rev) return;
if(ch[0]) ch[0]->trn();
if(ch[1]) ch[1]->trn();
rev = 0;
}
void upd() { siz = (ch[0]? ch[0]->siz : 0) + (ch[1]? ch[1]->siz : 0) + 1; }
bool isr() { return fa->ch[1] == this; }
bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
}pool[maxn];
void rot(node *x) {
node *y = x->fa, *z = y->fa;
bool k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
(x->ch[!k] = y)->ch[k] = w;
(y->fa = x)->fa = z;
if(w) w->fa = y;
y->upd(), x->upd();
}
void splay(node *o) {
static node *st[maxn];
int top;
st[top = 1] = o;
while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
while(top) st[top--]->dwn();
while(o->ntr()) {
if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
rot(o);
}
}
void access(node *x) {
for(node *y = NULL; x; x = (y = x)->fa)
splay(x), x->ch[1] = y, x->upd();
}
void makeroot(node *x) { access(x), splay(x), x->trn(); }
node *findroot(node *x) {
access(x), splay(x);
while(x->dwn(), x->ch[0]) x = x->ch[0];
return x;
}
public:
void link(int l, int r) {
node *x = pool + l, *y = pool + r;
makeroot(x), x->fa = y;
}
void cut(int l, int r) {
node *x = pool + l, *y = pool + r;
makeroot(x), access(y), splay(y);
if(y->ch[0] == x) y->ch[0] = x->fa = NULL;
}
int query(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(x) != findroot(y)) return -1;
makeroot(x), access(y), splay(y);
return y->siz - 1;
}
}s[2];
std::pair<int, int> mp[maxn];
int col[maxn];
int main() {
int n = in();
for(int i = 1; i < n; i++) s[1].link(mp[i].first = in(), mp[i].second = in()), col[i] = 1;
int x, y, p;
for(int T = in(); T --> 0;) {
p = in();
if(p == 1) {
if(col[x = in()]) continue;
s[0].cut(mp[x].first, mp[x].second);
s[col[x] = 1].link(mp[x].first, mp[x].second);
}
if(p == 2) {
if(!col[x = in()]) continue;
s[1].cut(mp[x].first, mp[x].second);
s[col[x] = 0].link(mp[x].first, mp[x].second);
}
if(p == 3) x = in(), y = in(), printf("%d\n", s[1].query(x, y));
}
return 0;
}
CF165D Beard Graph的更多相关文章
- CF165D Beard Graph(dfs序+树状数组)
题面 题解 乍一看,单点修改,单链查询,用树链剖分维护每条链上白边的数量就完了, 还是--得写树链剖分吗?--3e5,乘两个log会T吗-- (双手颤抖) (纠结) 不!绝不写树链剖分! 这题如果能维 ...
- 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 ...
- 树链剖分【CF165D】Beard Graph
Description 给定一棵树,有m次操作. 1 x 把第x条边染成黑色 2 x 把第x条边染成白色 3 x y 查询x~y之间的黑边数,存在白边输出-1 Input 第1行为一个整数\(n\), ...
- 题解 CF165D 【Beard Graph】
思路:将黑边标记为1,白边标记为100000,树链剖分 如果查询时ans超过100000,那就有白边,输出-1,不然直接输出ans #include<bits/stdc++.h> #def ...
- codeforces 165D.Beard Graph 解题报告
题意: 给一棵树,树的每条边有一种颜色,黑色或白色,一开始所有边均为黑色,有两个操作: 操作1:将第i条边变成白色或将第i条边变成黑色. 操作2 :询问u,v两点之间仅经过黑色变的最短距离. 树链剖分 ...
- Codeforces Round #112 (Div. 2)
Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
随机推荐
- leetcode905
vector<int> sortArrayByParity(vector<int>& A) { vector<int> EVEN;//偶数 vector&l ...
- 调用DLL的2种方式
[调用DLL的2种方式] DLL在生成的时候会有dll.lib2个文件,另外包含相应的.h. 1.静态方式,通过lib来引用dll,以及引入.h. 2.只通过dll来使用,前提是知道内部的函数符号.
- 【原创】7. MYSQL++中的查询结果获取(各种Result类型)
在本节中,我将首先介绍MYSQL++中的查询的几个简单例子用法,然后看一下mysqlpp::Query中的几个与查询相关的方法原型(重点关注返回值),最后对几个关键类型进行解释. 1. MYSQL++ ...
- mysql免安装版1067错误终极解决办法|在windows平台下MySql启动时的1067错误的解决方法及反思
[windows事件查看] 我的电脑--此电脑--右键管理--计算机管理--系统工具--事件查看器--Windows日志--应用程序--找错误标志,如下图 [提示] 按部就班,可能并不能解决你的问题, ...
- Loadrunner Analyze
Analysis 对controller运行的结果进行分析 1.首先必须明确:光靠Analysis是不行的,只要能通过Analysis分析出部分问题就已经很不错了, 善于利用它才是最关键的. 2.如何 ...
- Quick Find
--------------------siwuxie095 Quick Find 这里介绍并查集的一种实现思路:Qui ...
- memcache 加载(对象)所遇到的问题。资源
<?php $mem =new memcache(); if($mem->connect('127.0.0.1','11211')){ echo '连接OK'.'<br>'; ...
- Windows Cmder
一.简介 作为一个程序员,即使是在windows工作环境,cmd也是我们必不可少的使用工具.cmder 是为 Windows 提供的一个便携式控制台仿真器,用来替代windows的cmd,使用非常简单 ...
- jQuery--全选、反选、取消
主要知识点: prop()--主要检查和设置checked attr()是针对所有属性,prop()是针对checked和selected等单一存在的,判断他们的true或者false. find() ...
- 3.Hive中查看数据来源文件和具体位置方法
虚拟列 -- 当 hive 产生了非预期的或 null 的时候,可以通过虚拟列进行诊断,判断哪行数据出现问题 INPUT__FILE__NAME (输入文件名)map任务读入File的全路径 ...