Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package alg;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Collections;
  16. import java.util.Comparator;
  17.  
  18. /**
  19.  *
  20.  * @author chlupnoha
  21.  */
  22. public class Main {
  23.  
  24.     static ArrayList<ArrayList<Integer>> list = new ArrayList<>();
  25.     static ArrayList<int[]> result = new ArrayList<>();
  26.  
  27.     public static void main(String[] args) throws FileNotFoundException, IOException {
  28.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  29. //        BufferedReader in = new BufferedReader(new FileReader("/home/chlupnoha/Plocha/pub/pub04.in"));
  30.  
  31.         int N, M;
  32.         String[] line = in.readLine().trim().split(" ");
  33.         N = Integer.parseInt(line[0]);
  34.         M = Integer.parseInt(line[1]);
  35.  
  36.         for (int i = 0; i < N; i++) {
  37.             list.add(new ArrayList());
  38.         }
  39.  
  40.         String[] s;
  41.         int a1, a2;
  42.         for (int i = 0; i < M; i++) {
  43.             ArrayList tempList = new ArrayList();
  44.  
  45.             s = in.readLine().split(" ");
  46.             a1 = Integer.parseInt(s[0]);
  47.             a2 = Integer.parseInt(s[1]);
  48.  
  49.             tempList = list.get(a1);
  50.             tempList.add(a2);
  51.             list.set(a1, tempList);
  52.  
  53.             tempList = list.get(a2);
  54.             tempList.add(a1);
  55.             list.set(a2, tempList);
  56.         }
  57.  
  58. //        printInput();
  59.         ArrayList<Integer> tmp;
  60.         for (int i = 0; i < N; i++) {
  61.             tmp = list.get(i);
  62.             for (int q : tmp) {
  63.                 if (q > i) {
  64.                     findStar(i, q, 1, 1, new int[]{i, q, -1, -1, -1});
  65.                 }
  66.             }
  67.         }
  68.         printRes();
  69. //        System.out.println(result.size());
  70.     }
  71.  
  72.     public static void printInput() {
  73.         int count = 0;
  74.         for (ArrayList<Integer> array : list) {
  75.             System.out.print("c " + count + ": ");
  76.             for (Integer i : array) {
  77.                 System.out.print(i + " ");
  78.             }
  79.             System.out.println("");
  80.             count++;
  81.         }
  82.     }
  83.  
  84.     static int problem = 0;
  85.  
  86.     public static void findStar(int starting, int curr, int dioda, int pos, int[] visited) {
  87.         problem++;
  88.         System.out.print(problem + " starting:" + starting + " actual:" + curr + " pos: " + pos + " visited: ");
  89.         for (Integer v : visited) {
  90.             System.out.print(v + " ");
  91.         }
  92.         System.out.println("");
  93.  
  94.         visited[pos] = curr;
  95.  
  96.         //princip diody na duplikace by Evzen
  97.         int down = Integer.MAX_VALUE;
  98.         if (dioda > 1) {
  99.             down = curr;
  100.         }
  101.         for (int hrana : list.get(curr)) {
  102.             if (hrana > starting && hrana < down) {
  103.  
  104.                 int count = neighbor(visited, hrana, pos);
  105.  
  106.                 if (pos < 3 && count > 0) {
  107.                     continue;
  108.                 }
  109.                 if (pos == 3 && count != 1) {
  110.                     continue;
  111.                 }
  112.  
  113.                 if (pos == 3) {
  114.                     visited[pos + 1] = hrana;
  115.                     if (list.get(starting).contains(hrana)) {
  116.                         int[] nove = Arrays.copyOf(visited, visited.length);
  117.                         Arrays.sort(nove);
  118.                         Arrays.sort(nove);
  119.                         result.add(nove);
  120. //                        System.out.println("add");
  121.                     }
  122.                     visited[pos + 1] = 0;
  123.                 } else {
  124.                     if (hrana > curr) {
  125.                         dioda++;
  126.                     }
  127.  
  128. //                    int[] nove = new int[5];
  129. //                    System.arraycopy(visited, 0, nove, 0, pos + 1);
  130. //                    nove[pos + 1] = hrana;
  131.                     findStar(starting, hrana, dioda, pos + 1, visited);
  132.  
  133.                     if (hrana > curr) {
  134.                         dioda--;
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.         visited[pos] = 0;
  140.  
  141.     }
  142.  
  143.     public static int neighbor(int[] vis, int hrana, int pos) {
  144.         int count = 0;
  145.         for (int i = 0; i < pos; i++) {
  146.             if (list.get(hrana).contains(vis[i])) {
  147.                 count++;
  148.             }
  149.         }
  150.         return count;
  151.     }
  152.  
  153.     public static void printRes() {
  154.         Collections.sort(result, new ArrayComparator());
  155.         for (int[] array : result) {
  156.             for (Integer i : array) {
  157.                 System.out.print(i + " ");
  158.             }
  159.             System.out.println("");
  160.         }
  161.     }
  162.  
  163.     static class ArrayComparator implements Comparator<int[]> {
  164.  
  165.         @Override
  166.         public int compare(int[] t, int[] t1) {
  167.             for (int i = 0; i < 5; i++) {
  168.                 if (t[i] < t1[i]) {
  169.                     return -1;
  170.                 }
  171.                 if (t[i] > t1[i]) {
  172.                     return 1;
  173.                 }
  174.             }
  175.             return 0;
  176.         }
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement