Tree
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 6131   Accepted: 1682

Description

You are given a tree with N nodes. The tree’s nodes are numbered 1 through
N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i v Change the weight of the ith edge to v
NEGATE a b Negate the weight of every edge on the path from a to b
QUERY a b Find the maximum weight of edges on the path from a to b

Input

The input contains multiple test cases. The first line of input contains an integer
t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains
N (N ≤ 10,000). The next N − 1 lines each contains three integers
a, b and c, describing an edge connecting nodes a and
b
with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Sample Output

1
3

Source

题目大意:一棵树query查询a到b最大值,change 改动第a条边权值变成b,NEGATE a到b的全部权值变为相反数

ac代码

Problem: 3237		User: kxh1995
Memory: 3096K Time: 657MS
Language: C++ Result: Accepted
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define INF 0x7fffffff
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
using namespace std;
int vis[100500];
struct LCT
{
int bef[100050],pre[100050],next[100050][2],key[100050],val[100050],belong[100010],maxn[100010],minn[100010],neg[100010];
void init()
{
memset(pre,0,sizeof(pre));
memset(next,0,sizeof(next));
//memset(key,0,sizeof(key));
memset(neg,0,sizeof(neg));
maxn[0]=-INF;
minn[0]=INF;
}
void pushup(int x)
{
maxn[x]=key[x];
minn[x]=key[x];
if(next[x][0])
{
maxn[x]=max(maxn[x],maxn[next[x][0]]);
minn[x]=min(minn[x],minn[next[x][0]]);
}
if(next[x][1])
{
maxn[x]=max(maxn[x],maxn[next[x][1]]);
minn[x]=min(minn[x],minn[next[x][1]]);
}
}
void Not(int x)
{
if(x)
{
neg[x]^=1;
swap(minn[x],maxn[x]);
key[x]=-key[x];
minn[x]=-minn[x];
maxn[x]=-maxn[x];
}
}
void pushdown(int x)
{
if(neg[x])
{
Not(next[x][0]);
Not(next[x][1]);
neg[x]=0;
}
}
void rotate(int x,int kind)
{
int y,z;
y=pre[x];
z=pre[y];
pushdown(y);
pushdown(x);
next[y][!kind]=next[x][kind];
pre[next[x][kind]]=y;
next[z][next[z][1]==y]=x;
pre[x]=z;
next[x][kind]=y;
pre[y]=x;
pushup(y);
}
void splay(int x)
{
int rt;
for(rt=x;pre[rt];rt=pre[rt]);
if(x!=rt)
{
bef[x]=bef[rt];
bef[rt]=0;
pushdown(x);
while(pre[x])
{
if(next[pre[x]][0]==x)
{
rotate(x,1);
}
else
rotate(x,0);
}
pushup(x);
}
}
void access(int x)
{
int fa;
for(fa=0;x;x=bef[x])
{
splay(x);
pushdown(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=fa;
pre[fa]=x;
bef[fa]=0;
fa=x;
pushup(x);
}
}
void change(int x,int y)
{
int t=belong[x-1];
splay(t);
key[t]=y;
}
int negate(int x,int y)
{
access(y);
for(y=0;x;x=bef[x])
{
splay(x);
if(!bef[x])
{
Not(y);
Not(next[x][1]);
return 1;
}
pushdown(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=y;
pre[y]=x;
bef[y]=0;
y=x;
pushup(x);
}
return 0;
}
int query(int x,int y)
{
access(y);
for(y=0;x;x=bef[x])
{
splay(x);
if(!bef[x])
{
return max(maxn[y],maxn[next[x][1]]);
}
pushdown(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=y;
pre[y]=x;
bef[y]=0;
y=x;
pushup(x);
}
return 0;
}
}lct;
int head[100010],cnt;
struct s
{
int u,v,w,next;
}edge[100010<<1];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[u]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
lct.bef[v]=u;
lct.key[v]=edge[i].w;
lct.belong[i>>1]=v;
vis[v]=1;
q.push(v);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(head,-1,sizeof(head));
cnt=0;
for(int i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
lct.init();
bfs(1);
char str[10];
while(scanf("%s",str)!=EOF)
{
if(str[0]=='D')
break;
int a,b;
scanf("%d%d",&a,&b);
if(str[0]=='Q')
{
printf("%d\n",lct.query(a,b));
}
else
if(str[0]=='C')
lct.change(a,b);
else
lct.negate(a,b);
}
}
}

POJ 题目3237 Tree(Link Cut Tree边权变相反数,求两点最大值)的更多相关文章

  1. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  2. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  7. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  8. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  9. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  10. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

随机推荐

  1. 派生类地址比基类地址少4(CDerived对象的起始地址存放的是虚表指针vptr,也就是子类的第一项内容。接下来的是基类的成员变量,接下来再是自身的成员变量)

    大家对虚表并不陌生,都知道每个含有虚函数的类对象都有1个虚指针,但是在现实使用中,却总是因为这而调试半天,才发现原来是虚指针惹的祸.我这几天在调试代码时候也中招了,我的问题是这样的,如下图,CTree ...

  2. 操作系统-容器-引擎容器-百科:Docker

    ylbtech-操作系统-容器-引擎容器-百科:Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上 ...

  3. Windows下Word.exe在哪?

    在这里: C:\Program Files\Microsoft Office\root\Office16

  4. leetcode 系列文章目录

    leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...

  5. 【转】Docker基础

    一.简介 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的CGroup.Namespace.Union FS等技术实现的一种系统级虚拟化技术. 特性 更高效的利用系统资源: ...

  6. Vue 菜单栏点击实现高亮显示

    步骤: 遍历对象(goods)获取菜单栏每一项的对象(item)和下标(index) 添加点击事件toggle(),传入下标参数:@click="fn1();fn2()" 动态切换 ...

  7. 初学C#,总结一下.sln和.csproj的区别

    1.sln:solusion 解决方案 csproj:c sharp project C#项目 csproj文件大家应该不会陌生,那就是C#项目文件的扩展名,它是“C Sharp Project”的缩 ...

  8. WCF(一)控制台寄宿

    WCF是微软开发的一款通信框架.具有跨平台跨操作系统的特点,所以,WCF一般用于开发第三方接口或者在分布式系统用做数据交互. WCF三要素分别是地址(Address).绑定(Binding).契约(C ...

  9. Activiti Modeler 5.22.0整合到Spring项目

    转载 https://blog.csdn.net/u010411264/article/details/71480354

  10. SQL基本语句:1.模式 3.索引

    每次很长时间不用sql语句之后,都需要把基础的捡一捡,索性做个笔记,以后可以长看