洛谷:P3950 部落冲突
原题地址:https://www.luogu.org/problemnew/show/P3950
题目简述
给定一棵树,每次给定一个操作,有如下两种:
- 将某条边染黑
2.询问给定的u,v两点间是否有边被染黑
思路
询问两点间是否有边被染黑只需要在求LCA时判一下就行。所以直接上树链剖分即可。
本题不需要使用线段树,使用树状数组查询路径上是否有任意一段区间和不为0即可。
代码
#include <bits/stdc++.h>
#define lowbit(x) x&-x
using namespace std;
typedef pair<int, int> P;
const int maxn = 1000000;
P war[maxn];
int fa[maxn], dep[maxn], val[maxn], sz[maxn], top[maxn], son[maxn];
int tre[maxn];
int tot;
int cntw;
int n, m;
inline int read()
{
int ch, x = 0, f = 1;ch = getchar();
while((ch < '0' || ch > '9') && ch != '-') ch = getchar();
ch == '-' ? f = -1, ch = getchar() : 0;
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
struct Edge {
int to, len, nxt;
Edge() {}
Edge(int _to, int _len, int _nxt):to(_to), len(_len), nxt(_nxt) {}
}E[maxn];
int h[maxn], cnte;
int L[maxn], R[maxn];
void update(int x, int add) {
for(int i = x;i <= maxn; i += lowbit(i)) tre[i] += add;
}
int query(int x) {
int ans = 0; for(int i = x; i; i -= lowbit(i)) ans += tre[i]; return ans;
}
inline void add_edge(int u, int v, int w) {
E[++cnte] = Edge(v, w, h[u]), h[u] = cnte;
E[++cnte] = Edge(u, w, h[v]), h[v] = cnte;
}
void dfs1(int x) {
sz[x] = 1; dep[x] = dep[fa[x]] + 1;
for(int i = h[x]; i; i = E[i].nxt) {
int to = E[i].to;
if(to == fa[x]) continue;
fa[to] = x;val[x] = E[i].len;
dfs1(to);
sz[x] += sz[to];
if(sz[to] > sz[son[x]]) son[x] = to;
}
}
void dfs2(int x) {
L[x] = ++tot;
if(x == son[fa[x]]) top[x] = top[fa[x]];
else top[x] = x;
if(son[x]) dfs2(son[x]);
for(int i = h[x]; i; i = E[i].nxt) {
int to = E[i].to;
if(to == fa[x] || to == son[x]) continue;
dfs2(to);
}
R[x] = tot;
}
void cut(int x, int y) {
if(L[x] < L[y]) swap(x, y);
update(L[x], 1);
}
void connect(int x, int y) {
if(L[x] < L[y]) swap(x, y);
update(L[x], -1);
}
int qsum(int x, int y) {//其实可以查到有1就退出,不用查完和
int ans = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]])swap(x, y);
ans += (query(L[x]) - query(L[top[x]] - 1));
x = fa[top[x]];
}
if(dep[x] < dep[y])swap(x, y);
if (x!=y)
ans += (query(L[x]) - query(L[y]));
return ans;
}
signed main() {
scanf("%d%d", &n, &m);
for(int i = 1; i < n; i++) add_edge(read(), read(), 0);
dfs1(1);
dfs2(1);
for(int i = 1;i <= m; i++) {
char s[50];
cin >> s;
if(s[0] == 'C') {
int u = read(), v = read();
cut(u, v);
war[++cntw] = P(u, v);
}
else if(s[0] == 'U') {
int w = read();
connect(war[w].first, war[w].second);
}
else {
if(qsum(read(), read()) != 0) puts("No");
else puts("Yes");
}
}
return 0;
}
洛谷:P3950 部落冲突的更多相关文章
- 洛谷 P3950 部落冲突 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...
- 洛谷P3950 部落冲突 [LCT]
题目传送门 部落冲突 格式难调,体面就不放了. 分析: julao们应该都看得出来就是个$LCT$板子,战争就$cut$,结束就$link$,询问就$find$.没了... 太久没打$LCT$,然后发 ...
- 洛谷P3950 部落冲突(LCT)
洛谷题目传送门 最无脑LCT题解,Dalao们的各种算法都比这个好多啦... 唯一的好处就是只管码代码就好了 开战cut,停战link,询问findroot判连通性 太无脑,应该不用打注释了.常数大就 ...
- 【刷题】洛谷 P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- [洛谷P3950]部落冲突
题目大意:给你一棵树,有$3$个操作: $Q\;p\;q:$询问$p,q$是否连通 $C\;p\;q:$把$p->q$这条边割断 $U\;x:$恢复第$x$次操作二 题解:可以在割断时把这条边赋 ...
- [题解] 洛谷P3950 部落冲突
传送门 拿到题目,一看 裸LCT (其实是我懒得打,splay又臭又长) 首先,这道题的意思就是删掉一些边 所以常规操作 点权转边权 之后对于战争操作,在对应的边上+1 对于和平操作,在对应的边上-1 ...
- Cogs 2856. [洛谷U14475]部落冲突
2856. [洛谷U14475]部落冲突 ★★★ 输入文件:lct.in 输出文件:lct.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个叫做Travi ...
- 洛谷 U14475 部落冲突 【比赛】 【树链剖分 + 线段树】
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
- lupgu P3950 部落冲突
题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...
随机推荐
- CF 551 E GukiZ and GukiZiana
https://codeforces.com/contest/551/problem/E 分块真强. 题意就是1.区间加,2.询问整个区间中,最远的两个x的距离. 分块,然后,每次找位子用二分找即可. ...
- 九度 题目1454:Piggy-Bank 完全背包
题目1454:Piggy-Bank 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1584 解决:742 题目描述: Before ACM can do anything, a budg ...
- Java中ElasticSearch的删除示例
public class DeleteElasticAPI { private static RestClient restClient; static { restClient=RestClient ...
- elk安装和使用
elk安装和使用 使用的版本都是5.2.0 elasticsearch-5.2.0安装 在官网 下载 elasticsearch tar包 解压安装 tar zxf elasticsearch-5.2 ...
- iOS -打包上传成功,在"构建版本"一直刷不出来
今天提交版本到appstore,构建版本一直不出来,等了一天也没有出来,其实就是权限问题,iOS13 来了,所以面临的问题随之而来,苹果给邮箱发了这段话: Dear Developer,We iden ...
- Java并发synchronized详解
今天和大家一起学习下并发编程,先举一个简单的生活例子,我们去医院或者银行排队叫号,那每个工作人员之间如何保证不会叫重号呢? public class TicketDemo extends Thread ...
- HTML表格列表简介
HTML简介: html列表: ol标签:order list 有序列表. ul标签:unorder list 无序列表. li标签:list item 列表项. style属性: 标签内写css表达 ...
- 【转】在Linux下搭建Git服务器
在 Linux 下搭建 Git 服务器 环境: 服务器 CentOS6.6 + git(version 1.7.1)客户端 Windows10 + git(version 2.8.4.windows. ...
- SpringCloud之Turbine
[前面的话]书接上文,本文的某些知识依赖我的上一篇SpringCLoud的文章:SpringCloud之Feign,如果没有看过可以先移步去看一下.前文提到了hystrix的应用,以及hystrix的 ...
- 2018年蓝桥杯java b组第六题
标题:递增三元组 给定三个整数数组A = [A1, A2, ... AN], B = [B1, B2, ... BN], C = [C1, C2, ... CN],请你统计有多少个三元组(i, j, ...