SDOJ 3740 Graph
8.9 t3
【描述】
给你一个图,一共有 N 个点,2*N-2 条有向边。
边目录按两部分给出
1、 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点
到达。
2、 接下来的 N-1 条边,一定是从 i 到 1(2<=i<=N)的有向边,保证每个点都能到
1
有 q 次询问:
1 x w :表示将第x条边的边权修改为w
2 u v :询问u到v的最短距离
【输入格式】
第一行是 2 个整数 N,Q,表示一共 N 个点 Q 次询问
接下来是 N-1 行,每行 3 个整数 U,V,W,表示了前 N-1 条边,u 到 v 的有向边
接下来 N-1 行,每行 3 个整数 U,V,W,描述了每个点到 1 号点的边,V==1
接下来是 Q 行,表示 Q 次修改与询问
【输出格式】
若干行,每行回答一次询问
【输入样例】
5 9
1 3 1
3 2 2
1 4 3
3 5 4
5 1 5
3 1 6
2 1 7
4 1 8
2 1 1
2 1 3
2 3 5
2 5 2
1 1 100
2 1 3
1 8 30
2 4 2
2 2 4
【输出样例】
0
1
4
8
100
132
10
【数据规模】
20%数据 没有修改
30%数据 2<=N,Q<=1000 (其中有 10%数据没有修改)
100%数据 2<=N,Q<=100 000, 1 <=边权 <= 1000,000
以下正解
对于询问(u,v),分为两种情况
每个点记录第一次的dfs序st[i], 子树结束的 ed[i],在对于第 2 种情况是 min{dist[i]}-dis[u] + dis[v]
其中 i 是 u 的子树,dis[u]表示从 1 u的值
对于修改(x,w),也分为两种情况
1.当边 u->v 修改为 w,则 st[v]...ed[v] 增加 w- w’,w’表示 u->v 原来的值
2.当边 u->1 修改为 w:则 st[u]..st[u]增加 w-w’
当然暴力修改区间值肯定是会挂掉的ovo 怎么办?
当然是线段树啦
等一下 还要提一提查错
写线段树的时候一定要冷静 之前把 ans=min(ans,query(ql,qr,lc));直接没写min的比较....
以后先查主函数的逻辑和调用有没有问题,不能一直纠缠改算法和数据结构
还要注意数据范围和long long 与 int之间的选择,不能全部用long long
记得开大数组范围!!!
记得开大数组范围!!!
记得开大数组范围!!!
贴一下代码~
#include<queue>
#include<iostream>
#include<cstdio>
#include<cmath>
#define N 400010
using namespace std;
#define LL long long
int st[N],ed[N],rev[N],idfn=;
LL dist[N];
int x[N],y[N],z[N];
int n,Q;
struct node
{
int u,v,w,nxt;
}e[N*];
int first[N],cnt;
void ade(int u,int v,int w)
{
e[++cnt].nxt=first[u];first[u]=cnt;
e[cnt].v=v;e[cnt].u=u;e[cnt].w=w;
}
//segment tree #define lc (p<<1)
#define rc (p<<1|1)
LL a[N],mn[N*];
struct Node
{
int l,r; LL lazy;
}T[N*];
void pushnow(LL p,LL v)
{
T[p].lazy+=v;
mn[p]+=v;
return;
}
void pushdown(LL p,LL m)
{
if(T[p].lazy)
{
T[lc].lazy+=T[p].lazy;
T[rc].lazy+=T[p].lazy;
mn[lc]+=T[p].lazy;
mn[rc]+=T[p].lazy;
T[p].lazy=;
}
}
void pushup(int p)
{
mn[p]=min(mn[lc],mn[rc]);
}
void build(int p,int l,int r)//建树
{
T[p].lazy=;
T[p].l=l;T[p].r=r;
if(l==r)
{
mn[p]=dist[l];
return;
}
int mid=(l+r)>>;
build(lc,l,mid);
build(rc,mid+,r);
pushup(p);
}
void update(int ql,int qr,int v,int p)//向上维护 L=ql
{
if(ql>qr) return;
if(ql<=T[p].l&&T[p].r<=qr)
{
pushnow(p,v);//!!!!!!
return;
}
int mid=(T[p].l+T[p].r)>>;
pushdown(p,T[p].r-T[p].l+);
if(ql<=mid)
update(ql,qr,v,lc);
if(qr>mid)
update(ql,qr,v,rc);
pushup(p);
}
LL query(int ql,int qr,int p)
{
if(ql<=T[p].l&&qr>=T[p].r) return mn[p];
int mid=(T[p].l+T[p].r)>>;
pushdown(p,T[p].r-T[p].l+);
LL ans=0x3f3f3f3f;
if(ql<=mid) ans=min(ans,query(ql,qr,lc));//!!!!
if(qr>mid) ans=min(ans,query(ql,qr,rc));//!!!!
//pushup(p);
return ans;
}
void getdfn(int rt,int fa,long long w)
{
st[rt]=++idfn;
dist[st[rt]]=w+rev[rt];//算dist[i]值
for(int i=first[rt];i;i=e[i].nxt)
{
if(e[i].v==fa) continue;
getdfn(e[i].v,rt, w+e[i].w);
}
ed[rt]=idfn;
}
//LCA
int h[N],dep[N],fa[N][];
void dfs(int u)
{
for(int i=;i<=;i++) fa[u][i]=fa[fa[u][i-]][i-];
for(int i=first[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v!=fa[u][])
{
fa[v][]=u;
h[v]=h[u]+;
dep[v]=dep[u]+;
dfs(v);
}
}
}
int lca(int u,int v)
{
if(h[u]<h[v]) return lca(v,u);
int i,d=h[u]-h[v];
for(i=;i<=;i++)
if((d>>i)&) u=fa[u][i]; if(u==v) return u;
for(i=;i>=;i--)
{
if(fa[u][i]!=fa[v][i])
{
u=fa[u][i];
v=fa[v][i];
}
}
return fa[u][];
}
int main()
{
scanf("%d%d",&n,&Q);
for(int i=;i<=*(n-);i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
if(i<=n-) ade(x[i],y[i],z[i]);
else rev[x[i]]=z[i];//cout<<2<<endl;
}//cout<<"x"<<endl;
getdfn(,,);
//cout<<"x"<<endl;
dfs();
build(,,idfn);
int op,a,b,u,v;
for(int i=;i<=Q;i++)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%d",&a,&b);
if(a>=n)
{
update(st[x[a]],st[x[a]],b-rev[x[a]],);
rev[x[a]] = b;
}
else
{
update(st[y[a]],ed[y[a]],b-z[a] ,);
z[a]=b;
}
}
if(op==)
{
scanf("%d%d",&u,&v);
if(lca(u,v)==u)
{
long long du=query(st[u],st[u],)-rev[u];
long long dv=query(st[v],st[v],)-rev[v];
printf("%lld\n", dv - du);
}
else
{
LL mnDist=query(st[u],ed[u],)-query(st[u],st[u],) + rev[u];
LL dv=query(st[v],st[v],)-rev[v];
printf("%lld\n",mnDist+dv);
}
}
}
return ;
}/*
5 9
1 3 1
3 2 2
1 4 3
3 5 4
5 1 5
3 1 6
2 1 7
4 1 8
2 1 1
2 1 3
2 3 5
2 5 2
1 1 100
2 1 3
1 8 30
2 4 2
2 2 4
*/
233333
SDOJ 3740 Graph的更多相关文章
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
随机推荐
- C#调用C++接口返回字符串的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 现在有这样一种情景,假如C#调用C++接口需要返回一个字符串.因为字符串是不定长的,因此传递一个定长的字符串进去是不合 ...
- vue搭建骨架屏步骤配置
1.什么是骨架屏幕? 在页面加载数据之前,有一段空白时间,要么用loading加载,要么就用骨架屏. 在开发webapp的时候总是会受到首屏加载时间过长的影响,主流的解决方法是在载入完成之前显示loa ...
- MVC的验证码
后台: /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="validateCode" ...
- Android(java)学习笔记115:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- 无旋Treap【模板】P3369
题目 详情见链接. 代码 #include<cstdio> #include<iostream> #define outd(x) printf("%d\n" ...
- kubernetes-深入理解pod对象(七)
Pod中如何管理多个容器 Pod中可以同时运行多个进程(作为容器运行)协同工作.同一个Pod中的容器会自动的分配到同一个 node 上.同一个Pod中的容器共享资源.网络环境和依赖,它们总是被同时调度 ...
- js中异步方案比较完整版(callback,promise,generator,async)
JS 异步已经告一段落了,这里来一波小总结 1. 回调函数(callback) setTimeout(() => { // callback 函数体 }, 1000) 缺点:回调地狱,不能用 t ...
- 关于lua 5.3 服务端热更新流程
脚本的热更新的流程都大同小异, 第一步先保存旧代码的块的数据, 第二部加载新的代码块,第三步将旧代码块的局部和全局数据拷贝到新代码块的对应的 变量中. 在服务器热更新中,主要考虑热更的内容是什么, 一 ...
- matplotlib绘图股票走势图实践
导入模块 import pandas as pdimport numpy as npfrom pandas import Series,DataFrameimport matplotlib.pyplo ...
- Java的WatchService文件夹监听遇到的一些问题
打开word文档时会新增一个~$开头的同名文件,关闭时该文件自动删除 修改excel文件时,会新增一个文件名像E56B4610,CBC15610等这样的文件,同时也会产生tmp格式的文件 PPT文件修 ...