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. Sqoop基础学习(1)

    1. Sqoop的导入过程 在开始导入之前,Sqoop会通过JDBC来获得所需要的数据库元数据 1.导入表的列名.数据类型等: 2.接着这些数据库的数据类型(varchar.number等)会把映射成 ...

  2. Java中super关键字的作用与用法

    Java中的super是什么?java中的super关键字是一个引用变量,用于引用父类对象.关键字“super”以继承的概念出现在类中.主要用于以下情况: 1.使用super与变量:当派生类和基类具有 ...

  3. Yii2.0 集成使用富头像上传编辑器

    在开发过程中,我们会用到头像上传的功能.这里给大家推荐一款比较流行的头像上传组件,FullAvatarEditor 2.3(富头像上传编辑器). 实际效果如图所示: 1.下载组件,下载地址:http: ...

  4. [nmon]使用nmon工具监控系统资源

    1.下载nmon 下载正确的nmon版本, 查看linux服务器版本,命令:lsb_release -a,查看到当前系统为RedHat 6.4 然后我们根据我们的linux版本,下载相应nmon版本, ...

  5. jsoup获取网页属性

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  6. SDUT 1309 不老的传说问题 (区间DP)

    题意: 有一个环形序列,n个数字表示一种颜色,要求将白板环刷成一模一样的环,限制是每次最多只能刷连续的K个位置,问最少需要刷几次? 思路: 跟2008长春那道painter string 差不多.只是 ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. 让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包)

    原文出处:让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包) http://www.dotblogs.c ...

  9. BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)

    今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...

  10. python基础一 day15 复习

    迭代器和生成器迭代器 可迭代协议 —— 含有iter方法的都是可迭代的 迭代器协议 —— 含有next和iter的都是迭代器 特点 节省内存空间 方便逐个取值,一个迭代器只能取一次.生成器 —— 迭代 ...