牛客-桃花——一个计算多叉树直径的问题

桃花

题目链接

题目解析

这题有点坑,我开始以为是直接找相同的一堆直接一下扫荡,具体来说就是找所有相连在一起的点有多少,然后我很快想到用并查集的方式去写这道题,但一经提交就直接WA了。然后查阅资料,再看这道题,终于是理解了它的意思。。。它是需要找到这颗树上的一条“关系链”,也就是求多叉树的直径

以下为我并查集的代码,虽然过不了,但换成无向图就应该过了。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class UnionFind{
    int* root;
// 添加了 rank 数组来记录每个顶点的高度,也就是每个顶点的「秩」
    int* rank;
    int length;
public:
    ll* cnt;
    ll ret;
    UnionFind(int size):length(size){
        root = new int[size];
        rank = new int[size];
        cnt = new ll[size];
        for(int i=0;i<length;i++){
            root[i] = i;
            rank[i] = 1;
            cnt[i]=1;
        }
        ret = 1;
    }
    int find(int x){
        if(x==root[x])
            return x;
        return root[x]=find(root[x]);
    }
    // 按秩合并优化的 merge 函数
    void merge(int x,int y){
        int rootX = find(x);
        int rootY = find(y);
        //高度小的树被高度大的合并,如果高度一致合并后高度增加
        if(rootX!=rootY){
            if(rank[rootX]>rank[rootY]){
                root[rootY] = rootX;
            } else if(rank[rootX]<rank[rootY]){
                root[rootX] = rootY;
            }else{
                root[rootY] = rootX;
                rank[rootX]++;
            }
            ll sum_cnt = cnt[rootX] +cnt[rootY];
            cnt[rootX] = cnt[rootY] = sum_cnt;
            ret = max(ret,sum_cnt);
        }
    }
    bool isconnected(int x,int y){
        return find(x)==find(y);
    }
};

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    UnionFind check(n+1);
    int a,b;
    for (int i = 0; i < n - 1; ++i) {
        cin>>a>>b;
        check.merge(a,b);
    }
    cout<<check.ret;
    return 0;
}

后面稍微查了下多叉树的直径怎么求:

大概以下几步

  1. 首先,根据父子关系及边权重构建无向图

  2. 然后,随机找一顶点,利用深度优先搜索找到距离该点最远的顶点remote

  3. 最后,从remote顶点开始深度优先搜索找到最长路径,该路径即为直径

解题代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<int>* graph; //TODO 如果在意效率的话,可以用链式前向星

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    graph = new vector<int>[n+1];
    int a,b;
    for (int i = 1; i < n; ++i) {
        cin>>a>>b;
        graph[a].push_back(b);  //TODO 构建无向图
        graph[b].push_back(a);
    }
    int step_ret = 0;   //TODO 记录直径长度
    int remote = 0;     //TODO 记录距离随便选择的点的最远点
    bool check[n+1];    //TODO 防止走回头路
    //TODO dfs仿函数定义
    function<void(int,int)>dfs = [&](int root,int step){
        check[root] = true;
        if(step_ret<step){
            step_ret = step;
            remote = root;
        }
        for(auto i:graph[root]){
            if(!check[i])
                dfs(i,step+1);
        }
    };
    //TODO 正式的dfs过程
    memset(check,0,sizeof(check));
    dfs(1,1);
    memset(check,0,sizeof(check));
    dfs(remote,1);
    cout<<step_ret;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值