fork download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. class implemento {
  7. static int n;
  8. static int k;
  9. static ArrayList<Integer> child[];
  10. static long default_[][];
  11. public static void main(String[] args)throws IOException {
  12. String p[]=br.readLine().split(" ");
  13. n=Integer.parseInt(p[0]);
  14. k=Integer.parseInt(p[1]);
  15. //stores the adjacency list for the tree
  16. ArrayList<Integer> adjacency[]=new ArrayList[n];
  17.  
  18. for(int i=0;i<n;i++) adjacency[i]=new ArrayList<Integer>();
  19. //reading edges (w,v) for the input tree
  20. for(int i=1;i<n;i++){
  21. String p1[]=br.readLine().split(" ");
  22. int v=Integer.parseInt(p1[0])-1;
  23. int w=Integer.parseInt(p1[1])-1;
  24. adjacency[v].add(w);
  25. adjacency[w].add(v);
  26. }
  27. //lines 29 through initialize the given tree as a linked data structure
  28. Stack<Integer> temp=new Stack<Integer>();
  29. temp.push(0);
  30. boolean trav[]=new boolean[n];
  31. trav[0]=true;
  32. child=new ArrayList[n];
  33. for(int i=0;i<n;i++){
  34. child[i]=new ArrayList<Integer>();
  35. }
  36. int parent[]=new int[n];
  37. parent[0]=-1;
  38. while(temp.size()>0){
  39. int x=temp.peek();
  40. int l2=adjacency[x].size();boolean change =false;
  41. for(int u=0;u<l2;u++){
  42. int v=adjacency[x].get(u);
  43. if(!trav[v]){
  44. parent[v]=x;
  45. temp.push(v);
  46. change=true;
  47. trav[v]=true;
  48. break;
  49. }
  50. }
  51. if(!change){temp.pop();}
  52. }
  53. for(int i=0;i<n;i++){
  54. if(parent[i]>=0){
  55. child[parent[i]].add(i);}
  56. }
  57. GenericTreeNode root=initialize(0);
  58. default_=new long[2][k+1];
  59. default_[0][0]=1;
  60. default_[1][0]=1;
  61. long a[][]=getSubtreeInclude_Exclude(root);
  62. //a[0] is the exclude array
  63. //a[1] is the include array
  64. long sum=0;
  65. for(int i=0;i<=k;i++){
  66. sum+=a[0][i]+a[1][i];
  67. }
  68. System.out.println(sum);
  69. for(int i=0;i<2;i++){
  70. for(int j=0;j<=k;j++)
  71. System.out.print(a[i][j]+" ");
  72. System.out.println();
  73.  
  74. }
  75. }
  76. //function to initialise the given tree
  77. static GenericTreeNode initialize(int root){
  78. if(child[root].size()==0){
  79. return new GenericTreeNode(root);
  80. }
  81. else{int l=child[root].size();
  82. GenericTreeNode temp=new GenericTreeNode(root);
  83. for(int i=0;i<l;i++){
  84. GenericTreeNode t=initialize(child[root].get(i));
  85. temp.children.add(t);
  86. }
  87. return temp;
  88. }
  89. }
  90. // display the given tree
  91. static void display(GenericTreeNode root){
  92. System.out.println(root.node);
  93. if(!(root.children.size()==0||root.children==null))
  94. {
  95. int l=root.children.size();
  96. for(int i=0;i<l;i++){
  97. display(root.children.get(i));
  98. }
  99. }
  100. }
  101. //finds the number of valid subtrees
  102. static long[][] getSubtreeInclude_Exclude(GenericTreeNode root){
  103. if(root.children.size()==0||root.children==null) {
  104. //base case when tree is just a node with no children
  105. //return include as[1,0,0,0...]
  106. //return exclude as[1,0,0,0...]
  107. return default_;
  108. }
  109. else{
  110. GenericTreeNode L=root.children.get(0);
  111. root.children.remove(L);
  112. long l[][]=getSubtreeInclude_Exclude(L);
  113. long r[][]=getSubtreeInclude_Exclude(root);
  114. long tvInclude[]=new long[k+1];
  115. long tvExclude[]=new long[k+1];
  116. tvExclude[0]=1;
  117. tvInclude[0]=1;
  118. for(int i=1;i<=k;i++){
  119. tvExclude[i]=l[0][i]+r[1][i-1]+r[0][i];
  120. }
  121. for(int i=1;i<=k;i++){
  122. long sum=0;
  123. for(int j=0;j<=i;j++){
  124. int k1=i-j;
  125. sum+=(l[1][j]*r[1][k1]);
  126. }
  127. tvInclude[i]=l[1][i-1]+sum;
  128. }
  129. long ret[][]=new long[2][k+1];
  130. ret[0]=tvExclude;
  131. ret[1]=tvInclude;
  132. //return the include/exclude arrays for this step
  133. return ret;
  134. }
  135. }
  136. }
  137. class GenericTreeNode{
  138. int node;
  139. ArrayList<GenericTreeNode> children;
  140. GenericTreeNode(){
  141. children=new ArrayList<GenericTreeNode>();
  142. node=0;
  143. }
  144. GenericTreeNode(int n){
  145. children=new ArrayList<GenericTreeNode>();
  146. node=n;
  147. }
  148.  
  149. }
  150.  
Success #stdin #stdout 0.08s 381184KB
stdin
4 4
1 2
1 3
1 4
stdout
11
1  3  3  0  0  
1  3  0  0  0