fork(1) download
  1.  
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. class PT07Z
  6. {
  7.  
  8. static HashMap<Integer,ArrayList> map=new HashMap<Integer,ArrayList>();
  9. static BitSet visited=new BitSet();
  10.  
  11. public static void main(String args[])
  12. throws IOException
  13. {
  14. int n=Integer.parseInt(br.readLine().trim());
  15. //long start=System.nanoTime();
  16. for(int i=0;i<(n-1);i++)
  17. {
  18. String in[]=br.readLine().trim().split(" ");
  19. int n1=Integer.parseInt(in[0]);
  20. int n2=Integer.parseInt(in[1]);
  21.  
  22. if(map.containsKey(n1))
  23. {
  24. ArrayList recv=map.get(n1);
  25. recv.add(n2);
  26. map.put(n1,recv);
  27. }
  28. else
  29. {
  30. ArrayList st=new ArrayList();
  31. st.add(n2);
  32. map.put(n1, st);
  33. }
  34. if(map.containsKey(n2))// ki kore??
  35. {
  36. ArrayList recv=map.get(n2);
  37. recv.add(n1);
  38. map.put(n2, recv);
  39. }
  40. else
  41. {
  42. ArrayList st=new ArrayList();
  43. st.add(n1);
  44. map.put(n2, st);
  45. }
  46.  
  47. //
  48. }
  49. int first_end_vertex=bfs_first(1);
  50. //System.out.println(first_end_vertex);
  51. visited.clear();
  52. int max_length=bfs_second(first_end_vertex);
  53. System.out.println(max_length-1);
  54. //System.out.println((System.nanoTime()-start)/1000000000.0);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. static int bfs_first(int start)
  62. {
  63. int max=Integer.MIN_VALUE;
  64. Queue<Integer> queue=new LinkedList();
  65. queue.add(start);
  66. visited.set(start);
  67. int node=0,level=0,lastnode=0,no_of_children=0;
  68. while(!queue.isEmpty())
  69. {
  70. node=queue.remove();
  71. //finding out all the unvisited child nodes adjacent to node
  72. int child=0;
  73. while((child=get_unvisited_childnode(node))!=0)
  74. {
  75. visited.set(child);
  76. queue.add(child);
  77. no_of_children++;
  78. }
  79. if((queue.size()-no_of_children==0))
  80. {
  81. level++;
  82. no_of_children=0;
  83. }
  84. if(level>max)
  85. {
  86. max=level;
  87. lastnode=node;
  88. }
  89. }
  90. return lastnode;
  91. }
  92.  
  93.  
  94. static int bfs_second(int start)
  95. {
  96. int max=Integer.MIN_VALUE;
  97. Queue<Integer> queue=new LinkedList();
  98. queue.add(start);
  99. visited.set(start);
  100. int node=0,level=0,lastnode=0,no_of_children=0;
  101. while(!queue.isEmpty())
  102. {
  103. node=queue.remove();
  104. //finding out all the unvisited child nodes adjacent to node
  105. int child=0;
  106. while((child=get_unvisited_childnode(node))!=0)
  107. {
  108. visited.set(child);
  109. queue.add(child);
  110. no_of_children++;
  111. }
  112. if((queue.size()-no_of_children==0))
  113. {
  114. level++;
  115. no_of_children=0;
  116. }
  117. if(level>max)
  118. {
  119. max=level;
  120. lastnode=node;
  121. }
  122. }
  123. return level;
  124. }
  125.  
  126.  
  127. static int get_unvisited_childnode(int node)
  128. {
  129. ArrayList al=map.get(node);
  130. Iterator<Integer> list_iterator=al.iterator();
  131. while(list_iterator.hasNext())
  132. {
  133. int hold=list_iterator.next();
  134. if(!visited.get(hold))
  135. {
  136. //now mark the node as visited
  137. visited.set(hold);
  138. return hold;
  139. }
  140. }
  141. return 0;
  142. }
  143. }
Success #stdin #stdout 0.07s 380224KB
stdin
3
1 2
2 3
stdout
2