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

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

通过率挺低的,应该是那个大数据的测试用例,内存超出的问题卡住了,最大数据是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;
}
时间: 2024-08-24 20:01:55

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

PAT 1021. Deepest Root (25)

1021. Deepest Root (25) A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root

1021. Deepest Root (25) 并查集&amp;&amp;DFS

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root t

1021 Deepest Root (25 分)(经典搜索)

1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest roo

1021 Deepest Root (25 分)

1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest roo

1021. Deepest Root (25)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E

PAT (Advanced Level) 1021. Deepest Root (25)

先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[20000]; int n,sz

PAT 1021 Deepest Root

#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class Node { public: vector<int> adj; bool visited; Node() : visited(false) {} }; void reset_nodes(vector<Node>& nodes) { int len = nodes.size();

PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E

1021 Deepest Root [DFS]

这题一开始没认真看题,直接看样例以为求叶子结点和几棵树.题目的真正的意思是给你一张无环连通图,以任意点为根节点可以把该图看成一棵树,并且如果当前树的深度最大,就称这个点是Deepest Root,也就是题目所要求的(升序输出).如果这个图有多个连通分量,求连通分量的个数.DFS可以通吃,详见代码- #include <bits/stdc++.h> #define maxn 100005 #define INF 0x3f3f3f3f using namespace std; typedef lo