【洛谷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 条边的带权无向图,顶点表示各个星系, ...
随机推荐
- Django F对象的使用
概念 class FF()是代表模型字段的值,也就是说对于一些特殊的字段的操作,我们不需要用Python把数据先取到内存中,然后操作,在存储到db中了. 场景 例1:我们有个统计点击量的字段,每次更新 ...
- python反序列化
import pickle import os class A(object): def __reduce__(self): a = """python -c 'impo ...
- 【js】五子棋-UI学习
http://www.imooc.com/learn/639 棋盘用canvas实现
- Kali-linux渗透攻击应用
前面依次介绍了Armitage.MSFCONSOLE和MSFCLI接口的概念及使用.本节将介绍使用MSFCONSOLE工具渗透攻击MySQL数据库服务.PostgreSQL数据库服务.Tomcat服务 ...
- SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?
在web.xml中 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-c ...
- jmeter接口测试1-参数化
jmeter一个简单的接口测试例子 1.创建一个http请求: 大概的步骤就是: (1)创建一个线程组 (2)添加HTTP信息头管理器:右键线程组,选择,添加user-Agent,key-value形 ...
- scrapy---setting的字段含义
# -*- coding: utf-8 -*- # Scrapy settings for lizi project # # For simplicity, this file contains on ...
- Python cx_oracle自动化操作oracle数据库增删改查封装,优化返回查询数据
# coding=utf-8 import cx_Oracle import os import json os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_C ...
- 修改linux系统的默认语言
修改linux系统的默认语言: 1.全局修改: 所有用户都是同一种统一的语言设置 修改/etc/sysconfig/i18n文件 vi /etc/s ...
- 关于JavaScript 常见的面试题
关于JavaScript常见的面试题总结 一.JavaScript基本数据类型 null:空.无.表示不存在,当为对象的属性赋值为null,表示删除该属性 undefined:未定义.当声明变量却没有 ...