NOIP 2013 货车运输 树链剖分
2017-09-15 20:20
423 查看
3287 货车运输 2013年NOIP全国联赛提高组
时间限制: 1 s空间限制: 128000 KB
题目等级 : 钻石 Diamond
题目描述 Description
A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。输入描述 Input Description
第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。接下来 m 行每行 3 个整数 x、y、z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意:x 不等于 y,两座城市之间可能有多条道路。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意:x 不等于 y。
输出描述 Output Description
输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。样例输入 Sample Input
4 31 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
样例输出 Sample Output
3-1
3
数据范围及提示 Data Size & Hint
对于 30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q < 1,000;对于 60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q < 1,000;
对于 100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q < 30,000,0 ≤ z ≤ 100,000。
题解:
本来是准备练倍增的,结果最后变成用树链剖分水了……不过就当练习基于边权的树链剖分了吧。
基于边权的树链剖分:可以把这条边的边权下放到下面一个点上即可。
对于这个图,首先可以知道如果一条边不在这个图的最大生成森林上,那么这条边肯定不会参与到对答案的贡献中的,所以我们只需要求出最小生成森林,然后每次查找两个点之间的最小值就是答案。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f; } const int N = 50000 + 10; const int inf = 0x73f3f3f; struct node {int pre,u,v,w;}edge ,e ; int tot=0; void addedge(int from,int to,int w){ edge[++tot].v=to,edge[tot].u=from,edge[tot].w=w; } int num=0,head ; void adde(int from,int to,int w){ e[++num].pre=head[from],head[from]=num; e[num].v=to,e[num].w=w; } int n,m; int fa ,siz ,son ,a ,dep ; void dfs1(int u,int f,int d){ siz[u]=1,fa[u]=f,dep[u]=d; for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==f) continue; a[v]=e[i].w; dfs1(v,u,d+1); siz[u]+=siz[v]; if(siz[v]>siz[son[u]]) son[u]=v; } } int indx=0,in ,seq ; int top ; void dfs2(int u,int tp){ in[u]=++indx;seq[indx]=u; top[u]=tp; if(son[u]==0) return ; dfs2(son[u],tp); for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==son[u]||v==fa[u]) continue; dfs2(v,v); } } inline int Min(int a,int b) {return a<b?a:b;} struct Node{int mmin;}t ; void update(int root){ t[root].mmin=Min(t[root<<1].mmin,t[root<<1|1].mmin); } void build(int root,int l,int r){ if(l==r){ t[root].mmin=a[seq[l]];return ; } int mid=l+r>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); update(root); } int query(int root,int l,int r,int pos,int val){ if(pos<=l&&val>=r) return t[root].mmin; int mid=l+r>>1; int a=inf; if(pos<=mid) a=Min(a,query(root<<1,l,mid,pos,val)); if(val>mid) a=Min(a,query(root<<1|1,mid+1,r,pos,val)); return a; } int query(int u,int v){ int f1=top[u],f2=top[v]; int ans=inf; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(f1,f2),swap(u,v); ans=Min(ans,query(1,1,n,in[f1],in[u])); u=fa[f1],f1=top[u]; } if(dep[u]>dep[v]) swap(u,v); ans=Min(ans,query(1,1,n,in[son[u]],in[v])); return ans; } int ft ; bool cmp(const node &a,const node &b){ return a.w>b.w; } int find(int x){ return ft[x]==x?ft[x]:(ft[x]=find(ft[x])); } int main(){ n=read(),m=read(); for(register int i=1;i<=m;i++){ int u=read(),v=read(),w=read(); addedge(u,v,w); } for(register int i=1;i<=n;i++) ft[i]=i; sort(edge+1,edge+tot+1,cmp); int cnt=0; for(int i=1;i<=m;i++){ int u=edge[i].u,v=edge[i].v; int x=find(u),y=find(v); if(x==y) continue; else ft[x]=y,cnt++; adde(u,v,edge[i].w);adde(v,u,edge[i].w); if(cnt==n-1) break; } // for(register int i=1;i<=n;i++) printf("%d ",ft[i]); dfs1(1,0,0);dfs2(1,1); build(1,1,n); int q=read(); while(q--){ int u=read(),v=read(); int x=find(u),y=find(v); if(x!=y) printf("-1\n"); else printf("%d\n",query(u,v)); } return 0; }
相关文章推荐
- NOIP 2013 货车运输 (树链剖分)
- NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】
- 【NOIP2013-D1T3】货车运输
- [NOIP2013] 货车运输 最大生成树 LCA
- 洛谷1967货车运输 即 NOIP2013 DAY1 T3
- NOIP 2013 货车运输 TYVJ 3115
- NOIP2013货车运输[lca&&kruskal]
- NOIP 2013 Day1 T3 货车运输
- 【NOIP2013】货车运输
- 【题解】NOIP 2013 货车运输(最小生成树+LCA)
- NOIP 2013 货车运输 题解过程
- [P1967][NOIP2013]货车运输
- 【洛谷】1967 [noip2013]货车运输 最小生成树+LCA
- 2013年noip第三题货车运输truck(树链剖分LCA+最大生成树)
- NOIP2013 货车运输
- Codevs 3287 NOIP2013提高组 货车运输(重庆一中高2018级信息学竞赛测验2) 解题报告
- noip2013 货车运输
- 省选专练[SCOI2013]摩托车交易[NOIP2013]货车运输
- [NOIP2013]货车运输
- 水题笔记:noip2013 货车运输 [LCA]