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

 
----------------------------------------------------------------------------------------
 
这个题首先一看,20%没有修改.....大概可以写个LCA?
30% Q<=1000.......大概可以打个暴力?
 

以下正解

对于询问(u,v),分为两种情况
1.u为v的祖先,所以dis=dis[u]-dis[v];
2.u不是v的祖先 则 u 先到达 1 号点再到达 v 点。此时 u 必须先找到回到 1 的最短路(一定是自身直接回到 1 或通过以 u 为根子树的某点回到 1 )。且1号点到v点的路径唯一。
 
我们先做dfs序 然后对每个点维护一个dist[i],dist[i]=根到节点距离+节点返回根距离

每个点记录第一次的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的更多相关文章

  1. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  2. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. [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), ...

  5. [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), ...

  6. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  9. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

随机推荐

  1. {Linux} boot仅剩余XX字节

    1. 查看已安装的linux-image各版本 dpkg --get-selections |grep linux-image   2. 查看我们当前使用的是哪一个版本: uname -a    3. ...

  2. Python+selenium之键盘事件

    keuys()类提供键盘上所有的按键方法.send_keys()方法可以用来模拟键盘输入. from selenium import webdriver from selenium.webdriver ...

  3. (转载)WPF:DataGrid设置行、单元格的前景色

    WPF:DataGrid设置行.单元格的前景色 0. 说明 /********************************** 本示例实现功能1.DataGrid基本操作2.列标题样式3.内容居中 ...

  4. Java的数组与内存控制

    1     数组基础 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个数组元素(item),每个数组元素可以通过一个下标/索引来(index)访问它们. 数组 ...

  5. MFC:DISP_FUNCTION 参数

    /*#include <afxdisp.h>DISP_FUNCTION( theClass, pszName, pfnMember, vtRetVal, vtsParams )参数:the ...

  6. Codeforces Round 513 (Div.1+Div.2)

    比赛传送门 10月4号的比赛,因为各种原因(主要是懒),今天才写总结-- Div1+Div2,只做出两个题+迟到\(20min\),日常掉\(rating\)-- \(\rm{A.Phone\;Num ...

  7. 计算机应用第三次作业:自动开机自动关机 常用DOS命令 关于文件文件夹

    一.自动开机 台式机启动时按住DEL键 进入一个蓝色的界面,界面上是英文提示 这个界面是BIOS  ,是在机器的ROM中存储 二.自动关机 自动重启 方法一在120秒钟后自动关机 win+r (RUN ...

  8. drawRect - 谈画图功能的内存优化

    作者介绍 作者:毕洪博 ( @毕洪博 ),iOS 开发者,pop Art 追随者.现在正在鼓捣 AVFoundation,博客 bihongbo.com, 欢迎大家找我讨论技术. 作者已将本文在微信公 ...

  9. 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围

    一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...

  10. c语言之内存管理

    在计算机系统,特别是嵌入式系统中,内存资源是非常有限的.尤其对于移动端开发者来说,硬件资源的限制使得其在程序设计中首要考虑的问题就是如何有效地管理内存资源.本文是作者在学习C语言内存管理的过程中做的一 ...