Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. package sdk.backjun.dp;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * 백준, 2533, 다이나믹 프로그래밍
  8. * 사회망 서비스(SNS)
  9. *
  10. * @author whitebeard-k
  11. *
  12. */
  13.  
  14. public class Problem2533 {
  15.  
  16. static int N;
  17.  
  18. static int[] visit;
  19. static int[][] dp;
  20. static LinkedList<Integer>[] list;
  21.  
  22. public static void main(String[] args) {
  23.  
  24. Scanner sc = new Scanner(System.in);
  25. N = sc.nextInt();
  26.  
  27. visit = new int[N + 1];
  28. dp = new int[N + 1][2];
  29. list = new LinkedList[N + 1];
  30.  
  31. for (int i = 1; i <= N; i++)
  32. list[i] = new LinkedList<>();
  33.  
  34. for (int i = 0; i < N - 1; i++) {
  35. int a = sc.nextInt();
  36. int b = sc.nextInt();
  37.  
  38. list[a].add(b);
  39. list[b].add(a);
  40. }
  41.  
  42. int start = 1;
  43. dfs(start);
  44. System.out.println(Math.min(dp[start][0], dp[start][1]));
  45. }
  46.  
  47. public static void dfs(int index) {
  48.  
  49. visit[index] = 1;
  50. dp[index][0] = 0;
  51. dp[index][1] = 1;
  52.  
  53. LinkedList<Integer> item = list[index];
  54.  
  55. for (int to : item) {
  56.  
  57. if (visit[to] == 0) {
  58. dfs(to);
  59. dp[index][0] += dp[to][1];
  60. dp[index][1] += Math.min(dp[to][0], dp[to][1]);
  61. }
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment