CodeForces 455C Civilization

本文介绍了一种处理森林连通块直径问题的算法,通过并查集维护连通信息,利用DFS遍历求出每个连通块的直径,再通过特定公式计算合并后的连通块直径。适用于大规模数据处理,时间复杂度为Θ(nlog2n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意

%   给定一个森林,边权为1,支持以下两种操作
   1   x   y \texttt{1 x y} 1 x y 若节点x和节点y不在同一个连通块内,你需要在节点x所在连通块和节点y所在的连通块中分别找一个点,连接一条边权为1的边,使得最终的连通块内的直径尽量长。
   2   x \texttt{2 x} 2 x 输出节点x所在连通块中的直径长度。
  数据范围  1 ⩽ n ⩽ 3 × 1 0 5 1\leqslant n\leqslant 3\times 10^5 1n3×105

题解

%   先求出每个连通块内的直径长度,然后用并查集维护联通信息,对于操作2,,我们考虑连接两个连通块各自的直径的重点,则可以发现,最终得到的直径长度不会超过
⌈ d i a m e t e r [ a n c e s t o r [ x ] ] 2 ⌉ + ⌈ d i a m e t e r [ a n c e s t o r [ y ] ] 2 ⌉ + 1 \left\lceil\frac{diameter[ancestor[x]]}{2}\right\rceil+\left\lceil\frac{diameter[ancestor[y]]}{2}\right\rceil+1 2diameter[ancestor[x]]+2diameter[ancestor[y]]+1  用上式和 d i a m e t e r [ a n c e s t o r [ x ] ] , d i a m e t e r [ a n c e s t o r [ y ] ] diameter[ancestor[x]],diameter[ancestor[y]] diameter[ancestor[x]],diameter[ancestor[y]] 取最大值即可。时间复杂度为 Θ ( n α ( n ) ) \Theta(n\alpha(n)) Θ(nα(n)),下面的代码没有使用按秩合并,因而时间复杂度为 Θ ( n log ⁡ 2 n ) \Theta(n\log_2 n) Θ(nlog2n)

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 300010
struct edge{
	int v,next;
}edges[maxn<<1];
int n,m,q,head[maxn];
void ins(int u,int v){
	static int cnt=0;
	edges[++cnt]=(edge){v,head[u]};
	head[u]=cnt;
}
int f[maxn],g[maxn],vis[maxn];
void dfs(int u,int fa=-1){
	vis[u]=true;
	for(int i=head[u];i;i=edges[i].next){
		int v=edges[i].v;
		if(v==fa||vis[v]) continue;
		dfs(v,u);
		f[u]=max(f[u],g[v]+g[u]+1);
		f[u]=max(f[u],f[v]);
		g[u]=max(g[u],g[v]+1);
	}
}
int fa[maxn];
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
int main(){
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1,u,v;i<=m;i++){
		scanf("%d%d",&u,&v);
		ins(u,v);ins(v,u);
		int fx=find(u),fy=find(v);
		if(fx!=fy) fa[fx]=fy;
	}
	for(int u=1;u<=n;u++){
		int fu=find(u);
		if(!vis[fu]) dfs(fu);
	}
	for(int i=1,op;i<=q;i++){
		scanf("%d",&op);
		if(op==1){
			int x; scanf("%d",&x);
			printf("%d\n",f[find(x)]);
		}else{
			int x,y; scanf("%d%d",&x,&y);
			int fx=find(x),fy=find(y);
			if(fx!=fy){
				f[fx]=max((f[fx]+1>>1)+(f[fy]+1>>1)+1,max(f[fx],f[fy]));
				fa[fy]=fx;
			}
		}
	}
	return 0;
}
### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值