AcWing 846. 树的重心(DFS)

题目连接:https://www.acwing.com/problem/content/description/848/

错误代码分析:
重点就在于这句话:
t2 += son[e[j]];
并不是每一个子节点都应该被加上,这个过程只能在深搜时进行,因为st数组在深搜后就无效了,所以这么写为错的。

错误代码:

import java.util.*;

public class Main {
    static int N = (int) 1e5 + 10;
    static int[] h = new int[N], ne = new int[N], e = new int[N];
    static int[] st = new int[N], son = new int[N];
    
    static int idx = 0;
    
    static {
        Arrays.fill(h, -1);
    }
    
    static void add(int x, int y) {
        e[idx] = y;
        ne[idx] = h[x];
        h[x] = idx ++;
    }
    
    static void dfs(int x) {
        st[x] = 1;
        son[x] ++;
        for (int i = h[x]; i != -1; i = ne[i]) {
            int y = e[i];
            if(st[y] == 0) {
                dfs(y);
                son[x] += son[y];
            }
        }
    }
    
    public static  void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        for (int i = 0; i < n - 1; i ++ ) {
            int x = sc.nextInt(), y = sc.nextInt();
            add(x, y);
            add(y, x);
        }
        
        dfs(1);
        
        // for (int i = 1; i <= n; i ++) 
        //     System.out.println(i + ": " + son[i]);
            
        // System.out.println("------------------------------------");
        
        int res = N;
        for (int i = 1; i <= n; i ++) {
            int t = -1, t2  = 0;
            for (int j = h[i]; j != -1; j = ne[j]) {
                t2 += son[e[j]];
                t = Integer.max(t, son[e[j]]);
            }
            // System.out.println(i + ": " + t);
            t = Integer.max(t, n - t2 - 1);
            // System.out.println(i + ": " + t);
            res = Integer.min(res, t);
        }
        System.out.println(res);
    }
}

AC代码:

import java.util.*;

public class Main {
    static int N = (int) 1e5 + 10;
    static int[] h = new int[N], ne = new int[N * 2], e = new int[N * 2];
    static int[] st = new int[N];
    
    static int idx = 0, res = N, n;
    
    static {
        Arrays.fill(h, -1);
    }
    
    static void add(int x, int y) {
        e[idx] = y;
        ne[idx] = h[x];
        h[x] = idx ++;
    }
    
    static int dfs(int x) {
        st[x] = 1;
        int tn = 0, tmax = -1;
        for (int i = h[x]; i != -1; i = ne[i]) {
            int y = e[i];
            if(st[y] == 0) {
                int t = dfs(y);
                tn += t;
                tmax = Integer.max(tmax, t);
            }
        }
        
        tmax = Integer.max(tmax, n - tn - 1);
        
        res = Integer.min(res, tmax);
        
        return tn + 1;
    }
    
    public static  void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        
        for (int i = 0; i < n - 1; i ++ ) {
            int x = sc.nextInt(), y = sc.nextInt();
            add(x, y);
            add(y, x);
        }
        
        dfs(1);
        
        
        System.out.println(res);
    }
}
上一篇:846. 一手顺子


下一篇:95-846-820-源码-网络-Flink 网络传输优化技术