PAT甲级1021题,部分测试点无法通过

#include <cstdio>
#include <vector>
#include <algorithm>

namespace PATA1021{
    using namespace std;

    const int MAXN=10001;

    int n,maxH=0;
    int father[MAXN];
    bool isRoot[MAXN];
    vector<int> adt[MAXN],ans,temp;

    int findFather(int v){
        int ori=v;
        while(ori!=father[ori]){
            ori=father[ori];
        }
        while(v!=father[v]){
            int tempV=v;
            v=father[v];
            father[tempV]=ori;
        }
        return ori;
    }

    void unionSet(int s1,int s2){
        int fa1=findFather(s1);
        int fa2=findFather(s2);
        if(fa1!=fa2){
            father[fa2]=fa1;
        }
    }

    void init(){
        for(int i=1;i<=n;i++){
            father[i]=i;
            isRoot[i]=false;
        }
    }

    int calBlocks(){
        int blocks=0;
        for(int i=1;i<=n;i++){
            isRoot[findFather(i)]=true;
        }
        for(int i=1;i<=n;i++){
            if(isRoot[i]){
                blocks++;
            }
        }
        return blocks;
    }

    void dfs(int now,int pre,int h){
        if(h>=maxH){
            if(h>maxH){
                maxH=h;
                temp.clear();
            }
            temp.push_back(now);
        }
        for(int i=0;i<adt[now].size();i++){
            if(adt[now][i]!=pre){
                dfs(adt[now][i],now,++h);
            }
        }
    }
}

       红色字标记处是测试点无法通过的地方,只要把 ++h 换成 h+1 即可通过全部测试点,这是因为h是要传递下去的不能随便更改值,调试了好一会儿,才明白过来。

      如果本篇文章对你有帮助的话,请不要吝惜你的赞哦( ̄▽ ̄)~*

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值