luogu P3950 部落冲突
树剖板子题。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 3e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = ans * + ch - '', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
struct Edge
{
int to, nxt;
}e[maxn << ];
int head[maxn], ecnt = ;
void addEdge(int x, int y)
{
e[++ecnt].to = y;
e[ecnt].nxt = head[x];
head[x] = ecnt;
} int dep[maxn], fa[maxn], siz[maxn], son[maxn];
void dfs1(int now, int f)
{
siz[now] = ;
for(int i = head[now]; i; i = e[i].nxt)
{
if(e[i].to == f) continue;
dep[e[i].to] = dep[now] + ;
fa[e[i].to] = now;
dfs1(e[i].to, now);
siz[now] += siz[e[i].to];
if(!son[now] || siz[e[i].to] > siz[son[now]]) son[now] = e[i].to;
}
}
int top[maxn], dfsx[maxn], cnt = ;
void dfs2(int now, int f)
{
dfsx[now] = ++cnt;
if(son[now])
{
top[son[now]] = top[now];
dfs2(son[now], now);
}
for(int i = head[now]; i; i = e[i].nxt)
{
if(e[i].to == f || e[i].to == son[now]) continue;
top[e[i].to] = e[i].to;
dfs2(e[i].to, now);
}
} int l[maxn << ], r[maxn << ], sum[maxn << ];
void build(int L, int R, int now)
{
l[now] = L; r[now] = R;
if(L == R) return;
int mid = (L + R) >> ;
build(L, mid, now << );
build(mid + , R, now << | );
}
void update(int idx, int now)
{
if(l[now] == r[now]) {sum[now] ^= ; return;}
int mid = (l[now] + r[now]) >> ;
if(idx <= mid) update(idx, now << );
else update(idx, now << | );
sum[now] = sum[now << ] + sum[now << | ];
}
bool query(int L, int R, int now)
{
if(L == l[now] && R == r[now]) return sum[now];
int mid = (l[now] + r[now]) >> ;
if(R <= mid) return query(L, R, now << );
else if(L > mid) return query(L, R, now << | );
else return query(L, mid, now << ) | query(mid + , R, now << | );
} bool query_path(int x, int y)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
if(query(dfsx[top[x]], dfsx[x], )) return ;
x = fa[top[x]];
}
if(dfsx[x] > dfsx[y]) swap(x, y);
if(x == y) return ;
return query(dfsx[x] + , dfsx[y], ) ^ ;
} int war[maxn], wcnt = ;
char c[]; int main()
{
n = read(); m = read();
for(int i = ; i < n; ++i)
{
int x = read(), y = read();
addEdge(x, y); addEdge(y, x);
}
dfs1(, ); top[] = ; dfs2(, );
build(, cnt, );
for(int i = ; i <= m; ++i)
{
scanf("%s", c);
if(c[] == 'Q')
{
int x = read(), y = read();
printf("%s\n", query_path(x, y) ? "Yes" : "No");
}
else if(c[] == 'C')
{
int x = read(), y = read();
if(dfsx[x] < dfsx[y]) swap(x, y);
war[++wcnt] = x;
update(dfsx[x], );
}
else
{
int x = read();
update(dfsx[war[x]], );
}
}
return ;
}
luogu P3950 部落冲突的更多相关文章
- 【luogu P3950 部落冲突】 题解
题目连接:https://www.luogu.org/problemnew/show/P3950 1.像我这种学数据结构学傻了的 2.边权化点权 所有点权初始化0 3.对于战争 将深度较深的-1,对于 ...
- 【题解】Luogu P3950 部落冲突
原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我们用Link-Cut-Tree维护连通性(十分无脑) 一开始先把树中每条边的两端连接 U操作:把u,v两个点连起来 ...
- lupgu P3950 部落冲突
题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...
- 洛谷 P3950 部落冲突 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...
- 洛谷P3950 部落冲突 [LCT]
题目传送门 部落冲突 格式难调,体面就不放了. 分析: julao们应该都看得出来就是个$LCT$板子,战争就$cut$,结束就$link$,询问就$find$.没了... 太久没打$LCT$,然后发 ...
- luogu题解 P3950部落冲突--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...
- 【Luogu】P3950部落冲突(树链剖分)
题目链接 状态奇差无比,sbt都能错一遍. 不动笔光想没有想到怎么做,画图之后发现一个很明显的性质…… 那就是两个开战的部落,其中一个是另一个的父亲. 所以在儿子那里加个权值.查询的时候树链剖分查询链 ...
- 【刷题】洛谷 P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
随机推荐
- 记一次走心One 2 One沟通
聊的比较零零碎碎,内容比较散,有些solution不错,记一些要点吧(1)要学会汇报,就是坐你身边的人,也未必知道你在干啥 三个人都在砌墙.当人们分别问他们在做什么,他们的答案却不一样:第一个人头也没 ...
- MySQL重置root密码提示"Unknown column ‘password"的问题?
晚上打开MAC,发现root帐户突然不能正常登陆MySQL,于是打算重置密码,看了几篇文章,竟然重置不成功,总是得到Unknown column 'password'的错误,看了user的表结构也确实 ...
- 兼容ie6的ul水平居中对齐
---恢复内容开始--- margin可以为负数左移动. padding不可以. ---恢复内容结束---
- JAVA数据类型中的char类型
1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a'; //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=11 ...
- shodan在渗透测试中的应用
场景1:想搜索美国所有的elasticsearch服务器 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.设计 ...
- git必会必知
1 前言 git前身是BitKeeper,但是他不是开源软件,不符合当时开源趋势,于是就会有了开源的git,git开发只用了十天时间.目前git是公司开发必不可少的一个工具,用于多人开发的分布式版本控 ...
- mysql,简单介绍一下索引
汉字很多,人力有时尽,人不可能记住所有的字,为了解决这个问题,于是有了字典.数据库里的数据很多,为了方便检索,于是有了索引. 索引,是一种数据结构,在这种数据结构中实现了高级的查找算法,索引可以帮助我 ...
- VirtualBox使用Centos7与主机共享文件夹
最近使用VitrtualBox安装Centos7学习,liunx脚本和一些命令,经过一些研究完成了虚拟机与 主机共享文件夹,虚拟机链接外部网络,主机与虚拟机互相通信.在其中遇到一些我解决的技术问题记录 ...
- CF Dima and To-do List
B. Dima and To-do List time limit per test 1 second memory limit per test 256 megabytes input standa ...
- PowerDesigner设置所有int主键自增脚本
'*****************************************************************************dim model 'current mod ...