BZOJ 3732 Network
2016.1.28 纪念我BZOJ第一题
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 < = 15,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
/**************************************************************
Problem: 3732
User: cscscs
Language: C++
Result: Accepted
Time:224 ms
Memory:5036 kb
****************************************************************/ #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxlog=;
inline int read();
struct data
{
int a,b,c;
bool operator<(data d)const {return c<=d.c;}
}Edge[];
int n,m,k,father[];
int last[],to[],w[],final[],e,vis[];
int f[][maxlog],dist[][maxlog],dep[];
int FindFather(int x)
{
if(x==father[x]) return x;
return father[x]=FindFather(father[x]);
}
void AddEdge(int a,int b,int c)
{
w[++e]=c;to[e]=b;last[e]=final[a];final[a]=e;
w[++e]=c;to[e]=a;last[e]=final[b];final[b]=e;
}
void LCA(int x)
{
vis[x]=;
for(int i=;(<<i)<=dep[x];i++)
{
int c=f[x][i-];
f[x][i]=f[c][i-];
dist[x][i]=max(dist[c][i-],dist[x][i-]);
}
for(int i=final[x];i;i=last[i])
{
if(!vis[to[i]])
{
dep[to[i]]=dep[x]+;
f[to[i]][]=x;
dist[to[i]][]=w[i];
LCA(to[i]);
}
}
}
int query(int a,int b)
{
int ret=;
if(dep[a]<dep[b]) swap(a,b);
for(int i = maxlog ; i >= ; i-- ) if(dep[a]-(<<i)>=dep[b])
{
ret=max(ret,dist[a][i]);
a=f[a][i];
}
if(a==b) return ret;
for(int i = maxlog ; i >= ; i-- ) if(dep[a] > (<<i) && f[a][i] != f[b][i])
{
ret=max(ret, max(dist[a][i], dist[b][i]) );
a=f[a][i];b=f[b][i];
}
return max(ret, max(dist[a][], dist[b][]) );
}
int main()
{
n=read();m=read();k=read();
for(int i=;i<=m;i++)
{
Edge[i]={read(),read(),read()};
}
sort(Edge+,Edge+m+);
for(int i=;i<=n;i++) father[i]=i;
for(int i=;i<=m;i++)
{
int u=FindFather(Edge[i].a),v=FindFather(Edge[i].b);
if(u!=v)
{
father[u]=v;
AddEdge(Edge[i].a,Edge[i].b,Edge[i].c);
}
}
for(int i=;i<=n;i++) if(!vis[i]) LCA(i);
while(k--)
{
int x=read(),y=read();
printf("%d\n",query(x,y));
}
}
//----------------------------------------------------
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'') {
if(ch=='-') f=-;ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
BZOJ 3732 Network的更多相关文章
- BZOJ 3732: Network 最小生成树 倍增
3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...
- 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) ,第 ...
- 【刷题】BZOJ 3732 Network
Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_ ...
- BZOJ 3732 Network Link-Cut-Tree (我是认真的!!
题目大意:给定一个n个点m条边的无向连通图.k次询问两点之间全部路径中最长边的最小值 LCT的裸题! 首先维护一个动态的最小生成树,然后每次增加边时删除两点间路径上权值最大的边.最后询问时直接求x到y ...
- BZOJ 3732 Network 最小瓶颈路
题目大意:给出一个无向边,非常多询问,问x,y两地之间的最长路最短是多少. 思路:乍一看好像是二分啊. 的确这个题二分能够做.可是时间会慢非常多,有的题直接就T掉(NOIP2013货车运输). 事实上 ...
随机推荐
- Spring Application Event Example
Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...
- [读书笔记]自动装箱的陷阱以及==与equals
先看一段代码,来自周志明的<深入理解Java虚拟机>. Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Intege ...
- 3.Complementing a Strand of DNA
Problem In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'. The r ...
- metagenome 简介
宏基因组 ( Metagenome)(也称微生物环境基因组 Microbial Environmental Genome, 或元基因组) .是由 Handelsman 等 1998 年提出的新名词, ...
- jQuery插件开发的两种方法及$.fn.extend的详解
jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...
- js判断手机系统是iOS还是android
var arg = navigator.platform; if(arg == "iPhone"){ ...
- php查询文件扩展名
//查询文件扩展名 function extension($str){ $str = implode("",explode("\\",$str)); $str ...
- Eclipse 中 Tomcat启动卡100%(preparing launch delegate...)
我自己遇到这个问题的时候去百度了好几天,没找到我的解决方案,因为我的错误和别人不一样,但提示却和别人一样,在tomcat启动到100%的时候,卡住了,最后显示45秒不够启动,建议我增加时间,所以结果可 ...
- 《C与指针》第十三章练习
本章例程 13.1类型无关的链表查找 #include <stdio.h> #include "node.h" Node *search_list(Node *node ...
- Linux 下如何安装软件
一.解析Linux应用软件安装包 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rpm包 ...