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 ...
随机推荐
- MVC与Holla聊天工具
MVC 是一种设计模式, 它将应用划分为 3 个部分 : 数据( 模型). 展现层( 视图) 和用 户交互层( 控制器). 换句话说, 一个事件的发生是这样的过程 : 1. 用户和应用产生交互. 2. ...
- O2O创业团队,遇到生死悠关的问题,希望大家支招?
简单概括下情况:公司名下有两个内部创业团队,A团队成立3年以上,现在模式基本成熟稳定,有固定营收,但是还未收支平衡:B团队O2O项目,成立5个月左右,还处于萌芽阶段,技术+运营+市场共计9名成员,现总 ...
- Windows Azure 配置Active Directory 主机(3)
步骤 4:在 CloudSite 中安装附加域控制器 1.登录到 YourVMachine,单击“开始”,键入“dcpromo”,然后按 Enter. 2.在“欢迎使用”页上,单击“下一步”. 3.在 ...
- 重写strcat函数,以实现strcat的功能
char * strcatTest(char *dst,const char *src);Action(){ char a[]="come on"; char b[]=" ...
- Redis哨兵原理详解
一.概述 Redis哨兵(以下称哨兵)是为Redis提供一个高可靠解决方案,对一定程序上的错误,可以不需要人工干预自行解决. 哨兵功能还有监视.事件通知.配置功能.以下是哨兵的功能列表: 监控:不间断 ...
- javaSe-反射2
//创建实体类 package com.java.chap07.sec03; public class Student { private String name; private Integer a ...
- opencv c++编译
g++ image2png.cpp -o test `pkg-config --cflags --libs opencv`
- 判断是否是同一人的方法——equals()?在Person类中提供一个比较的方法compare()返回boolean值?对象自己和自己比?
判断是否是同一人的方法——equals() 不能直接用per1==per2,这不是对象内容的比较而是存放对象地址的值得比较 在Person类中提供一个比较的方法compare()返回boolean值 ...
- Python Web 架构
1. Django(全能型)2. Tornado3. BottlePython+Bottle+Sina SAE快速构建网站http://www.cnblogs.com/Xjng/p/3511983.h ...
- pandas删除及其映射修改操作。
1.使用drop_duplicates()函数删除重复的行 df.drop_duplicates() 2.映射 映射的含义,创建一个映射关系,把values元素和一个特定的标签或字符串绑定 map = ...