https://www.luogu.org/problemnew/show/1967

kruskal + Lca

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string> using namespace std;
const int N = 1e5 + ; #define oo 99999999 struct Node {
int u, v, w;
} S[N * ];
struct Edge {
int u, v, w, nxt;
} E[N << ]; int now = , n, m, js;
int head[N], p[N], f[N][], g[N][], deep[N]; inline int read() {
int x = ;
char c = getchar();
while(c < '' || c > '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline bool cmp(Node a, Node b) {
return a.w > b.w;
} int getf(int x) {
return p[x] == x ? x : p[x] = getf(p[x]);
} void add(int u, int v, int w) {
E[now].v = v;
E[now].w = w;
E[now].nxt = head[u];
head[u] = now ++;
} inline void Kruskal() {
for(int i = , doen = ; i <= (m << ) && doen < n; i ++) {
int u = S[i].u, v = S[i].v;
int pu = getf(u), pv = getf(v);
if(pu != pv) {
p[pu] = pv;
add(u, v, S[i].w);
add(v, u, S[i].w);
doen ++;
}
}
} void make_deep(int u, int depth) {
deep[u] = depth;
for(int i = head[u]; ~ i; i = E[i].nxt) {
int v = E[i].v;
if(!deep[v]) {
f[v][] = u;
g[v][] = E[i].w;
make_deep(v, depth + );
}
}
} inline void make_jump() {
for(int j = ; ( << j) <= n; j ++)
for(int i = ; i <= n; i ++)
if(f[i][j - ]) f[i][j] = f[f[i][j - ]][j - ], g[i][j] = min(g[i][j - ], g[f[i][j - ]][j - ]);
} inline int lca(int x, int y) {
int ret = oo;
if(x == y) return ;
if(deep[x] < deep[y]) swap(x, y);
int k = log2(deep[x]);
for(int i = k; i >= ; i --) {
if(deep[f[x][i]] >= deep[y]) {
ret = min(ret, g[x][i]);
x = f[x][i];
}
}
if(x == y) return ret;
for(int i = k; i >= ; i --) {
if(f[x][i] != f[y][i]) {
ret = min(ret, min(g[x][i], g[y][i]));
x = f[x][i];
y = f[y][i];
}
}
ret = min(ret, min(g[x][], g[y][]));
return ret;
} int main() {
n = read();
m = read();
for(int i = ; i <= n; i ++) head[i] = -, p[i] = i;
for(int i = ; i <= m; i ++) S[i].u = read(), S[i].v = read(), S[i].w = read();
sort(S + , S + m + , cmp);
Kruskal();
for(int i = ; i <= n; i ++)
if(!deep[i])
make_deep(i, );//可能会有多块
make_jump();
int T = read();
while(T --) {
int x = read(), y = read();
if(getf(x) != getf(y)) {
printf("-1\n");
continue ;
}
int answer = lca(x, y);
printf("%d\n", answer);
}
return ;
}

[Luogu] 货车运输的更多相关文章

  1. [Luogu 1967] NOIP2013 货车运输

    [Luogu 1967] NOIP2013 货车运输 一年多前令我十分头大的老题终于可以随手切掉了- 然而我这码风又变毒瘤了,我也很绝望. 看着一年前不带类不加空格不空行的清纯码风啊,时光也好像回去了 ...

  2. [luogu 1967]货车运输

    货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情 ...

  3. kruskal - 倍增 - 并查集 - Luogu 1967 货车运输

    P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过 ...

  4. Luogu P1967 货车运输(Kruskal重构树)

    P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...

  5. NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】

    NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...

  6. NOIP2013 货车运输 (最大生成树+树上倍增LCA)

    死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...

  7. C++之路进阶——codevs3287(货车运输)

    3287 货车运输 2013年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description A 国有 n ...

  8. NOIP2013 货车运输

    3.货车运输 (truck.cpp/c/pas) [问题描述] A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货 ...

  9. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

随机推荐

  1. pandas之时间重采样笔记

    周期由高频率转向低频率称为降采样:例如5分钟股票交易数据转换为日交易数据 相反,周期也可以由低频转向高频称为升采样 其他重采样:例如每周三(W-WED)转换为每周五(W-FRI) import pan ...

  2. myisam和innodb的区别,java事务不起作用原因

    myisam:只支持表级锁.不支持事务.方便移植.该类型是mysql默认表存储类型 innodb:支持表级锁和行级锁.支持事务. 如果你的事务不起作用,很可能是用了myisam存储引擎,检查数据表引擎 ...

  3. [转]Entity Framework 异常: 'OFFSET' 附近有语法错误。\r\n在 FETCH 语句中选项 NEXT 的用法无效

    https://blog.csdn.net/weixin_34321977/article/details/85850064 在使用 EF 的时候,突然发现更新后在服务器中运行出错,异常信息主要包含以 ...

  4. Action请求后台出现Response already commited异常解决方法

    在编写导出功能使用action请求,在处理导出异常时期望跳转异常页,Controller中的方法返回类型String的url 在处理完逻辑导出文件后后台控制台出现 WARN  [org.springf ...

  5. SEO基础知识

    SEO: SEO是由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”!SEO是指通过对网站进行站内优化和修复(网站Web结构调整.网站内容建设.网站代码优 ...

  6. ReactNative报错null is not an object (evaluating '_rngesturehandlermodule.default.direction')

    程序报错: null is not an object (evaluating 'rngesturehandlermodule.default.direction') 解决: react-native ...

  7. setTimeout()方法和setInterval()方法

    setTimeout方法: 定义和用法: setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. tip: 1000 毫秒= 1 秒. tip:  如果你只想重复执行可以使用setI ...

  8. springboot学习入门简易版三---springboot2.0启动方式

    2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...

  9. List转换字典去重问题

    数据源 var list = new List<TestClass> { ,Name="}, ,Name="}, ,Name="}, ,Name=" ...

  10. Mysql之表的查询

    一.单表的查询 首先让我们先熟悉一下mysql语句在查询操作时执行的顺序: (1)from  (2) on  (3) join  (4) where  (5)group by  (6) avg,sum ...