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的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
随机推荐
- out参数
out参数: 参数在方法的内部必须为其赋值:可以同时返回不同类型的值: 在Main方法里定义,在方法里赋值: 输 ...
- css样式margin padding border
- 在线HTML文档编辑器使用入门之图片上传与图片管理的实现
在线HTML文档编辑器使用入门之图片上传与图片管理的实现: 官方网址: http://kindeditor.net/demo.php 开发步骤: 1.开发中只需要导入选中的文件(通常在 webapp ...
- Javaweb之EL表达式
1.EL表达式简介 EL全名为Expression Language.EL的主要作用为: 获取数据:EL表达式主要用于替换jsp页面中的脚本表达式,以从各种类型的web域中检索java对象,获取数据. ...
- poj 3260 最少硬币(01+多重+完全背包)
http://www.cnblogs.com/ACMan/archive/2012/08/14/2637437.html #include <iostream> #include < ...
- jQuery源码分析系列 : 整体架构
query这么多年了分析都写烂了,老早以前就拜读过, 不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍 我也不会照本宣科的翻译源码,结合自己的实际经验一起拜读吧! ...
- <Android 基础(二十)> CoordinatorLayout Behavior
介绍 Interaction behavior plugin for child views of {@link CoordinatorLayout}. A Behavior implements o ...
- C++基础--回调的应用
一.类成员函数的回调 1. 类成员函数的回调,函数的调用必须通过类来调用: CallBack.h #pragma once class CallBack { public: CallBack(); ~ ...
- How to Build MySQL from Source Code on Windows & compile MySQL on win7+vs2010
Not counting obtaining the source code, and once you have the prerequisites satisfied, [Windows] use ...
- Android Studio修改app图标
1.将下载好的图片放到app\src\main\res\drawable目录下 2.在AndroidManifest.xml下加入一句 android:icon="@drawable/??? ...