Guest User

Untitled

a guest
Jun 17th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Test {
  7. public static void main(String args[]) throws IOException {
  8. new Test(new Scanner(System.in));
  9. }
  10.  
  11. Graph g;
  12. // functions as defined above
  13. int []dp1, dp2;
  14. int []C;
  15.  
  16. // pV is parent of node V
  17. void dfs(int V, int pV) {
  18. // for storing sums of dp1 and max(dp1, dp2) for all children of V
  19. int sum1=0, sum2=0;
  20.  
  21. // traverse over all children
  22. for(Edge v : g.adj[V]) {
  23. if(v.j == pV) continue;
  24. dfs(v.j, V);
  25. sum1 += dp2[v.j];
  26. sum2 += Math.max(dp1[v.j], dp2[v.j]);
  27. }
  28.  
  29. dp1[V] = C[V] + sum1;
  30. dp2[V] = sum2;
  31. }
  32.  
  33. Test(Scanner in) throws IOException {
  34. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  35. dp1 = new int[50];
  36. dp2 = new int[50];
  37. g = new Graph(in.nextInt()+1);
  38. int M = in.nextInt();
  39. C = new int[] { 1, 0, 0, 22, 22, 0, 0 };
  40.  
  41. while(M-->0)
  42. {
  43. int i = in.nextInt();
  44. int j = in.nextInt();
  45. g.add(i, j);
  46. }
  47.  
  48. dfs(1, 0);
  49. int ans = Math.max(dp1[1], dp2[1]);
  50.  
  51. System.out.println(ans);
  52. }
  53.  
  54. class Graph
  55. {
  56. int N, M;
  57.  
  58. ArrayList<Edge>[] adj;
  59.  
  60. Graph(int NN)
  61. {
  62. M = 0;
  63. adj = new ArrayList[N=NN];
  64. for(int i=0; i<N; i++)
  65. adj[i] = new ArrayList<>();
  66. }
  67.  
  68. void add(int i, int j)
  69. {
  70. adj[i].add(new Edge(j, M));
  71. adj[j].add(new Edge(i, M));
  72. M++;
  73. }
  74. }
  75.  
  76. class Edge
  77. {
  78. int j, id;
  79.  
  80. Edge(int jj, int ii) {
  81. j=jj; id=ii;
  82. }
  83. }
  84. }
Add Comment
Please, Sign In to add comment