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. SCCM Collection 集合获取计算机最后启动时间

    获取计算机客户端最后一次启动时间,我们可以通过多种来源获取,如活动目录组 ,而不仅仅是SCCM 收集,希望对您有所帮助,下面分享PowerShell 脚本 # 1 $CollectionName = ...

  2. 【UWP】【新坑】Excel批量翻译工具(1)

    嗯……具体思路是这样的.使用的时候,你导入一个excel,直观地选择某些区域,选择语言点击翻译,就可以对多个单元格进行批量翻译,并且支持多种不同的导出格式(excel副本.txt文件……) 1,多种翻 ...

  3. Update主循环的实现原理

    从写一段程序,到写一个app,写一个游戏,到底其中有什么不同呢?一段程序的执行时间很短,一个应用的执行时间很长,仅此而已.游戏中存在一个帧的概念. 这个概念大家都知道,类比的话,它就是电影胶卷的格.一 ...

  4. thinkphp 的事务回滚处理 和 原始PHP的事务回滚实例

    1.  要程序里面支持事务,首先连接的数据库和数据表必须支持事务 mysql   为例: 数据库InnoDB支持 transactions 数据表支持事务:InnoDB  支持transaction ...

  5. Hybrid App开发之Html基本标签使用

    前言: 前面简单学习了html简单标签的使用,今天学习一下其他的标签的使用. HTML的超链接 1.)创建一个超链接 <div> <p> <a href="ht ...

  6. Hbase 操作工具类

    依赖jar <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-cli ...

  7. mongodb 导入导出

    F:\Mongodb\bin>mongoexport.exe -h localhost:27017 -d proxy_db -c proxy_tb -o f:/p1.json 导出 -h 服务器 ...

  8. Hibernate中session的save方法问题

    今天在使用session.save(),进行插入数据操作时,一直没有成功,也没有报错.后来发现是因为没有创建事务,提交事务的原因 你对flush和commit的意思没有理解到:1,flush代表刷新, ...

  9. Windows上PostgreSQL安装配置教程

    Windows上PostgreSQL安装配置教程 这篇文章主要为大家详细介绍了Windows上PostgreSQL安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PostgreSQL的 ...

  10. 【wqs二分 决策单调性】HHHOJ#261. Brew

    第一道决策单调性…… 题目描述 HHHOJ#261. Brew 题目分析 挺好的……模板题? 寄存了先. #include<bits/stdc++.h> typedef long long ...