zoukankan      html  css  js  c++  java
  • 笔试题整理 第三题

     

    #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即可。

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

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

  • 相关阅读:
    Dll版本管理
    线程池ThreadPool
    关于sitemesh和freemark在struts2中的一些问题总结
    Google 怎么搜索
    android 设计模式
    android webview
    ios 基础数据类型
    android 常用
    android Handler vs Timer
    网站
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10714119.html
Copyright © 2011-2022 走看看