当前位置: 首页 > news >正文

[洛谷P3950]部落冲突

题目大意:给你一棵树,有$3$个操作:

  1. $Q\;p\;q:$询问$p,q$是否连通
  2. $C\;p\;q:$把$p->q$这条边割断
  3. $U\;x:$恢复第$x$次操作二

题解:可以在割断时把这条边赋值上$1$,恢复时赋成$0$,只需要求$p->q$路径和是否为$0$即可,可以用$dfs$序+树状数组维护

卡点:$LCA$越界

 

C++ Code:

#include <cstdio>
#include <cctype>
#include <algorithm>
namespace __IO {
	namespace R {
		int x, ch;
		inline int read() {
			ch = getchar();
			while (isspace(ch)) ch = getchar();
			for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
			return x;
		}
	}
}
using __IO::R::read;

#define maxn 300010

int head[maxn], cnt;
struct Edge {
	int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {
	e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
}

int n, m;
#define M 20
int fa[maxn][M], sz[maxn], dep[maxn], dfn[maxn], idx;
void dfs(int u) {
	dfn[u] = ++idx;
	sz[u] = 1;
	for (int i = 1; i < M; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (v != *fa[u]) {
			*fa[v] = u;
			dep[v] = dep[u] + 1;
			dfs(v);
			sz[u] += sz[v];
		}
	}
}

inline int LCA(int x, int y) {
	if (x == y) return x;
	if (dep[x] < dep[y]) std::swap(x, y);
	for (int i = dep[x] - dep[y]; i; i &= i - 1) x = fa[x][__builtin_ctz(i)];
	if (x == y) return x;
	for (int i = M - 1; ~i; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
	return *fa[x];
}

namespace BIT {
	int Tr[maxn], res;
	inline void modify(int p, int num) {for (; p <= n; p += p & -p) Tr[p] += num;}
	inline int query(int p) {for (res = 0; p; p &= p - 1) res += Tr[p]; return res;}
}

inline void modify(int x, int num) {
	BIT::modify(dfn[x], num);
	BIT::modify(dfn[x] + sz[x], -num);
}
inline void query(int x, int y) {
	int res = BIT::query(dfn[x]) + BIT::query(dfn[y]) - BIT::query(dfn[LCA(x, y)]) * 2;
	puts(res ? "No" : "Yes");
}

int war[maxn], Tim;
int main() {
	n = read(), m = read();
	for (int i = 1, a, b; i < n; i++) {
		a = read(), b = read();
		add(a, b);
		add(b, a);
	}
	dfs(1);
	while (m --> 0) {
		int x, y;
		char op = getchar();
		while (!isalpha(op)) op = getchar();
		switch (op) {
			case 'Q':
				x = read(), y = read();
				query(x, y);
				break;
			case 'C':
				x = read(), y = read();
				if (dep[x] < dep[y]) std::swap(x, y);
				war[++Tim] = x;
				modify(x, 1);
				break;
			case 'U':
				x = read();
				modify(war[x], -1);
				break;
		}
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9997465.html

相关文章:

  • 技术工坊|高TPS和去中心化存储带来的第三代区块链技术革新机遇(深圳)
  • 深入Redis持久化
  • 【模板】最近公共祖先(LCA)
  • 端口的作用
  • Scrum立会报告+燃尽图(十一月二十二日总第三十次):加强回归测试
  • Java开发者福音 阿里巴巴宣布连任Java全球管理组织席位
  • FFmpeg(七)音频的播放
  • AI创业公司“一览群智”完成1.5亿元融资,经纬中国、策源资本投资
  • HTTP API 自动化测试从手工测试到平台的演变
  • JS 中的深拷贝与浅拷贝
  • 理解React Hooks
  • Django的Rbac介绍3
  • 毫秒级从百亿大表任意维度筛选数据,是怎么做到的...
  • 网上流传的那些关于链表的面试问题
  • JavaScript的使用你知道几种?(上)
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • Centos6.8 使用rpm安装mysql5.7
  • Fundebug计费标准解释:事件数是如何定义的?
  • HashMap剖析之内部结构
  • JavaScript新鲜事·第5期
  • JavaScript中的对象个人分享
  • linux安装openssl、swoole等扩展的具体步骤
  • mac修复ab及siege安装
  • ng6--错误信息小结(持续更新)
  • SOFAMosn配置模型
  • WebSocket使用
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 产品三维模型在线预览
  • 复杂数据处理
  • 排序(1):冒泡排序
  • 设计模式 开闭原则
  • 实习面试笔记
  • 王永庆:技术创新改变教育未来
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 整理一些计算机基础知识!
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (12)Linux 常见的三种进程状态
  • (23)Linux的软硬连接
  • (HAL库版)freeRTOS移植STMF103
  • (js)循环条件满足时终止循环
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .form文件_SSM框架文件上传篇
  • .gitignore文件—git忽略文件
  • .Net 8.0 新的变化
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .Net 知识杂记
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .Net中间语言BeforeFieldInit