当前位置: 首页 > news >正文

0924

https://www.luogu.com.cn/problem/P1967

换了一种做法做出来了

不记得老师之前怎么讲的

  1 #include<bits/stdc++.h>
  2 #define R 200001
  3 using namespace std;
  4 int n,m,cnt,q;
  5 struct node
  6 {
  7     int u,v,dis;
  8 }edge[R];
  9 bool cmp(node a,node b)
 10 {
 11     return a.dis>b.dis;
 12 }
 13 struct node2
 14 {
 15     int v,nxt;
 16 }E[R];
 17 int head[R],tmp;
 18 int val[R],ff[R],vis[R];
 19 int fa[R],top[R],son[R];
 20 int size[R],dep[R];
 21 void add(int u,int v)
 22 {
 23     E[++tmp].nxt=head[u];
 24     E[tmp].v=v;
 25     head[u]=tmp;
 26 }
 27 int find(int x)
 28 {
 29     if(x==ff[x]) return x;
 30     else return ff[x]=find(ff[x]);
 31 }
 32 void dfs1(int u,int pa)
 33 {
 34     size[u]=1;
 35     vis[u]=1;
 36     for(int i=head[u];i;i=E[i].nxt)
 37     {
 38         int v=E[i].v;
 39         if(v==pa) continue;
 40         dep[v]=dep[u]+1;
 41         fa[v]=u;
 42         dfs1(v,u);
 43         size[u]+=size[v];
 44         if(size[v]>size[son[u]]) son[u]=v;
 45     }
 46 }
 47 void dfs2(int u,int tp)
 48 {
 49     top[u]=tp;
 50     if(son[u]) dfs2(son[u],tp);
 51     for(int i=head[u];i;i=E[i].nxt)
 52     {
 53         int v=E[i].v;
 54         if(v==fa[u]||v==son[u]) continue;
 55         dfs2(v,v);
 56     }
 57 }
 58 void kruskal()
 59 {
 60     sort(edge+1,edge+1+m,cmp);
 61     for(int i=1;i<=n;i++) ff[i]=i;
 62     for(int i=1;i<=m;i++)
 63     {
 64         int fu=find(edge[i].u),fv=find(edge[i].v);
 65         if(fu!=fv)
 66         {
 67             val[++cnt]=edge[i].dis;
 68             ff[cnt]=ff[fu]=ff[fv]=cnt;
 69             add(fu,cnt),add(cnt,fu),add(fv,cnt),add(cnt,fv);
 70         }
 71     }
 72     for(int i=1;i<=cnt;i++)
 73     {
 74         if(!vis[i])
 75         {
 76             int f=find(i);
 77             dfs1(f,0);
 78             dfs2(f,f);
 79         }
 80     }
 81 }
 82 int LCA(int u,int v)
 83 {
 84     while(top[u]!=top[v])
 85     {
 86         if(dep[top[u]]>dep[top[v]]) u=fa[top[u]];
 87         else v=fa[top[v]];
 88     }
 89     return dep[u]<dep[v]?u:v;
 90 }
 91 int main()
 92 {
 93     cin>>n>>m;
 94     cnt=n;
 95     for(int i=1;i<=m;i++) cin>>edge[i].u>>edge[i].v>>edge[i].dis;
 96     kruskal();
 97     cin>>q;
 98     while(q--)
 99     {
100         int u,v;
101         cin>>u>>v;
102         if(find(u)!=find(v)) puts("-1");
103         else cout<<val[LCA(u,v)]<<endl;;
104     }
105     return 0;
106 }
View Code

 

http://www.rkmt.cn/news/11156.html

相关文章:

  • 扫描线学习笔记
  • AI完美声音克隆及情绪控制,与真人无异,Lark下载介绍
  • mysql慢sql配置
  • 新节点加入k8s集群命令查看 - 详解
  • 自动化测试脚本
  • WPF Datagrid loaded 79M items in mvvm , Microsoft.Extensions.DependencyInjection
  • 外部 Tomcat 部署详细 - 实践
  • 20231326《密码系统设计》第三周预习报告
  • FortiGate连接中国联通SDWAN
  • 【Golang】素材设计模式
  • 2025.9.24 闲话:Lucas 定理究极证明
  • 画矩形
  • NOIP 模拟赛八
  • 随便写的
  • Bcliux-docker-nacos2.2.0升级至2.2.3版本
  • 事件和图形界面(暂未完成)
  • Spring连环炮。哈罗面试:Spring Bean生命周期,Spring怎么创建Bean的,BFPP和BPP的x别
  • 软工9.24
  • 无法安装 WebView2! 没有它,此应用就无法运行(解决方式附安装包)
  • 2025CSP-S模拟赛51
  • 2025年9月24日 - 20243867孙堃2405
  • 分库分表后如何高效处理分页
  • 详细介绍:【Selenium】UI自动化测试框架设计:从项目结构到Base-Page层的最佳实践
  • 架构图设计还得是华为 - 智慧园区
  • 解决zsh: corrupt history file /home/sgud4h5gh/.zsh_history的办法
  • 对象初始化器的使用方法
  • 我的学习记录之自我介绍、思维导图和监督措施
  • Nuget安装以及西门子PLC通信
  • 每日反思(2025_09_24)
  • 安装Flask库