Home BOJ. excellence town (1949)
Post
Cancel

BOJ. excellence town (1949)

[Link] https://www.acmicpc.net/problem/1949


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	static int[] fdp;
	static int[] edp;
	static boolean[] visit;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = toi(br.readLine());
		int[] arr;
		Node[] nodes = new Node[n];
		fdp = new int[n];
		edp = new int[n];
		Arrays.fill(fdp, 1);
		visit = new boolean[n];
		for(int i = 0; i < n; i++) nodes[i] = new Node(i);
		for(int i = 0; i < n - 1; i++) {
			arr = getArr();
			int a = arr[0] - 1, b = arr[1] - 1;
			nodes[a].list.add(nodes[b]);
			nodes[b].list.add(nodes[a]);
		}
		nodes[0].parent = -1;
		bfs(nodes[0]);
		int ans = 0;
		println(edp[0] == 0 ? fdp[0] : Math.min(edp[0], fdp[0]));
	}

	static void bfs(Node node) {
		if(node.list.size() == 1 && node.parent != -1) {
			return;
		}
		for(Node child: node.list) {
			if(child.idx == node.parent) continue;
			child.parent = node.idx;
			if(!visit[child.idx]) bfs(child);
			fdp[node.idx] += Math.min(fdp[child.idx], edp[child.idx]);
			edp[node.idx] += fdp[child.idx];
		}
		visit[node.idx] = true;
	}

	static class Node {
		int idx;
		int parent;
		boolean evenIdx = false;
		ArrayList<Node> list = new ArrayList<>();
		Node(int idx) { this.idx = idx; }
	}


	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.