Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.PrintWriter;
  7. import java.math.BigInteger;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.BitSet;
  11. import java.util.Comparator;
  12. import java.util.HashMap;
  13. import java.util.HashSet;
  14. import java.util.LinkedList;
  15. import java.util.Queue;
  16. import java.util.Random;
  17. import java.util.Scanner;
  18. import java.util.Set;
  19. import java.util.Stack;
  20. import java.util.StringTokenizer;
  21.  
  22. public class Contest{
  23. static Random random;
  24. private static void mySort(int[] s) {
  25. for (int i = 0; i < s.length; ++i) {
  26. int j = random.nextInt(i + 1);
  27. int t = s[i];
  28. s[i] = s[j];
  29. s[j] = t;
  30. }
  31. Arrays.sort(s);
  32. }
  33.  
  34. public static HashMap<Long,Boolean> visited;
  35.  
  36. public static void main(String[] args) {
  37. random = new Random(543534151132L + System.currentTimeMillis());
  38. InputStream inputStream = System.in;
  39. OutputStream outputStream = System.out;
  40. InputReader in = new InputReader(inputStream);
  41. PrintWriter out = new PrintWriter(outputStream);
  42.  
  43.  
  44. int nb=in.nextInt();
  45.  
  46. visited=new HashMap<Long,Boolean>();
  47. HashMap<Long,ArrayList<Long>> Graph=new HashMap<Long,ArrayList<Long>>();
  48. for(int i=0;i<nb;i++) {
  49. long x=in.nextLong();
  50. visited.put(x, false);
  51. Graph.put(x,new ArrayList<Long>());
  52. for(long j : Graph.keySet()) {
  53. if(j!=x && (j*2==x || (j%3==0 && j/3==x))) {
  54. Graph.get(j).add(x);
  55. }else if(j!=x && (x*2==j || (x%3==0 && x/3==j))) {
  56. Graph.get(x).add(j);
  57. }
  58. }
  59. }
  60.  
  61.  
  62. for(long i: Graph.keySet()) {
  63. long count=0;
  64. for(long j: Graph.keySet()) {
  65. if(j!=i && Graph.get(j).contains(i))count++;
  66. }
  67. //searching for degree 0 to start dfs
  68. if(count==0) {
  69. dfs(Graph,i);
  70. break;
  71. }
  72. }
  73.  
  74.  
  75. out.close();
  76.  
  77. }
  78.  
  79.  
  80.  
  81. private static void dfs(HashMap<Long, ArrayList<Long>> graph, long vertex) {
  82. visited.put(vertex,true);
  83. System.out.print(vertex+" ");
  84. for(long node: graph.get(vertex)) {
  85. if(!visited.get(node)) {
  86. dfs(graph,node);
  87. }
  88. }
  89.  
  90. }
  91.  
  92. public static int gcd(int a, int b)
  93. {
  94. BigInteger b1 = BigInteger.valueOf(a);
  95. BigInteger b2 = BigInteger.valueOf(b);
  96. BigInteger gcd = b1.gcd(b2);
  97. return gcd.intValue();
  98. }
  99.  
  100. public static long gcd(long a, long b)
  101. {
  102. BigInteger b1 = BigInteger.valueOf(a);
  103. BigInteger b2 = BigInteger.valueOf(b);
  104. BigInteger gcd = b1.gcd(b2);
  105. return gcd.longValue();
  106. }
  107.  
  108. static class InputReader {
  109. public BufferedReader reader;
  110. public StringTokenizer tokenizer;
  111.  
  112. public InputReader(InputStream stream) {
  113. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  114. tokenizer = null;
  115. }
  116.  
  117. public String next() {
  118. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  119. try {
  120. tokenizer = new StringTokenizer(reader.readLine());
  121. } catch (IOException e) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. return tokenizer.nextToken();
  126. }
  127.  
  128. public int nextInt() {
  129. return Integer.parseInt(next());
  130. }
  131.  
  132. public long nextLong() {
  133. return Long.parseLong(next());
  134. }
  135.  
  136. public double nextDouble() {
  137. return Double.parseDouble(next());
  138. }
  139.  
  140. public float nextFloat() {
  141. return Float.parseFloat(next());
  142. }
  143.  
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement