题面

题解

\(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. 弹框插件self(动效兼容到IE9,功能兼容IE6)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. jq模仿h5 placeholder效果

    $(".pay-license input").on("input propertychange blur",function(){ if($(this).va ...

  3. Linux环境下GNU, GCC, G++编译器

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012823258/article/details/36890125 一.GNU GNU是" ...

  4. git回滚线上代码

        由于之前自己推代码的时候操作失误,push代码的时候没有push到线上的dev分支,而是push到了线上master分支(主要是因为没有在命令后写分支名,直接推到默认master分支上了),覆 ...

  5. 【jQuery】学习jQuery插件的使用与写法(表单验证插件-validation)

    最新最全的插件可以从jQuery官方网站的插件板块下载,网站地址为:http://plugins.jquery.com/ Validation优点:内置验证规则:自定义验证规则:简单强大的验证信息提示 ...

  6. fdfs上传图片成功在浏览器中访问不到404 Not Found

    1.检查自己nginx配置文件,看是否有   user root这行 . 在nginx.conf文件里加一条:user root; 2.检查自己配置文件: storage.conf中的文件路径是否正确 ...

  7. IT经理苏大强:我不吃,我不喝,我要赶项目!

    IT经理老苏的日常 1周,2周,3周 -- 为了公司的发展和孩子的奥利奥 这点短痛不算什么 Iron Cloud 微服务开发云[www.ironz.com] 高效满足业务需求 高速交付 驱动增长

  8. jquery toggleclass方法

    给元素更改样式,一般使用 addClass() 和removeClass() jquery官方文档 对 addClass的介绍: Adds the specified class(es) to eac ...

  9. UML架构设计师必备神器

    UML-架构设计师必备神器 做过Java开发的一定都听过UML,也都能感觉到它的重要性.由其是在网上搜索一些高级技术介绍,写的好的.阅读量高的.让初.中级程序员容易看懂的.思路清晰的文章一定有UML类 ...

  10. Ansible常用功能

    Ansible 是近几年十分流行的DevOps工具架构什么的我就不在这里赘述了,网上一找一堆 这里写点我在ansible常用功能 1.命令串 #copy文件 #ansible host文件中的主机组 ...