Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.BitSet;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.Stack;
- import java.util.StringTokenizer;
- public class Contest{
- static Random random;
- private static void mySort(int[] s) {
- for (int i = 0; i < s.length; ++i) {
- int j = random.nextInt(i + 1);
- int t = s[i];
- s[i] = s[j];
- s[j] = t;
- }
- Arrays.sort(s);
- }
- public static HashMap<Long,Boolean> visited;
- public static void main(String[] args) {
- random = new Random(543534151132L + System.currentTimeMillis());
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- InputReader in = new InputReader(inputStream);
- PrintWriter out = new PrintWriter(outputStream);
- int nb=in.nextInt();
- visited=new HashMap<Long,Boolean>();
- HashMap<Long,ArrayList<Long>> Graph=new HashMap<Long,ArrayList<Long>>();
- for(int i=0;i<nb;i++) {
- long x=in.nextLong();
- visited.put(x, false);
- Graph.put(x,new ArrayList<Long>());
- for(long j : Graph.keySet()) {
- if(j!=x && (j*2==x || (j%3==0 && j/3==x))) {
- Graph.get(j).add(x);
- }else if(j!=x && (x*2==j || (x%3==0 && x/3==j))) {
- Graph.get(x).add(j);
- }
- }
- }
- for(long i: Graph.keySet()) {
- long count=0;
- for(long j: Graph.keySet()) {
- if(j!=i && Graph.get(j).contains(i))count++;
- }
- //searching for degree 0 to start dfs
- if(count==0) {
- dfs(Graph,i);
- break;
- }
- }
- out.close();
- }
- private static void dfs(HashMap<Long, ArrayList<Long>> graph, long vertex) {
- visited.put(vertex,true);
- System.out.print(vertex+" ");
- for(long node: graph.get(vertex)) {
- if(!visited.get(node)) {
- dfs(graph,node);
- }
- }
- }
- public static int gcd(int a, int b)
- {
- BigInteger b1 = BigInteger.valueOf(a);
- BigInteger b2 = BigInteger.valueOf(b);
- BigInteger gcd = b1.gcd(b2);
- return gcd.intValue();
- }
- public static long gcd(long a, long b)
- {
- BigInteger b1 = BigInteger.valueOf(a);
- BigInteger b2 = BigInteger.valueOf(b);
- BigInteger gcd = b1.gcd(b2);
- return gcd.longValue();
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public float nextFloat() {
- return Float.parseFloat(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement