洛谷: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 ...
随机推荐
- Jira更改工作流后,敏捷看板里无法显示sprint对应的问题列表
转自:http://blog.csdn.net/computerheart/article/details/68924295 Jira更改工作流后,敏捷看板里无法显示sprint对应的问题列表 原创 ...
- POJ 1002 487-3279 map
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 287874 Accepted: 51669 Descr ...
- Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation
题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- codeforces 807 E. Prairie Partition(贪心+思维)
题目链接:http://codeforces.com/contest/807/problem/E 题意:已知每个数都能用x=1 + 2 + 4 + ... + 2k - 1 + r (k ≥ 0, 0 ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- java注解使用总结
2005年,sun公司推出了jdk1.5,同时推出的注解功能吸引了很多人的目光,使用注解编写代码,能够减轻java程序员繁琐配置的痛苦. 使用注解可以编写出更加易于维护,bug更少的代码. 注解是什么 ...
- 【Mac】快速复制文件路径
一.使用Automator新建服务 二.配置 三.使用 四.创建快捷键 一.使用Automator新建服务 在应用程序文件夹里打开Automator,选择文件菜单,新建一个服务,如下  二.配置 在 ...
- springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
- 2019年全国新课标I卷文理科数学LaTeX排版试题与解析
整体分析,没有偏怪难题之分,中等题偏多,题目较往年有题型改动变化,但难度还称不上很难.具体内容贴上链接! https://mp.weixin.qq.com/s/WKXhCKI_-z3UT-zUwI23 ...