【洛谷P2245】星际导航
题面
题解
\(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】星际导航的更多相关文章
- 洛谷 P2245 星际导航 解题报告
P2245 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向 ...
- [洛谷P2245]星际导航
题目大意:有一张n点m边的带权无向图,和一些问题,每次询问两个点之间的路径的最大边权最小是多少. 解题思路:同NOIP2013货车运输,只是数据增大,大变成小,小变成大了而已.所以具体思路见货车运输. ...
- 最小生成树+LCA【洛谷 P2245】 星际导航
[洛谷 P2245] 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边 ...
- 【luogu P2245 星际导航】 题解
题目链接:https://www.luogu.org/problemnew/show/P2245 = 货车运输 被逼着写过mst+lca 后来成了mst+树剖 #include <cstdio& ...
- 洛谷.2754.星际转移问题(最大流Dinic 分层)
题目链接 枚举时间 每一个时间点 对于每个之前的位置像当前位置连边,表示这一时刻可待在原地 每艘船 之前时刻位置向当前时刻连边 注意别漏了0时刻src连向earth的边 #include<cst ...
- 洛谷P2469 星际竞速
上下界费用流比较无脑,提供一种更巧妙的费用流,无需上下界. #include <cstdio> #include <algorithm> #include <queue& ...
- P2245 星际导航
题目描述 sideman 做好了回到 Gliese星球的硬件准备,但是 sideman 的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有 N 个顶点和 M 条边的带权无向图,顶点表示 ...
- P2245 星际导航 瓶颈路
\(\color{#0066ff}{ 题目描述 }\) sideman 做好了回到 \(\text{Gliese}\) 星球的硬件准备,但是 \(\text{sideman}\) 的导航系统还没有完全 ...
- [LUOGU] P2245 星际导航
题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向图,顶点表示各个星系, ...
随机推荐
- angularJs的工具方法3
一.angular.version 判断angular的版本 console.log(angular.version); 二.angular.equals 判断两 ...
- 用Web技术开发客户端(一)
http://www.cnblogs.com/lefan/archive/2012/12/27/2836400.html 范怀宇(@duguguiyu)分享了<豌豆荚2.0重构时遇到的坑> ...
- BZOJ1048:[HAOI2007]分割矩阵(记忆化搜索DP)
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个), 这样分割了(n-1)次后,原矩阵被分割成 ...
- php反序列化
之前听漏洞银行的一个女生讲php反序列化.她说了一句.php反序列话找public变量. 导致我以为必须php反序列化.可控的变量必须是public or protected.private私有的变量 ...
- 【PHP】 mysqli_autocommit() 函数
//获取每一篇文章的内容 function getPost($f_parent_id, $f_title, $f_username, $f_board_id,$f_post_time, $f_ip,$ ...
- js中时间的操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- HTTP 错误 401.0 - Unauthorized 的解决方案
1.安装vs2015后,以前做的项目中Forms身份验证,竟然不能使用了 2.打开当前项目属性,将windows身份验证属性改为启用 3.vs2015生成的mvc项目中,webconfig缺失auth ...
- Java SPI(Service Provider Interface)
SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制. 目前有不少框架用它来做服务的扩展发现, 简单来说,它就是一种动态替换发现的机制, 举个 ...
- Java Runnable和Thread区别
Thread是多个线程分别完成自己的任务,Runnable是多个线程共同完成1个任务.在实际开发中,一个多线程的操作很少使用Thread类,而是通过Runnable接口完成,好处有: 1. 避免点继承 ...
- 网络测量中基于Sketch方法的简单介绍
Sketch介绍 为什么要用Sketch 网络流主要根据五元组.主机地址.包的大小来分类.在网络中存在各种各样的包,如果按照上述分类方法,对每一种包都分配一个计数器来储存,虽然测量准确,那么存放计数器 ...