题意:最大生成树上找

q组两个点的lca

然后求出u->lca->v这条路径上的最小边

倍增大法好

# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <algorithm>
# include <string.h>
# define IL inline
# define ll long long
# define Fill(a, b) memset(a, b, sizeof(a));
using namespace std; IL ll Read(){
char c = '%'; ll x = , z = ;
for(; c < '' || c > ''; c = getchar()) z = c == '-' ? - : ;
for(; c >= '' && c <= ''; c = getchar()) x = x * + c - '';
return x * z;
} const int MAXN = , MAXM = ;
int ft[MAXN], n, m, cnt, fa[MAXN][], w[MAXN], deep[MAXN], Fa[MAXN], num;
struct Edge{
int to, nt;
} edge[MAXM];
struct Kruskal{
int u, v, f;
IL bool operator <(Kruskal b) const{
return f > b.f;
}
} road[MAXM]; IL int Find(int x){
return Fa[x] == x ? x : Fa[x] = Find(Fa[x]);
} IL void Add(int u, int v){
edge[cnt] = (Edge){v, ft[u]}; ft[u] = cnt++;
edge[cnt] = (Edge){u, ft[v]}; ft[v] = cnt++;
} IL void Dfs(int u){
for(int e = ft[u]; e != -; e = edge[e].nt){
int v = edge[e].to;
if(!deep[v]){
deep[v] = deep[u] + ;
fa[v][] = u;
Dfs(v);
}
}
} IL int LCA(int u, int v){
if(Find(u) != Find(v)) return -;
if(deep[u] < deep[v]) swap(u, v);
for(int i = ; i >= ; i--)
if(deep[fa[u][i]] >= deep[v]) u = fa[u][i];
if(u == v) return w[u];
for(int i = ; i >= ; i--)
if(fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return w[fa[u][]];
} int main(){
Fill(ft, -);
num = n = Read(); m = Read();
for(int i = ; i <= * n; i++)
Fa[i] = i;
for(int i = ; i <= m; i++)
road[i] = (Kruskal){Read(), Read(), Read()};
sort(road + , road + m + );
for(int i = , tot = ; i <= m && tot < n; i++){
int u = Find(road[i].u), v = Find(road[i].v);
if(u != v){
tot++;
w[++num] = road[i].f;
Fa[u] = Fa[v] = num;
Add(u, num); Add(v, num);
}
}
for(int i = num; i; i--)
if(!deep[i]) deep[i] = , Dfs(i);
for(int i = ; i <= ; i++)
for(int j = ; j <= num; j++)
fa[j][i] = fa[fa[j][i - ]][i - ];
int Q = Read();
while(Q--){
int u = Read(), v = Read();
printf("%d\n", LCA(u, v));
}
return ;
}

最新文章

  1. 前端开发--ppt展示页面跳转逻辑实现
  2. WCF第一个Demo
  3. GCC编译源代码的四个步骤【转】
  4. 转 Xcode磁盘空间大清理
  5. Ice_cream’s world III--2122
  6. wpf下拉框不能多选的原因
  7. html网页获取php网页数据等知识记录
  8. Linux搭建SVN服务器(服务端)
  9. ynoi2018
  10. Django框架详细介绍---ORM---图书信息系统专题训练
  11. cocos2dx 3.x版本搭建Mac环境工程(创建一个新的C++工程)百分百可行
  12. 利用vue写一个复选框的组件
  13. org.springframework.dao.DuplicateKeyException
  14. spring-aop代理的生效原理
  15. 简明 MongoDB 入门教程
  16. 使用delphi 开发多层应用(二十三)KbmMW 的WIB
  17. 配置maven为阿里云加速
  18. Echarts树图定制详解
  19. Cache和Buffer的区别(转载)
  20. myeclipse搭建activemq 简单聊天

热门文章

  1. BestCoder Round #63 (div.2)
  2. visual studio code (vscode)像 sublime text 的 ctrl+d 一样多光标选中
  3. maven3 org.codehaus.plexus.classworlds.launcher.launcher 找不到或无法加载主类
  4. sublime 快捷键,左菜单乱码
  5. 如何将cordova导入Android studio,只需两步即可
  6. activiti踩坑
  7. scala如何解决类型强转问题
  8. memcached 不同客户端的问题
  9. Microsoft Office Document Imaging批量ocr 方法
  10. SpringBoot学习笔记(4):与前端交互的日期格式