PAT-1021. Deepest Root (25)-DFS求树的最大深度

本文探讨了在一个不保证连通性的图中,计算从任一节点出发的最大树深度的方法。通过使用深度优先搜索(DFS)算法,并采用邻接表而非邻接矩阵保存图结构,有效解决了大数据测试用例下的内存超限问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题目主要是给你一个图,那么计算从任何一点开始,以此为根节点,树的最大深度。不保证图的连通性。

通过率挺低的,应该是那个大数据的测试用例,内存超出的问题卡住了,最大数据是10^4,如果用邻接矩阵的形式保存图形,那么将是n*n的空间复杂度,就是10^8*4B个数据,为4*10^5KB内存,题目是3.2*10^5KB内存,所以内存超出。卡在内存上了。所以我们想到的是用邻接表来保存图,但是我们实现的时候不用链表,而是用向量数组的形式来保存图,这个方式极好,要常用。

思路就是用dfs来搜索每一个节点的最大深度。有些人提到用并查集来检查图形的连通性,这样会增加复杂度,本来dfs就可以用来检测图形的连通性,为什么还要用并查集来排除呢,直接上dfs就可以了。

//
#include<stdio.h>
#include<string>
#include<iostream>
#include<vector>
using namespace std;
#define MAX 10001
int maxInt=0xffffffff>>1;
bool visited[MAX];
int height[MAX];
int map[MAX][MAX];
vector<int> adj[MAX];
int n;
int dfs(int node)
{
    bool flag=false;
    int max=-1;
    visited[node]=true;
    vector<int>::iterator it=adj[node].begin();
    for(;it!=adj[node].end();it++)
    {
        int i=*it;
        if(!visited[i])
        {
            flag=true;
            int temp=dfs(i);//subtree max height
            //printf("node %d: %d\n",i,temp);
            if(max<temp)
              max=temp;
        }
    }
    if(!flag)
      return 1;
    return max+1;
}

void init()
{
    for(int i=1;i<=n;i++)
    {
        visited[i]=false;
    }
}
int dfsg()
{
    int count=0;
    for(int i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            count++;
            height[i]=dfs(i);
        }
    }
    return count;
}

int main()
{
    while(cin>>n)
    {
        int a,b;
        visited[n]=false;
        for(int i=1;i<=n;i++)
        {
            adj[i].clear();
        }
        for(int i=1;i<=n-1;i++)
        {
            visited[i]=false;
            cin>>a>>b;
            //map[a][b]=map[b][a]=1;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        bool iserr=false;
        int count=1;
        int max_height=-1;
        for(int i=1;i<=n;i++)
        {
            init();
            height[i]=dfs(i);
            if(height[i]>max_height)
                max_height=height[i];
            for(int j=1;j<=n;j++)
            {
                if(!visited[j])
                {
                    count++;
                    dfs(j);
                }
            }
            if(count>1)
            {
              iserr=true;
              break;
            }
        }
        if(iserr)
            printf("Error: %d components\n",count);
        else
        {
            for(int i=1;i<=n;i++)
            {
                if(height[i]==max_height)
                    printf("%d\n",i);
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/championlai/p/4008451.html

def search_astra_datalog( directory: str = os.getcwd(), filelist: list = [], depth: int = 0, max_depth: int = 10, ) -> list: """ Traverse the files and folders from the current working directory downwards to find and collect astra data log. Parameters ---------- directory : str, default current working directory Used to specify the absolute starting directory path. By default, it starts from the directory path where the script is run, and you can also customize the directory path. filelist : list, default [] The directory path used to store the astra data log from the collected. The default is an empty list, or you can use a defined list. depth : int, default 0 Used to specify the starting directory level. If you know the exact starting directory level, you can define it here to reduce operating time. max_depth : int, default 10 Used to specify the ending directory level. You can specify the deepest directory level so that you can end the traversal early. Returns ------- list The returns contains the absolute directory path of all astra data logs. Notes ----- ...... """ new_directory = directory if os.path.isfile(directory): if ( "csv" in directory and not "event" in directory and not "tar.gz" in directory and "#" in directory ): filelist.append(directory) elif os.path.isdir(directory): if depth < max_depth: try: for s in os.listdir(directory): new_directory = os.path.join(directory, s) search_astra_datalog(new_directory, filelist, depth + 1, max_depth) except PermissionError: print(f"Access denied to directoryectory: {directory}") return filelist
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值