Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package alg;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- /**
- *
- * @author chlupnoha
- */
- public class Main {
- static ArrayList<ArrayList<Integer>> list = new ArrayList<>();
- static ArrayList<int[]> result = new ArrayList<>();
- public static void main(String[] args) throws FileNotFoundException, IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- // BufferedReader in = new BufferedReader(new FileReader("/home/chlupnoha/Plocha/pub/pub04.in"));
- int N, M;
- String[] line = in.readLine().trim().split(" ");
- N = Integer.parseInt(line[0]);
- M = Integer.parseInt(line[1]);
- for (int i = 0; i < N; i++) {
- list.add(new ArrayList());
- }
- String[] s;
- int a1, a2;
- for (int i = 0; i < M; i++) {
- ArrayList tempList = new ArrayList();
- s = in.readLine().split(" ");
- a1 = Integer.parseInt(s[0]);
- a2 = Integer.parseInt(s[1]);
- tempList = list.get(a1);
- tempList.add(a2);
- list.set(a1, tempList);
- tempList = list.get(a2);
- tempList.add(a1);
- list.set(a2, tempList);
- }
- // printInput();
- ArrayList<Integer> tmp;
- for (int i = 0; i < N; i++) {
- tmp = list.get(i);
- for (int q : tmp) {
- if (q > i) {
- findStar(i, q, 1, 1, new int[]{i, q, -1, -1, -1});
- }
- }
- }
- printRes();
- // System.out.println(result.size());
- }
- public static void printInput() {
- int count = 0;
- for (ArrayList<Integer> array : list) {
- System.out.print("c " + count + ": ");
- for (Integer i : array) {
- System.out.print(i + " ");
- }
- System.out.println("");
- count++;
- }
- }
- static int problem = 0;
- public static void findStar(int starting, int curr, int dioda, int pos, int[] visited) {
- problem++;
- System.out.print(problem + " starting:" + starting + " actual:" + curr + " pos: " + pos + " visited: ");
- for (Integer v : visited) {
- System.out.print(v + " ");
- }
- System.out.println("");
- visited[pos] = curr;
- //princip diody na duplikace by Evzen
- int down = Integer.MAX_VALUE;
- if (dioda > 1) {
- down = curr;
- }
- for (int hrana : list.get(curr)) {
- if (hrana > starting && hrana < down) {
- int count = neighbor(visited, hrana, pos);
- if (pos < 3 && count > 0) {
- continue;
- }
- if (pos == 3 && count != 1) {
- continue;
- }
- if (pos == 3) {
- visited[pos + 1] = hrana;
- if (list.get(starting).contains(hrana)) {
- int[] nove = Arrays.copyOf(visited, visited.length);
- Arrays.sort(nove);
- Arrays.sort(nove);
- result.add(nove);
- // System.out.println("add");
- }
- visited[pos + 1] = 0;
- } else {
- if (hrana > curr) {
- dioda++;
- }
- // int[] nove = new int[5];
- // System.arraycopy(visited, 0, nove, 0, pos + 1);
- // nove[pos + 1] = hrana;
- findStar(starting, hrana, dioda, pos + 1, visited);
- if (hrana > curr) {
- dioda--;
- }
- }
- }
- }
- visited[pos] = 0;
- }
- public static int neighbor(int[] vis, int hrana, int pos) {
- int count = 0;
- for (int i = 0; i < pos; i++) {
- if (list.get(hrana).contains(vis[i])) {
- count++;
- }
- }
- return count;
- }
- public static void printRes() {
- Collections.sort(result, new ArrayComparator());
- for (int[] array : result) {
- for (Integer i : array) {
- System.out.print(i + " ");
- }
- System.out.println("");
- }
- }
- static class ArrayComparator implements Comparator<int[]> {
- @Override
- public int compare(int[] t, int[] t1) {
- for (int i = 0; i < 5; i++) {
- if (t[i] < t1[i]) {
- return -1;
- }
- if (t[i] > t1[i]) {
- return 1;
- }
- }
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement