跳到正文

个人记录

8/14 401

bro_fell_bad约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月14日
收录
2026年8月14日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v1当前

A

用dfs标记处每个点最短距离,

然后for一遍即可

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1e5+10;
int n,d;
int a[N];
int sum;
vector<int> op[N];
int t[N];
void dfs(int u,int fa,int o){
	if(o<a[u]){
		a[u]=o;
	}
	for(int i=0;i<op[u].size();i++){
		int v=op[u][i];
		if(v!=fa){
			dfs(v,u,o+1);
		}
	}
}

//unsigned
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>d;
	a[1]=0;
	for(int i=2;i<=n;i++){
		a[i]=1e9;
	}
	for(int i=1;i<=n-1;i++){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
		op[y].push_back(x);
	}
	dfs(1,0,0);
	int sum=0;
	for(int i=2;i<=n;i++){
		if(a[i]<=d) sum++;
	}
	cout<<sum;
	return 0;
}

B

结论:

从连通图任意一点求最长路径,

最后一点必定为直径的端点之一

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1e5+10;
int n,d;
int ma,id;
vector<int> op[N];
void dfs(int u,int fa,int o){
	if(o>ma){
		ma=o;
		id=u;
	}
	for(int i=0;i<op[u].size();i++){
		int v=op[u][i];
		if(v!=fa){
			dfs(v,u,o+1);
		}
	}
}

//unsigned
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n-1;i++){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
		op[y].push_back(x);
	}
	dfs(1,0,0);
	ma=0;
	dfs(id,0,0);
	cout<<ma;
	return 0;
}

C

求出每个连通块的直径,

+1即为点数,求和即可

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1e5+10;
int n,m,sum=0;
int ma,id;
int vis[N];
vector<int> op[N];
void dfs(int u,int fa,int o){
	if(o>ma){
		ma=o;
		id=u;
	}
	vis[u]=1;
	for(int i=0;i<op[u].size();i++){
		int v=op[u][i];
		if(v!=fa){
			dfs(v,u,o+1);
		}
	}
}

//unsigned
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
		op[y].push_back(x);
	}
	for(int i=1;i<=n;i++){
		if(vis[i]==0){
            id=0;
			dfs(i,0,0);
			ma=0;
			dfs(id,0,0);
			sum+=ma+1;
			ma=0;			
		}
	}

	cout<<sum;
	return 0;
}

D

结论:

一个点的出度:有向图中的一点出去的边数

一个点的入度:有向图中有多少条边指向它

无向图中,一个点的度数表示这个点链接的边数

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int N=3e5+10;
int n,m;
int ma,id;
vector<int> op[N];
void dfs(int u,int fa,int o){
    if(o>ma){
        ma=o;
        id=u;
    }
	for(int i=0;i<op[u].size();i++){
		int v=op[u][i];
		if(v!=fa){
			dfs(v,u,o+op[u].size()-1);
		}
	}
}

//unsigned
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
		op[y].push_back(x);
	}
	dfs(1,0,1);
	ma=0;
	dfs(id,0,1);
    if(m==0) ma--;
	cout<<ma+1;
	return 0;
}

E

结论:

无论是图的直径还是树的直径

如果要链接x和y两个连通块得到的新直径最小:

答案只有3种情况

1.链接x的直径的中心点和y的直径的中心点

2.还是原来的x的直径

3.还是原来的x的直径

————————————

搭配并查集就行

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int N=3e5+10;
int n,m,q;
int maxx,id;
int fa[N],dp[N];
vector<int> op[N];

int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}

void bvvd(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y) fa[x]=y;
}


void dfs(int u,int fa,int o){
	if(o>maxx){
		maxx=o;
		id=u;
	}
	for(int i=0;i<op[u].size();i++){
		int v=op[u][i];
		if(v!=fa){
			dfs(v,u,o+1);
		}
	}
}

//unsigned
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m>>q;
	
	for(int i=1;i<=n;i++) fa[i]=i;
	
	
	
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
		op[y].push_back(x);
		bvvd(x,y);
	}
	
	
	for(int i=1;i<=n;i++){
		if(fa[i]==i){
			maxx=id=0;
			dfs(i,0,0);
			int st=id;
			maxx=0;
			dfs(st,0,0);
			dp[i]=maxx;
		}
	}
	
	while(q--){
		int qa,x,y;
		cin>>qa>>x;
		if(qa==1){
            cout<<dp[find(x)]<<"\n"; 
            x=x;
        }else{
			cin>>y;
			x=find(x);
			y=find(y);
			if(x==y) continue;
			int bmpt=max(max(dp[x],dp[y]), (dp[x]+1)/2+(dp[y]+1)/2+1);
			fa[x]=y;
			dp[y]=bmpt;
		}
	}
	return 0;
}
原文