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 javaapplication39;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import static java.lang.Math.abs;
- import static java.lang.Math.abs;
- import java.util.Arrays;
- import java.lang.Object;
- import java.util.Collections;
- import java.util.Comparator;
- public class JavaApplication39 {
- static int[] cesta;
- public static int[] komponenty;
- static int velikost, pocetVrcholu, pocetHran, i, j, delka;
- public static void main(String args[]) throws IOException {
- int[] pole;
- //File f = new File("a.txt");
- //BufferedReader in = new BufferedReader(new FileReader(f));
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- String line = in.readLine();// preskoceni radku
- velikost = Integer.parseInt(line);
- delka = velikost * velikost;
- pole = new int[delka];
- int index = 0;
- while ((line = in.readLine()) != null) {
- String[] casti = line.split(" ");
- for (int i = 0; i < velikost; i++) {
- pole[index] = Integer.parseInt(casti[i]);
- index++;
- }
- if (index == delka) {
- break;
- }
- }
- pocetVrcholu = delka;
- komponenty = new int[pocetVrcholu];
- for (int i = 0; i < komponenty.length; i++) {
- komponenty[i] = i;
- }
- pocetHran = (velikost + velikost) * (velikost - 1);
- cesta = new int[pocetVrcholu + 1];
- Hrana e[] = new Hrana[pocetHran];
- Hrana t = new Hrana();
- int hrana = 0;
- for (int i = 1; i < pole.length; i++) {
- if (i % velikost != 0) {
- e[hrana] = new Hrana();
- e[hrana].odkud = i - 1;
- e[hrana].kam = i;
- e[hrana].vaha = abs(pole[i] - pole[i - 1]);
- hrana++;
- }
- if (i >= velikost) {
- e[hrana] = new Hrana();
- e[hrana].odkud = i - velikost;
- e[hrana].kam = i;
- e[hrana].vaha = abs(pole[i] - pole[i - velikost]);
- hrana++;
- }
- }
- Arrays.sort(e);
- for (i = 0; i < pocetVrcholu; i++) {
- cesta[i] = 0;
- }
- i = 0;
- j = 0;
- Hrana kostra[] = new Hrana[pocetVrcholu - 1];
- int max = 0;
- while ((i != pocetVrcholu - 1) && (j != pocetHran)) {
- if (jeCyklus(e[j])) {
- kostra[i] = new Hrana();
- kostra[i].odkud = e[j].odkud;
- kostra[i].kam = e[j].kam;
- kostra[i].vaha = e[j].vaha;
- max = e[j].vaha;
- i++;
- }
- j++;
- }
- for (int i = 0; i < kostra.length; i++) {
- if (max != kostra[i].vaha) {
- pridatHranu(kostra[i].odkud, kostra[i].kam);
- }
- }
- Vypis(komponenty, velikost);
- }
- public static boolean jeCyklus(Hrana e) {
- int u = e.odkud, v = e.kam;
- while (cesta[u] > 0) {
- u = cesta[u];
- }
- while (cesta[v] > 0) {
- v = cesta[v];
- }
- if (u != v) {
- cesta[u] = v;
- return true;
- }
- return false;
- }
- static class Hrana implements Comparable<Hrana>{
- int odkud, kam, vaha;
- public Integer getVaha() {
- return vaha;
- }
- @Override
- public int compareTo(Hrana o) {
- return this.getVaha().compareTo(o.getVaha());
- }
- }
- public static void Vypis(int[] pole, int delka) {
- System.out.println(delka);
- for (int i = 0; i < pole.length; i++) {
- if (pole[i] == 0) {
- System.out.printf(" 0 ");
- } else {
- System.out.printf(" 1 ");
- }
- if ((i + 1) % velikost == 0) {
- System.out.println("");
- }
- }
- }
- public static void pridatHranu(int index1, int index2) {
- int temp = komponenty[index1];
- int temp2 = komponenty[index2];
- for (int i = 0; i < komponenty.length; i++) {
- if (komponenty[i] == temp2) {
- komponenty[i] = temp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement