P3950部落冲突
题面
\(Solution:\)
法一:LCT裸题
又好想又好码,只不过常数太大。
法二:树链剖分
每次断边将该边权的值++,连边--,然后边权化点权(给儿子),询问就查询从x到y的路径上的边权和,树状数组套树链剖分维护.
\(Source\)
// luogu-judger-enable-o2
#include <stdio.h>
#include <vector>
#include <assert.h>
#include <ctype.h>
#include <set>
#include <cstring>
using namespace std;
namespace io {
char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
return x * f;
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 0) : 1; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 0) : 1; }
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define ____ debug("go")
#define rep(i, a, b) for (register int i = a; i <= b; ++ i)
#define dep(i, a, b) for (register int i = a; i >= b; -- i)
#define travel(i, u) for (register int i = head[u]; i; i = nxt[i])
#define mem(a, b) memset(a, b, sizeof a)
}
using io::rint;using io::chkmin;using io::chkmax;
const int N = 4e5 + 1;
int n, m;
int tot, ver[N<<1], head[N], nxt[N<<1];
void add(int u, int v)
{ ver[++tot] = v, nxt[tot] = head[u], head[u] = tot; }
int seg[N], size[N], top[N], son[N], cnt, fa[N], dep[N];
void DFS1(int x, int f) {
fa[x] = f;
size[x] = 1;
dep[x] = dep[f] + 1;
travel(i, x) {
if (ver[i] == f) continue;
DFS1(ver[i], x);
size[x] += size[ver[i]];
if (size[son[x]] < size[ver[i]]) son[x] = ver[i];
}
}
void DFS2(int u, int topf) {
seg[u] = ++cnt;
top[u] = topf;
if (son[u]) DFS2(son[u], topf);
else return;
travel(i, u) {
if (ver[i] != fa[u] && ver[i] != son[u]) DFS2(ver[i], ver[i]);
}
}
struct BIT {
int s[N];
void insert(int x, int c)
{ for (int i = x; i <= n; i += (i & -i)) s[i] += c; }
int query(int x)
{ int res = 0; for (int i = x; i; i -= (i & -i)) res += s[i]; return res; }
} T;
vector<pair<int, int> > War;
int Query(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);//打挂的地方
ans += T.query(seg[x]) - T.query(seg[top[x]] - 1);
x = fa[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
ans += T.query(seg[x]) - T.query(seg[y]);
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("P3950.in", "r", stdin);
freopen("P3950.out", "w", stdout);
#endif
n = rint(); m = rint();
for (int i = 1; i < n; ++ i) {
int u = rint(), v = rint();
add(u, v); add(v, u);
}
DFS1(1, 0);
DFS2(1, 1);
char op[10]; int x, y;
while (m --) {
scanf("%s", op);
if (op[0] == 'Q') {
x = rint(), y = rint();
if (Query(x, y)) puts("No");
else puts("Yes");
} else if (op[0] == 'U') {
x = rint();
x --;
int u = War[x].first, v = War[x].second;
if (seg[u] < seg[v]) swap(u, v);
T.insert(seg[u], -1);
} else {
x = rint(), y = rint();
if (seg[x] < seg[y]) swap(x, y);
T.insert(seg[x], 1);
War.push_back(make_pair(x, y));
}
}
}
P3950部落冲突的更多相关文章
- 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$,然后发 ...
- 【刷题】洛谷 P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- 【luogu P3950 部落冲突】 题解
题目连接:https://www.luogu.org/problemnew/show/P3950 1.像我这种学数据结构学傻了的 2.边权化点权 所有点权初始化0 3.对于战争 将深度较深的-1,对于 ...
- 【Luogu】P3950部落冲突(树链剖分)
题目链接 状态奇差无比,sbt都能错一遍. 不动笔光想没有想到怎么做,画图之后发现一个很明显的性质…… 那就是两个开战的部落,其中一个是另一个的父亲. 所以在儿子那里加个权值.查询的时候树链剖分查询链 ...
- 洛谷:P3950 部落冲突
原题地址:https://www.luogu.org/problemnew/show/P3950 题目简述 给定一棵树,每次给定一个操作,有如下两种: 将某条边染黑 2.询问给定的u,v两点间是否有边 ...
- luogu题解 P3950部落冲突--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...
随机推荐
- 【洛谷P3834】(模板)可持久化线段树 1(主席树)
[模板]可持久化线段树 1(主席树) https://www.luogu.org/problemnew/show/P3834 主席树支持历史查询,空间复杂度为O(nlogn),需要动态开点 本题用一个 ...
- 【luogu P3469 [POI2008]BLO-Blockade】 题解
题目链接:https://www.luogu.org/problemnew/show/P3469 #include <cstdio> #include <cstring> #i ...
- 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解
题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...
- SQL limit
employee 表 id name gender hire_date salary performance manage deparmant 1001 张三 男 2/12/1991 00:00:00 ...
- Python使用dict和set
dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也成为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的 ...
- 使用两个嵌套的for循坏探测2-100的所有素数
只能被1和本身整除的整数才叫做素数 public class prime { public static void main(String[] args) { ; i <= ; i++) { ; ...
- 史上最简单的 SpringCloud 教程 | 第十四篇: 服务注册(consul)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc14-consul/ 本文出自方志朋的博客 这篇文章主要介绍 s ...
- plupload批量上传分片(后台代码)
plupload批量上传分片功能, 对于文件比较大的情况下,plupload支持分片上传,后台代码如下: /** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本 ...
- MySQL版本详解
一.版本说明 1.1.MySQL相关连接 MySQL官网:https://www.mysql.com/ MySQL下载:https://dev.mysql.com/downloads/mirrors/ ...
- 廖老师的Python教程——安装Python
因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...