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) ,第 ...
随机推荐
- Constructor call must be the first statement in a constructor
super()和this ()不能共存.否则编译时会报异常. Constructorcall must be the first statement in a constructor 换句话说就是su ...
- 设置Webdriver启动chrome为默认用户的配置信息
Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...
- FPGA静态时序分析——IO口时序(Input Delay /output Delay)
1.1 概述 在高速系统中FPGA时序约束不止包括内部时钟约束,还应包括完整的IO时序约束和时序例外约束才能实现PCB板级的时序收敛.因此,FPGA时序约束中IO口时序约束也是一个重点.只有约束正确 ...
- Python 序列化处理
序列化 文件为dump 字符串为dumps dumps()方法返回一个str,内容就是标准的JSON loads()方法将其还原 在程序运行的过程中,所有的变量都是在内存 d = dict(name= ...
- Car Talk1
This question is based on a Puzzler that was broadcast on the radioprogram Car Talk1: “I was driving ...
- TabHost的自定义
使用自定义的TabHost可以不用继承TabActicity,但是要注意的是如果使用Activity作为Content的话,有两处代码是一定要加的.不然就会出现RuntimeError,还有在XML布 ...
- NodeJS学习笔记 (27)实用工具模块-util(ok)
debuglog(section) 很有用的调试方法.可以通过 util.debuglog(name) 来创建一个调试fn,这个fn的特点是,只有在运行程序时候,声明环境变量NODE_DEBUG=na ...
- 紫书 习题 10-15 UVa 12063(数位dp)
大佬真的强!!https://blog.csdn.net/u014800748/article/details/45225881 #include<cstdio> #include< ...
- 一个HelloWorld版的MySQL数据库管理器的设计与实现(源码)
2011年,实习期间写了一个简单的数据库管理器. 今天,特意整理了下,分享给大家. 有兴趣的同学,可以下载源码,瞧瞧. 源码只有4个类:LoginGUI,DatabaseGUI,Record,MySQ ...
- ubuntu中开启、关闭防火墙
1.关闭ubuntu的防火墙 ufw disable 开启防火墙 ufw enable 2.卸载了iptables apt-get remove iptables 3.关闭 ...