BZOJ——T 3732: Network
http://www.lydsy.com/JudgeOnline/problem.php?id=3732
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1880 Solved: 894
[Submit][Status][Discuss]
Description
给你N个点的无向图 (1 <= N <= 15,000),记为:1…N。
图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000).
现在有 K个询问 (1 < = K < = 20,000)。
每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?
Input
第一行: N, M, K。
第2..M+1行: 三个正整数:X, Y, and D (1 <= X <=N; 1 <= Y <= N). 表示X与Y之间有一条长度为D的边。
第M+2..M+K+1行: 每行两个整数A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?
Output
对每个询问,输出最长的边最小值是多少。
Sample Input
1 2 5
2 3 4
3 4 3
1 4 8
2 5 7
4 6 2
1 2
1 3
1 4
2 3
2 4
5 1
6 2
6 1
Sample Output
5
5
4
4
7
4
5
HINT
1 <= N <= 15,000
1 <= M <= 30,000
1 <= d_j <= 1,000,000,000
1 <= K <= 15,000
Source
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int n,m,k; #define LL long long
int head[N],sumedge;
struct E
{
int u,v,w;
}e[M];
bool cmp(E a,E b)
{
return a.w<b.w;
}
struct Edge
{
int v,next;
LL dis;
Edge(int v=,int next=,LL dis=):
v(v),next(next),dis(dis){}
}edge[];
inline void ins(int u,int v,LL w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int fa[N],cnt;
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Kruskar()
{
sort(e+,e+m+,cmp);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
int x=e[i].u,y=e[i].v;
int fx=find(x),fy=find(y);
if(fa[fx]==fy) continue;
fa[fx]=fy;
ins(x,y,(LL)e[i].w);
ins(y,x,(LL)e[i].w);
if(++cnt==n-) return ;
}
} LL maxx[N];
int dad[N],deep[N];
void DFS(int x)
{
deep[x]=deep[dad[x]]+;
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].v;
if(dad[x]==v) continue;
maxx[v]=edge[i].dis;
dad[v]=x; DFS(v);
}
}
LL LCA(int x,int y)
{
LL ret=-;
if(deep[x]>deep[y]) swap(x,y);
for(;deep[y]>deep[x];y=dad[y]) ret=max(ret,maxx[y]);
for(;x!=y;x=dad[x],y=dad[y])
ret=max(ret,max(maxx[x],maxx[y]));
return ret;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
memset(maxx,-,sizeof(maxx));
for(int u,v,w,i=;i<=m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
Kruskar();
DFS();
for(int u,v;k--;)
{
scanf("%d%d",&u,&v);
printf("%lld\n",LCA(u,v));
}
return ;
}
BZOJ——T 3732: Network的更多相关文章
- 【BZOJ】3732: Network【Kruskal重构树】
3732: Network Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2812 Solved: 1363[Submit][Status][Dis ...
- BZOJ 3732: Network 最小生成树 倍增
3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...
- 3732: Network
3732: Network Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 395 Solved: 179[Submit][Status] Descr ...
- BZOJ 3732 Network
2016.1.28 纪念我BZOJ第一题 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= ...
- bzoj 3732 Network(最短路+倍增 | LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...
- bzoj 3732: Network 树上两点边权最值
http://www.lydsy.com/JudgeOnline/problem.php?id=3732 首先想到,要使得最长边最短,应该尽量走最短的边,在MST上. 然后像LCA那样倍增娶个最大值 ...
- BZOJ 3732 Network —— 最小生成树 + 倍增LCA
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 <= N <= 15, ...
- Kruskal重构树+LCA || BZOJ 3732: Network
题面:https://www.lydsy.com/JudgeOnline/problem.php?id=3732 题解:Kruskal重构树板子 代码: #include<cstdio> ...
- [bzoj 3732] Network (Kruskal重构树)
kruskal重构树 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第 ...
随机推荐
- Gradle学习之自己定义属性
请通过下面方式下载本系列文章的Github演示样例代码: git clone https://github.com/davenkin/gradle-learning.git 在前面的文章中我们 ...
- hdoj--1408--盐水的故事(技巧)
盐水的故事 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- POJ 3670 DP LIS?
权值为1~3 好了 此题是水题-- i表示到了第i个数,j表示结尾的数是j f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j)) 1<=k<=j 最长上升的. ...
- jqueryValidator自定义校验规则的一种方式(不覆盖源码)
1.封装自定义验证方法-validate-methods.js /***************************************************************** j ...
- Linux 下段错误 core文件
什么是core dump? core的意思是内存,dump的意思是扔出来,堆出来:当一个程序奔溃时,在进程当前工作目录的core文件中复制了该进程的存储图像.core文件仅仅是一个内存映像(同时加上调 ...
- Objective-C 小记(10)__weak
本文使用的 runtime 版本为 objc4-706. __weak 修饰的指针最重要的特性是其指向的对象销毁后,会自动置为 nil,这个特性的实现完全是依靠运行时的.实现思路是非常简单的,对于下面 ...
- Javascript中正则的 match、test、exec使用方法和区别
总结: match 是str调用 test和exec是正则表达式调用 test只返回true或false, exec和match的结果是相同的,返回结果比较复杂
- NodeJS学习笔记 (22)全局对象-global
https://github.com/chyingp/nodejs-learning-guide
- 记intel杯比赛中各种bug与debug【其二】:intel caffe的使用和大坑
放弃使用pytorch,学习caffe 本文仅记录个人观点,不免存在许多错误 Caffe 学习 caffe模型生成需要如下步骤 编写network.prototxt 编写solver.prototxt ...
- 05-数据类型转换(bool类型)