笔试题整理 第三题

 

 

#include <iostream>
#include <vector>

using namespace std;
bool flag_fir = true;

int count_sec(vector<vector<int>> all_sec,vector<int> cur_set)
{
    int len = cur_set.size();
    int num = 0;

    if (len == 0)
        return 1;
    else if (flag_fir)
    {
        num++;
        flag_fir = false;
    }
        

    for (int i = 0; i < len; i++)
        num += count_sec(all_sec, all_sec[cur_set[i]]);

    return num;
}

int main() {

    int n;
    cin >> n;
    vector<vector<int>>all_sec(n);
    for (int i = 0; i < n-1; i++)
    {
        int x, y;
        cin >> x;
        cin >> y;
        all_sec[y - 1].push_back(x - 1);
    }

    int res = 0;
    int len = all_sec[0].size();

    for (int i = 0; i < len; i++)
    {
        flag_fir = true;
        int cur_res = count_sec(all_sec, all_sec[all_sec[0][i]]);
        if (cur_res > res)
            res = cur_res;
    }

    cout << res+1;

    system("pause");
    return 0;
}

 分析:

后来经过推演,认为和1节点连接的所有节点中,求出所有分枝个数最大值,再加1即可。

这个程序有个小毛病,就是默认输入第一个节点值永远大于第二个。

目测没问题,一些案例也通过,但是由于当时没写出来,所以没法真正验证。

转载于:https://www.cnblogs.com/CJT-blog/p/10714119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值