最小瓶颈路 加强版

重构树

最小生成树在合并 (x, y) 时,新建节点 z,link(x, z), link(y, z), 新建节点的权值为 w_{x,y}, 这样的

话任意两点的 answer 为新树上两点 lca 的权值,由于询问次数非常多,显然不可以 logn 求 lca。这里利用

RMQ 求 lca,预处理后时间复杂度 O(1)

#include <bits/stdc++.h>

const int N = 7e4 + 10, M = 1e5 + 10, Mod = 1e9 + 7;

int n, m;
struct Node {
int u, v, w;
bool operator < (const Node a) const {return w < a.w;}
} E[M];
int A, B, C, P; inline int Rand() {A = (A * B + C) % P; return A;} int fa[N << 1];
int Get(int x) {return x == fa[x] ? x : fa[x] = Get(fa[x]);} std:: vector <int> Vec[N << 1];
int tot_point;
int Val[N << 1]; void Rebuild() {
tot_point = n;
for(int i = 1; i <= (n << 1); i ++) fa[i] = i;
for(int i = 1; i <= m && tot_point != n + n - 1; i ++) {
int fau = Get(E[i].u), fav = Get(E[i].v);
if(fau != fav) {
tot_point ++;
fa[fau] = fa[fav] = tot_point;
Vec[tot_point].push_back(fau);
Vec[tot_point].push_back(fav);
Val[tot_point] = E[i].w;
}
}
} int deep[N << 1], In[N << 1], Id[N << 2];
int Tr; void Dfs(int u, int dep) {
deep[u] = dep, In[u] = ++ Tr; Id[Tr] = u;
int S = Vec[u].size();
for(int i = 0; i < S; i ++) Dfs(Vec[u][i], dep + 1), Id[++ Tr] = u;
} int f[N << 2][30], Pow[30], Log[N << 2]; #define T Tr
void Make_st() {
for(int i = 0; (Pow[i] = (1 << i)) <= T; i ++);
Log[1] = 0;
for(int i = 2; i <= T; i ++) Log[i] = Log[i >> 1] + 1;
for(int i = 1; i <= T; i ++) f[i][0] = Id[i];
for(int i = 1; Pow[i] <= T; i ++)
for(int j = 1, ti = T - Pow[i] + 1; j <= ti; j ++)
f[j][i] = (deep[f[j][i - 1]] < deep[f[j + Pow[i - 1]][i - 1]] ? f[j][i - 1] : f[j + Pow[i - 1]][i - 1]);
} int main() {
std:: cin >> n >> m;
for(int i = 1; i <= m; i ++) {
int u, v, w; std:: cin >> u >> v >> w;
E[i] = (Node) {u, v, w};
}
std:: sort(E + 1, E + m + 1);
Rebuild();
Dfs(tot_point, 1);
Make_st();
int Q; std:: cin >> Q;
std:: cin >> A >> B >> C >> P;
int Ans = 0;
for(; Q; Q --) {
int _1 = Rand() % n + 1, _2 = Rand() % n + 1;
int u = In[_1], v = In[_2];
if(u == v) continue;
if(u > v) std:: swap(u, v);
int t;
int ID = f[u][t = Log[v - u + 1]];
if(deep[ID] > deep[f[v - Pow[t] + 1][t]]) ID = f[v - Pow[t] + 1][t];
if((Ans += Val[ID]) >= Mod) Ans -= Mod;
}
std:: cout << Ans;
return 0;
}

loj #137 and #6021的更多相关文章

  1. LOJ#137. 最小瓶颈路 加强版(Kruskal重构树 rmq求LCA)

    题意 三倍经验哇咔咔 #137. 最小瓶颈路 加强版 #6021. 「from CommonAnts」寻找 LCR #136. 最小瓶颈路 Sol 首先可以证明,两点之间边权最大值最小的路径一定是在最 ...

  2. loj#137 最小瓶颈路 加强版

    分析 我们知道答案一定再最小生成树上 于是我们按边权从小到大建立kruskal重构树 然后每次查询lca的值即可 由于询问较多采用st表维护lca 代码 格式化代码 #include<bits/ ...

  3. LOJ题解#136. 最小瓶颈路 DFS+Kruskal

    题目链接: https://loj.ac/problem/136 思路: 在我的这篇博客中已经讲到什么是最短瓶颈路,同时给出了一个用Kruskal求最短瓶颈路的一个简洁易懂的方法,然而这道题目可以看作 ...

  4. leetcode 137

    137. Single Number II Given an array of integers, every element appears three times except for one. ...

  5. TCP和UDP的135、137、138、139、445端口的作用

    如果全是2000以上的系统,可以关闭137.138.139,只保留445 如果有xp系统,可能以上四个端口全部要打开 无论你的服务器中安装的是Windows 2000 Server,还是Windows ...

  6. [Swust OJ 137]--波浪数(hash+波浪数构造)

    题目链接:http://acm.swust.edu.cn/problem/137/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  7. Leetcode 136 137 260 SingleNumber I II III

    Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...

  8. Apache2.4 137行 httpd-ahssl.conf

    C:\Users\Administrator>E:\PHP\Apache24\bin\httpd.exe -w -n "apache2.4" -k startAH00526: ...

  9. 预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445端口

    预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445等端口   如果这种网络端口关闭方法行不通,可以尝试一种新的关闭网络端口方法(比较繁琐)见106楼,补丁安装教程见126 ...

随机推荐

  1. jvm堆内存模型原理分析及堆内存分析工具jhat和MAT的使用超详细教程

  2. python 获取mysql数据库列表以及用户权限

    一.需求分析 需要统计出当前数据库的所有数据库名,以及每个用户的授权信息. 获取所有数据库 在mysql里面,使用命令: show databases 就可以获取所有数据库了 获取所有用户 执行命令: ...

  3. python中的可哈希与不可哈希

    什么是可哈希(hashable)? 简要的说可哈希的数据类型,即不可变的数据结构(字符串str.元组tuple.对象集objects). 哈希有啥作用? 它是一个将大体量数据转化为很小数据的过程,甚至 ...

  4. Deep one-class classification

    Deep one-class classification 2019-03-17 23:09:59 zpainter 阅读数 1027  收藏 文章标签: 单分类问题异常检测 更多 分类专栏: 论文 ...

  5. Centos7部署开源聊天软件rocket.chat

    一.部署rocket.chat 1.看官方文档部署,很简单,一步一步跟着部署即可 注意:需要部署节点需要联网主要是yum方式 https://rocket.chat/docs/installation ...

  6. MongoDB 增删改查 Shell使用及操作

    下载链接:https://robomongo.org/download 安装步骤省略,下一步下一步... 图形界面,连接默认,取个名字就行. 连接成功,可以愉快的使用了,不用总是敲命令了,简洁方便,多 ...

  7. jquery easyui datagrid 在翻页以后仍能记录被选中的行及刷新设置选中行数据

    //easyUI的datagrid在复选框多选时,如何在翻页以后仍能记录被选中的行://注意datagrid中需要配置idField属性,一般为数据的主键 $.ajax({ type: 'GET', ...

  8. C# Java的加密的各种折腾

    24位加密 Java public class DESUtil { private static final String KEY_ALGORITHM = "DESede"; pr ...

  9. iview表单数字验证

    sort: [ {required: true, message: '请填写栏目排序', trigger: 'blur'}, {type: 'number', message: '请输入数字', tr ...

  10. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)的解决

    报错:log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironme ...