zoukankan      html  css  js  c++  java
  • PAT(Advanced Level)A1021. Deepest Root

    题意

    找到一个点,使得以它为根结点的树最高。如果不唯一,按照结点的数字升序排列。如果不存在,就输出连通块个数

    思路

    • 首先先统计一下连通块个数,如果大于1个,那么图不连通,不会构成树
    • 高度最高的树,按照我的理解就是用DFS,看谁能递归到最深的层数。用一个数组ans保存所有点的最大深度,最后进行比较的时候输出

    代码

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<map>
    using namespace std;
    const int MAXV = 10010;
    vector<int> adj[MAXV];
    int ans[MAXV];
    bool vis[MAXV];
    int nodes, blocks;
    int cur_level;
    void dfs(int u, int depth)
    {
        vis[u] = true;
        for(int i=0;i<adj[u].size();i++)
        {
            int ta = adj[u][i];
            if(!vis[ta])
                dfs(ta, depth + 1);
        }
        if(depth > cur_level)   cur_level = depth;	//到最深处返回的时候进行比较
        return;
    }
    
    void dfs_travel()
    {
        
        for(int i=1;i<=nodes;i++)   //每次从一个新的顶点出发进行遍历
        {
            if(!vis[i])
            {
                dfs(i, 0);
                blocks++;
            }
        }
    }
    int get_blocks()
    {
        memset(vis, false, sizeof(vis));
        vis[0] = true;
        blocks = 0;
        dfs_travel();
        return blocks;
    }    //先看有几个连通块
    int main()
    {
        scanf("%d", &nodes);
        int u, v;
        for(int i=0;i<nodes-1;i++)
        {
            scanf("%d %d", &u, &v);
            adj[u].emplace_back(v);
            adj[v].emplace_back(u);
        }
        get_blocks();
        if(blocks > 1)  //>1个连通块
        {
            printf("Error: %d components
    ", blocks);
        }else{    //确保是树之后再对每个点来一次DFS
            for(int i=1;i<=nodes;i++)
            {
                memset(vis, false, sizeof(vis));
                cur_level = 0;
                dfs(i, 0);
                ans[i] = cur_level;
            }
            int max_value = -1;
            for(int i=1;i<=nodes;i++)
                if(ans[i] > max_value)
                    max_value = ans[i];
            for(int i=1;i<=nodes;i++)
            {
                if(ans[i] == max_value)
                    cout << i << endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    BZOJ 1003 物流运输
    549565
    26566
    68
    554554
    5656
    49886
    5989
    6898
    656
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/13721964.html
Copyright © 2011-2022 走看看