【2019京东笔试】—— AcWing 681. 疏散人群

【题目描述】
在这里插入图片描述在这里插入图片描述

DFS写法

【思路】
左右子树节点个数较大者即为最短疏散时间

//左右子树节点个数较大者
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    static int N = 100010;
    static int M = 100010 * 2;
    static int e[] = new int[M]; 
    static int ne[] = new int[M];
    static int h[] = new int[N];
    static int idx;
    
    public static void add(int a, int b){
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        idx ++;
    }
    
    
    //以u为根节点的树的节点数
    public static int dfs(int u, int father){
        
        int res = 1;
        for(int i = h[u]; i != - 1; i = ne[i]){
            int son  = e[i];
            if( son == father ) continue;
            res += dfs(son, u);
        }
        
        return res;
        
        
    }
    public static void main(String args[]){
        Scanner reader = new Scanner(System.in);
        int n =  reader.nextInt();
        
        //千万要记得初始化头结点
        Arrays.fill(h, -1);
        for(int i = 0; i < n - 1; i ++){
            //双向建图
            int a = reader.nextInt(), b = reader.nextInt();
            add(a, b);
            add(b, a);
        }
        int res = 0;
        //计算以1号节点为根的所有子树的节点数的最大值
        for(int i = h[1]; i != - 1; i = ne[i])
            res = Math.max(dfs(e[i], 1), res);
        
        System.out.println(res);
    }
}

BFS 写法

//左右子树节点个数较大者
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    static int N = 100010;
    static int M = 100010 * 2;      // 数组开小会 越界后出现一些莫名的错误
    static int e[] = new int[M]; 
    static int ne[] = new int[M];
    static int h[] = new int[N];
    static int idx;
    
    static boolean st[] = new boolean[N];  //标记数组
    
    //队列
    static int hh = 0, tt = -1;
    static int q[] = new int[N];
    
    public static void add(int a, int b){
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        idx ++;
    }
    
    //计算以u为根节点的子树的节点数
    public static int bfs(int u){
        
        // Arrays.fill(st, false);
        
        int hh = 0, tt = -1;
        q[ ++ tt] = u;
        st[ u ] = true;
        
        while( hh <= tt){//队列非空
            
            //取队首
            int t =  q[hh ++];
            
            
            //扩展相邻节点
            //值为t的节点的相邻节点
            for(int i = h[t]; i != -1; i = ne[i]){
                int j =  e[i];
                if( !st[j] ){//未被访问过
                    //入队列
                    q[ ++ tt] =  j;
                    st[j] = true;
                    
                }
            }
            
        }
        
        //以u为根节点的树的节点数目为队列中元素的个数
        return tt + 1;
        
        
    }
    
    
    
    public static void main(String args[]){
        Scanner reader = new Scanner(System.in);
        int n =  reader.nextInt();
        
        //千万要记得初始化头结点
        Arrays.fill(h, -1);
        for(int i = 0; i < n - 1; i ++){
            //双向建图
            int a = reader.nextInt(), b = reader.nextInt();
            add(a, b);
            add(b, a);
        }
        int res = 0;
        
        //注意要标记1号节点已经访问过了
        st[1] = true;
        //计算以1号节点为根的所有子树的节点数的最大值
        for(int i = h[1]; i != - 1; i = ne[i])
            res = Math.max(bfs(e[i]), res);
        
        System.out.println(res);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值