【NOIP2013/Codevs3287】货车运输-最小生成树(大)-树上倍增
https://www.luogu.org/problemnew/show/P1967
由题可知,我们走的路的边应尽可能大,所以通过$kruscal$建最大生成树的图,再树上倍增,注意可能有多棵树;
#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(register int i = (l);i <= (r);i++)
#define down(i,l,r) for(register int i = (l);i >= (r);i--)
#define traversal_vedge(i) for(register int i = head[u]; i ;i = e[i].nxt)
#define ll long long
using namespace std;
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;
}
const int N = 1e4+,M = 5e5+,inf = 0x3f3f3f3f;
int n,m,q,f[N];
struct kedge{
int u,v,limit;
bool operator < (const kedge &x) const{
return limit > x.limit;
}
}ke[M<<]; struct edge{
int v,limit,nxt;
}e[M<<];int tot,head[N]; void build_tree(int u,int v,int w) {e[++tot] = (edge){v,w,head[u]}; head[u] = tot;} //-----------------------------------------------------------------
int find(int i) {if(f[i] == i) return i;f[i] = find(f[i]);return f[i]; }
void kruscal(){
sort(ke+,ke+m+);
up(i,,n) f[i] = i;
int cnt = ;
up(i,,m)
{
if(cnt == n-) break;
if(find(ke[i].u) != find(ke[i].v))
{
f[f[ke[i].u]] = f[ke[i].v];
cnt++;
build_tree(ke[i].u,ke[i].v,ke[i].limit);
build_tree(ke[i].v,ke[i].u,ke[i].limit);//debug ke[i].u -> ke[i].v;
}
else continue;
}
}
//----------------------------------------------------------------------- int fa[N][],mine[N][],dep[N],vis[N];//debug 15 ->14 void dfs(int u,int f,int w){
vis[u] = ;
dep[u] = dep[f] + ;
fa[u][] = f;
mine[u][] = w;
for(int i = ; (<<i) <= dep[u]; i++)
{
fa[u][i] = fa[fa[u][i-]][i-];
mine[u][i] = min(mine[u][i-],mine[fa[u][i-]][i-]);
}
traversal_vedge(i)
{
int v = e[i].v;
if(v == f) continue;
dfs(v,u,e[i].limit);
}
} /*int LCA(int x,int y){
if(dep[x] < dep[y]) swap(x,y);
down(i,14,0)
{
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
}
if(x == y) return x;
down(i,14,0)
{
if(fa[x][i] != fa[y][i])
{
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}*/
//----------------------------------------------------------------- int query(int x,int y)
{
int ans = inf;
if(dep[x] < dep[y]) swap(x,y);
down(i,,)
{
if(dep[fa[x][i]] >= dep[y])
{
ans = min(ans,mine[x][i]);
x = fa[x][i];
}
}
if(x == y) return ans;
down(i,,)
{
if(fa[x][i] != fa[y][i])
{
ans = min(ans,min(mine[x][i],mine[y][i]));
x = fa[x][i];
y = fa[y][i];
}
}
ans = min(ans,min(mine[x][],mine[y][]));
return ans;
} /*int query(int x,int lca){
//if(x == lca) return 0x3f3f3f3f;
int i = 0;
while(dep[fa[x][i]] > dep[lca] ) i++;
return mine[x][i];
}*/ void work(){
kruscal();
memset(mine,0x3f,sizeof(mine));
//debug 未考虑多棵树
up(i,,n)
{
if(!vis[i])
dfs(i,,inf);
}
q = read;
while(q--)
{
int x = read, y = read;
if(find(x) != find(y))
{
printf("-1\n");
continue;
}
//int lca = LCA(x,y);
//ans = min(query(x,lca),query(y,lca));
printf("%d\n",query(x,y));
}
} void readdata()
{
n = read; m = read;
up(i,,m)
{
ke[i].u = read; ke[i].v = read; ke[i].limit = read;
}
} int main()
{
freopen("input21.txt","r",stdin);
//freopen("output21.out","w",stdout);
readdata();
work();
return ;
}
最开始的写法之所以是错误的,是因为会多求一段,而我们只需要求到LCA就行;
【NOIP2013/Codevs3287】货车运输-最小生成树(大)-树上倍增的更多相关文章
- [NOIP2013/Codevs3287]货车运输-最小[大]生成树-树上倍增
Problem 树上倍增 题目大意 给出一个图,给出若干个点对u,v,求u,v的一条路径,该路径上最小的边权值最大. Solution 看到这个题第一反应是图论.. 然而,任意路径最小的边权值最大,如 ...
- xsy 2018 【NOIP2013】货车运输
[NOIP2013]货车运输 Description A 国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有q辆货车在运输货物,司机们想知道每辆车在不超 ...
- 「NOIP2013」「LuoguP1967」货车运输(最大生成树 倍增 LCA
题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...
- codevs3287货车运输(最小生成树+LCA)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 ...
- Luogu1967 NOIP2013 货车运输 最大生成树、倍增
传送门 题意:给出一个$N$个节点.$M$条边的图,$Q$次询问,每一次询问两个点之间的所有可行路径中经过的边的边权的最小值中的最大值.$N \leq 10000 , M \leq 50000 , Q ...
- $Noip2013/Luogu1967$ 货车运输 最大生成树+倍增$lca$
$Luogu$ $Sol$ 首先当然是构建一棵最大生成树,然后对于一辆货车的起点和终点倍增跑$lca$更新答案就好.记得预处理倍增的时候不仅要处理走了$2^i$步后是那个点,还有这中间经过的路径权值的 ...
- C++之路进阶——codevs3287(货车运输)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 n ...
- NOIP2013 D1T3 货车运输 zz耻辱记
目录 先来证明下lemma: 图上2点间最小边权最大的路径一定在MST上 感性理解下: 每次kruskal algo都连接最大的不成环边 此时有2个未联通的联通块被连起来. 那么考虑u, v两点的联通 ...
- NOIP2013 D1T3 货车运输
[NOIP2013T3]货车运输 背景 noip2013day1 描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重 量限制,简称限重.现在有 q 辆货 ...
随机推荐
- iterator简单描述
Item 26. Prefer iterator to const iterator, reverse_iterator, and const_reverse_iterator. 上面一段话,是< ...
- Java访问Phoenix连接
两种方法,一种是直接使用jdbc连接,一种是使用spring连接. jdbc连接和访问oracle步骤相同: ///////////// 测试Phoenix连接 /////////////// Str ...
- 【Linux】【Jenkins】代码编译和执行过程中的问题汇总
1.问题1:java.io.FileNotFoundException: /root/.jenkins/workspace/Videoyi_AutoTest_Maven/config-log4j\lo ...
- 李清华201772020113《面向对象程序设计(java)》第二周学习总结
李清华201772020113<面向对象程序设计(java)>第二周学习总结 第一部分 理论知识 第三章 本章主要讲了java基本知识中的标识符,关键字,注释,以及数据类型,变量,运算符, ...
- Python 进行查询日志查询条件分析
任务:crm日志的查询条件 每次是哪几个字段查,有几种组合 ,统计每种组合查询的量 日志样例: -- ::] -- ::] 查询条件:query查询条件可以多个,用|and|分割. 步骤: 1.正则 ...
- 一个判断I2C总线通信异常原因的方法
此问题由某客户提出,应用处理器 AP与 MCU进行 I2C通信,通信会经常发生异常,需要定位原因. 首先需要定位的是因为哪个器件发的波形不正确导致通信异常,所以我们在 I2C 线路上增加了以下处理,增 ...
- Echarts 柱状图属性详解
<script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init ...
- SSM商城项目(五)
1. 学习计划 1.前台系统搭建 2.商城首页展示 3.Cms系统的实现 a) 内容分类管理 b) 内容管理 4.前台内容动态展示 2. 商城首页展示 2.1. ...
- Linux Oracle安装
lsnrctl status // 查看linux系统oracle的监听状态lsnrctl start // 启动linux系统oracle的监听状态 sqlplus /nolog // 连接 ...
- HttpRunnerManager安装部署
uname -a cat /etc/redhat-release 1.安装docker.mysql.rabbitmq sudo yum update curl -fsSL https://get.do ...