[蓝桥杯 2023 省 A] 网络稳定性

思路:最大生成树+lca查询

时间复杂度:O(eloge + qlogn)

using namespace std;
#include<iostream>
#include<algorithm>
#include<cmath>

const int maxn=1e5+10;
 
struct EDGE{
	int next,to,from,w;
	bool operator<(const EDGE&it)const
	{
		return w>it.w;
	}
}e[maxn<<2],edge[maxn<<1];

int n,m,q,head[maxn],cnt=1,pre[maxn],used[maxn],depth[maxn],dp[maxn][26],dist[maxn][26];

void add(int x,int y,int w)
{
	edge[cnt]={head[x],y,x,w};
	head[x]=cnt++;
}

int find(int x)
{
	return x==pre[x]?x:pre[x]=find(pre[x]);
}

void kr()
{
	sort(e+1,e+1+m);
	
	for(int i=1;i<=n;++i)pre[i]=i;
	
	for(int i=1,from,to;i<=m;++i)
	{
		from=find(e[i].from);
		to=find(e[i].to);
		
		if(from==to)continue;
		
		pre[from]=to;
		
		add(e[i].from,e[i].to,e[i].w);
		add(e[i].to,e[i].from,e[i].w);
	}
}

void dfs(int x)
{
	for(int i=1;(1<<i)<depth[x];++i)
		dp[x][i]=dp[dp[x][i-1]][i-1],
		dist[x][i]=min(dist[x][i-1],dist[dp[x][i-1]][i-1]);
		
	used[x]=1;
	for(int i=head[x],v;i;i=edge[i].next)
	{
		v=edge[i].to;
		
		if(used[v])continue;
		
		depth[v]=depth[x]+1;
		dp[v][0]=x;
		dist[v][0]=edge[i].w;
		
		dfs(v);
	}
}

void init()
{
	for(int i=1;i<=n;++i)
	{
		if(used[i])continue;
		depth[i]=1;
		dfs(i);
	}
}

int lca(int x,int y)
{
	if(depth[x]<depth[y])swap(x,y);
	
	int k=log2(depth[x]-depth[y]),ans=1e9;
	
	for(int i=k;i>=0;--i)
		if(depth[dp[x][i]]>=depth[y])
		{
			ans=min(ans,dist[x][i]);
			x=dp[x][i];
		}
		
	if(x==y)return ans;
	
	k=log2(depth[x]);
	
	for(int i=k;i>=0;--i)
		if(dp[x][i]!=dp[y][i])
		{
			ans=min(ans,dist[x][i]);
			ans=min(ans,dist[y][i]);
			x=dp[x][i];
			y=dp[y][i];
		}
	
	ans=min(ans,dist[x][0]);
	ans=min(ans,dist[y][0]);
	
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	cin>>n>>m>>q;
	
	for(int i=1;i<=m;++i)cin>>e[i].from>>e[i].to>>e[i].w;
	
	kr();
	
	init();
	
	int x,y;
	
	while(q--)
	{
		cin>>x>>y;
		
		if(find(x)!=find(y))cout<<-1<<'\n';
		else
		{
			cout<<lca(x,y)<<'\n';
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
蓝桥杯2023年B组C++赛的题目包括了2个填空题和8个编程题。根据参赛者的博客\[1\],本届蓝桥杯的难度相较于上届有所增加,编程题的难度较大,而思维题的数量较少。参赛者在博客中提到了其中一个题目,即试题A:日期统计\[2\]。在博客中还提到了一种解题方法,即暴力枚举\[3\]。暴力枚举的代码如下: ```cpp #include <iostream> #include <cmath> #include <algorithm> using namespace std; const int total = 23333333; const double H = 11625907.5798; int main() { for (int i = 0; i < total / 2; i++) { double ans = 0; ans -= 1.0 * i * i / total * log2(1.0 * i / total); ans -= 1.0 * (total - i) * (total - i) / total * log2(1.0 * (total - i) / total); if (abs(ans - H) < 1e-4) { cout << i << endl; return 0; } } return 0; } ``` 以上是关于蓝桥杯2023年B组C++赛的一些信息。希望对你有所帮助! #### 引用[.reference_title] - *1* *3* [2023年第十四届蓝桥杯C++B组复盘](https://blog.csdn.net/m0_46326495/article/details/130043563)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [2023第十四届蓝桥杯C++B组题目回顾与参赛感想](https://blog.csdn.net/BinBinCome/article/details/130048888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DogDu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值