fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3.  
  4. /*
  5.  
  6. 7
  7. 6
  8. 1 2
  9. 1 3
  10. 2 4
  11. 2 5
  12. 3 6
  13. 3 7
  14.  
  15. case 2:-
  16. 2
  17. 1
  18. 1 2
  19.  
  20. cas 3:-
  21. 1
  22. 0
  23.  
  24. */
  25.  
  26.  
  27. import java.util.*;
  28. import java.lang.*;
  29. import java.io.*;
  30.  
  31. /* Name of the class has to be "Main" only if the class is public. */
  32. class Ideone
  33. {
  34. public static void main (String[] args) throws java.lang.Exception
  35. {
  36. Scanner sc = new Scanner(System.in);
  37. int n = sc.nextInt();
  38. int m = sc.nextInt();
  39. List<List<Integer>> adj = new ArrayList<>();
  40. for(int i=0; i<=n;i++){
  41. adj.add(new ArrayList<>());
  42. }
  43.  
  44. for(int i=0; i<m; i++){
  45. int u = sc.nextInt();
  46. int v = sc.nextInt();
  47. adj.get(u).add(v);
  48. adj.get(v).add(u);
  49. }
  50. int []parent = new int[adj.size()];
  51. boolean []visited = new boolean[adj.size()];
  52.  
  53. // List<Integer> leaves = printLeafdfs(1,adj,parent,visited);
  54. // System.out.println(leaves);
  55. int[] child = new int[adj.size()];
  56. printLeafdfs(1,adj,parent,visited,child);
  57.  
  58. for(int i=1;i<child.length;i++){
  59. if(child[i] == 0){
  60. System.out.print(i);
  61. }
  62. }
  63. }
  64.  
  65.  
  66. public static void printLeafdfs(int node,List<List<Integer>> adj,int []par,boolean []vis,int []child){
  67. vis[node] = true;
  68. for(int x : adj.get(node)){
  69. if(vis[x] == false){
  70. par[x] = node;
  71. child[node] =child[node]+1;
  72. printLeafdfs(x,adj,par,vis,child);
  73. }
  74.  
  75. }
  76.  
  77. }
  78. }
Success #stdin #stdout 0.11s 56552KB
stdin
7
6
1 2
1 3
2 4
2 5
3 6
3 7
stdout
4567