题面

题解

\(kruskal\)重构树板子题??(大雾

因为重构树上两点之间的\(LCA\)的权值就是原图上最小生成树上的瓶颈。

所以建个重构树,跑\(LCA\)即可。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout); inline int read()
{
int data = 0, w = 1;
char ch = getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
} const int maxn(2e5 + 10), maxm(3e5 + 10);
struct edge { int next, to; } e[maxn << 2];
struct Edge { int from, to, dis; } E[maxm]; inline bool cmp(const Edge &lhs, const Edge &rhs) { return lhs.dis < rhs.dis; }
int fa[maxn], f[maxn], size[maxn], belong[maxn], heavy[maxn], head[maxn], e_num, cnt, pos[maxn], val[maxn], n, m, n_cnt;
inline void add_edge(int from, int to) { e[++e_num] = (edge) {head[from], to}; head[from] = e_num; } void dfs(int x)
{
size[x] = 1;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == fa[x]) continue;
fa[to] = x; dfs(to); size[x] += size[to];
if(size[heavy[x]] < size[to]) heavy[x] = to;
}
} void dfs(int x, int chain)
{
pos[x] = ++cnt; belong[x] = chain;
if(!heavy[x]) return;
dfs(heavy[x], chain);
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == fa[x] || to == heavy[x]) continue;
dfs(to, to);
}
} inline int LCA(int a, int b)
{
while(belong[a] != belong[b])
{
if(pos[belong[a]] < pos[belong[b]]) std::swap(a, b);
a = fa[belong[a]];
}
return pos[a] < pos[b] ? a : b;
} inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
inline void Kruskal()
{
std::sort(E + 1, E + m + 1, cmp); n_cnt = n;
for(RG int i = 1; i <= m; i++)
{
int fx = find(E[i].from), fy = find(E[i].to);
if(fx == fy) continue;
f[fx] = f[fy] = ++n_cnt; f[n_cnt] = n_cnt;
add_edge(n_cnt, fx); add_edge(fx, n_cnt);
add_edge(n_cnt, fy); add_edge(fy, n_cnt);
val[n_cnt] = E[i].dis;
} for(RG int i = n_cnt; i; i--)
if(!pos[i]) dfs(i), dfs(i, i);
} int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read(); m = read();
for(RG int i = 1; i <= m; i++) E[i] = (Edge) {read(), read(), read()};
for(RG int i = 1; i <= n; i++) f[i] = i;
RG int q = read(); Kruskal();
while(q--)
{
int a = read(), b = read();
if(find(a) != find(b)) puts("impossible");
else printf("%d\n", val[LCA(a, b)]);
}
return 0;
}

【洛谷P2245】星际导航的更多相关文章

  1. 洛谷 P2245 星际导航 解题报告

    P2245 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向 ...

  2. [洛谷P2245]星际导航

    题目大意:有一张n点m边的带权无向图,和一些问题,每次询问两个点之间的路径的最大边权最小是多少. 解题思路:同NOIP2013货车运输,只是数据增大,大变成小,小变成大了而已.所以具体思路见货车运输. ...

  3. 最小生成树+LCA【洛谷 P2245】 星际导航

    [洛谷 P2245] 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边 ...

  4. 【luogu P2245 星际导航】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2245 = 货车运输 被逼着写过mst+lca 后来成了mst+树剖 #include <cstdio& ...

  5. 洛谷.2754.星际转移问题(最大流Dinic 分层)

    题目链接 枚举时间 每一个时间点 对于每个之前的位置像当前位置连边,表示这一时刻可待在原地 每艘船 之前时刻位置向当前时刻连边 注意别漏了0时刻src连向earth的边 #include<cst ...

  6. 洛谷P2469 星际竞速

    上下界费用流比较无脑,提供一种更巧妙的费用流,无需上下界. #include <cstdio> #include <algorithm> #include <queue& ...

  7. P2245 星际导航

    题目描述 sideman 做好了回到 Gliese星球的硬件准备,但是 sideman 的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有 N 个顶点和 M 条边的带权无向图,顶点表示 ...

  8. P2245 星际导航 瓶颈路

    \(\color{#0066ff}{ 题目描述 }\) sideman 做好了回到 \(\text{Gliese}\) 星球的硬件准备,但是 \(\text{sideman}\) 的导航系统还没有完全 ...

  9. [LUOGU] P2245 星际导航

    题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向图,顶点表示各个星系, ...

随机推荐

  1. PyCharm创建虚拟环境 和 TVTK库的安装

    注:示例系统环境:Windows10 64位 + Anaconda3: 昨天安装TVTK库遇到些麻烦,当时随便将库下到个文件夹安装后(没找到VENV文件夹...), 进行测试:from tvtk.to ...

  2. 【[国家集训队]Crash的数字表格 / JZPTAB】

    这道题我们要求的是 \[\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)\] 总所周知\(lcm\)的性质不如\(gcd\)优雅,但是唯一分解定理告诉我们\(gcd(i,j)\time ...

  3. WiFi密码忘记了怎么办之解决方案

    随着科技不断进步,网络产品也越来越便宜了.家家户户基本上都有能力装上宽带. 但是有的时候,时间久了,我们可能会忘记密码(密码设置比较复杂的情况下).那么如何找到密码呢? 通常的办法有很多,百度或者Go ...

  4. Dubbo实践(四)设计模式

    Dubbo框架在初始化和通信过程中使用了多种设计模式,可灵活控制类加载.权限控制等功能. 工厂模式 Provider在export服务时,会调用ServiceConfig的export方法.Servi ...

  5. [LuoguP1221]最多因子数

    [Luogu1221]最多因子数(Link) 求区间[L,R]内约数个数最多的数和它的约数个数. 这个题吧,乍一看确实不是很难,然后稍微一想,嗯,是个傻*题.这是唯一感受,不要问我为什么. 首先我们定 ...

  6. css让不同大小的图片适应div的大小,且不变形。

    做成背景图片 单个 .imgdiv { width: 100px; // 你要的正方形 height: 100px; // 你要的正方形 background-image: url(/your/ima ...

  7. HashMap源码阅读与解析

    目录结构 导入语 HashMap构造方法 put()方法解析 addEntry()方法解析 get()方法解析 remove()解析 HashMap如何进行遍历 一.导入语 HashMap是我们最常见 ...

  8. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  9. vue面试题!!!

    由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...

  10. vue+element 点击按钮后 导致 刷新页面 致url中拼接 ? 或者拼接参数

    https://blog.csdn.net/sinat_37255207/article/details/88917162 element 自己的<el-form></el-form ...