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的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
随机推荐
- [H5表单]一些html5表单知识及EventUtil对象完善
紧接着上面的文章,一开始准备一篇文章搞定,后来看到,要总结的东西还不少,干脆,把上面文章拆成两部分吧,这部分主要讲讲表单知识! 表单知识 1.Html5的autofocus属性. 有个这个属性,我们不 ...
- MYSQL连接字符串参数解析(解释)
被迫转到MySQL数据库,发现读取数据库时,tinyint类型的值都被转化为boolean了,这样大于1的值都丢失,变成true了.查阅资料MySQL中无Boolean类型,都是存储为tinyint了 ...
- [javaEE] EL表达式获取数据
jsp标签: <jsp:include> <jsp:forward> 实现请求转发 <jsp:param> 给上面的添加参数的 EL表达式: 1.获取变量数据 &l ...
- Spring_Spring与IoC_基于XML的DI
一.注入分类 bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化.初始化时由容器自动完成的,称为注入.根据注入方式的不同,常用的有2类:设值注入.构造注入.(还有一种,实现 ...
- SQL Join 语句
SQL Join 语句 SQL 中每一种连接操作都包括一个连接类型和连接条件. 连接类型 决定了如何处理连接条件不匹配的记录. 连接类型 返回结果 inner join 只包含左右表中满足连接条件的记 ...
- 修改vue的配置项支持生产环境下二级目录访问的方法
本文主要记录如何配置vue的打包文件配置项,使打包后的文件可以支持二级目录的访问. 1.常规打包 在实际的项目中,我们通常都使用 npm run build 直接打包文件后丢到服务器上访问 打包后的文 ...
- SVN认证失败的错误分析
作者:朱金灿 来源:http://blog.csdn.net/clever101 时常碰见SVN认证失败的问题,经过一番思考,可以总结出错误根源是:在SVN的数据库目录下有一个svnserve.con ...
- C++类继承--基类析构函数加上Virtual
下面的内容要说明两个问题:1. 基类的析构函数为什么要加上Virtual--防止内存泄露 1. 基类虚构函数无virtual,派生类无法析构,会导致内存泄露 #include <stdio.h& ...
- 08_Spring自定义标签
[ 项目工程 ] [ Person.java 模型类 ] package com.spring.selfxml.model; /** * Created by HigginCui on 2018/9/ ...
- Git学习-Git时光机之版本回退(二)
Git,是Linus花了两周时间用C写的一个分布式版本控制系统.牛人该怎么定义? 零.结论先行 倒叙总结一下: HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git ...