Advertisement
Dim4eBeBrat

Komponenti na svrzanost

Jan 5th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.LinkedList;
  7.  
  8. class SLLNode<E> {
  9.     protected E element;
  10.     protected SLLNode<E> succ;
  11.  
  12.     public SLLNode(E elem, SLLNode<E> succ) {
  13.         this.element = elem;
  14.         this.succ = succ;
  15.     }
  16.  
  17.     @Override
  18.     public String toString() {
  19.         return element.toString();
  20.     }
  21. }
  22.  
  23. // Implementacija na redica
  24.  
  25. interface Queue<E> {
  26.  
  27.     // Elementi na redicata se objekti od proizvolen tip.
  28.  
  29.     // Metodi za pristap:
  30.  
  31.     public boolean isEmpty ();
  32.         // Vrakja true ako i samo ako redicata e prazena.
  33.  
  34.     public int size ();
  35.         // Ja vrakja dolzinata na redicata.
  36.  
  37.     public E peek ();
  38.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  39.  
  40.     // Metodi za transformacija:
  41.  
  42.     public void clear ();
  43.         // Ja prazni redicata.
  44.  
  45.     public void enqueue (E x);
  46.         // Go dodava x na kraj od redicata.
  47.  
  48.     public E dequeue ();
  49.         // Go otstranuva i vrakja pochetniot element na redicata.
  50.  
  51. }
  52.  
  53. class LinkedQueue<E> implements Queue<E> {
  54.  
  55.     // Redicata e pretstavena na sledniot nacin:
  56.     // length go sodrzi brojot na elementi.
  57.     // Elementite se zachuvuvaat vo jazli dod SLL
  58.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  59.     SLLNode<E> front, rear;
  60.     int length;
  61.  
  62.     // Konstruktor ...
  63.  
  64.     public LinkedQueue () {
  65.         clear();
  66.     }
  67.  
  68.     public boolean isEmpty () {
  69.         // Vrakja true ako i samo ako redicata e prazena.
  70.         return (length == 0);
  71.     }
  72.  
  73.     public int size () {
  74.         // Ja vrakja dolzinata na redicata.
  75.         return length;
  76.     }
  77.  
  78.     public E peek () {
  79.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  80.         if (front == null)
  81.             throw new NoSuchElementException();
  82.         return front.element;
  83.     }
  84.  
  85.     public void clear () {
  86.         // Ja prazni redicata.
  87.         front = rear = null;
  88.         length = 0;
  89.     }
  90.  
  91.     public void enqueue (E x) {
  92.         // Go dodava x na kraj od redicata.
  93.         SLLNode<E> latest = new SLLNode<E>(x, null);
  94.         if (rear != null) {
  95.             rear.succ = latest;
  96.             rear = latest;
  97.         } else
  98.             front = rear = latest;
  99.         length++;
  100.     }
  101.  
  102.     public E dequeue () {
  103.         // Go otstranuva i vrakja pochetniot element na redicata.
  104.         if (front != null) {
  105.             E frontmost = front.element;
  106.             front = front.succ;
  107.             if (front == null)  rear = null;
  108.             length--;
  109.             return frontmost;
  110.         } else
  111.             throw new NoSuchElementException();
  112.     }
  113.  
  114. }
  115.  
  116. class GraphNode<E> {
  117.     private int index;//index (reden broj) na temeto vo grafot
  118.     private E ime;
  119.     private E prezime;
  120.     private LinkedList<GraphNode<E>> neighbors;
  121.    
  122.     public GraphNode(int index, E ime, E prezime) {
  123.         this.index = index;
  124.         this.ime = ime;
  125.         this.prezime = prezime;
  126.         neighbors = new LinkedList<GraphNode<E>>();
  127.     }
  128.    
  129.     public GraphNode(int index) {
  130.         this.index = index;
  131.        
  132.         neighbors = new LinkedList<GraphNode<E>>();
  133.     }
  134.    
  135.     boolean containsNeighbor(GraphNode<E> o){
  136.         return neighbors.contains(o);
  137.     }
  138.    
  139.     void addNeighbor(GraphNode<E> o){
  140.         neighbors.add(o);
  141.     }
  142.    
  143.     void removeNeighbor(GraphNode<E> o){
  144.         if(neighbors.contains(o))
  145.             neighbors.remove(o);
  146.     }
  147.  
  148.    
  149.  
  150.     public int getIndex() {
  151.         return index;
  152.     }
  153.  
  154.     public void setIndex(int index) {
  155.         this.index = index;
  156.     }
  157.  
  158.    
  159.  
  160.  
  161.     public LinkedList<GraphNode<E>> getNeighbors() {
  162.         return neighbors;
  163.     }
  164.  
  165.     public void setNeighbors(LinkedList<GraphNode<E>> neighbors) {
  166.         this.neighbors = neighbors;
  167.     }
  168. }
  169.  
  170. class Graph<E> {
  171.  
  172.     int num_nodes;
  173.     GraphNode<E> adjList[];
  174.  
  175.    
  176.  
  177.     @SuppressWarnings("unchecked")
  178.     public Graph(int num_nodes) {
  179.         this.num_nodes = num_nodes;
  180.         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  181.         for (int i = 0; i < num_nodes; i++)
  182.             adjList[i] = new GraphNode<E>(i);
  183.     }
  184.  
  185.     int adjacent(int x, int y) {
  186.         // proveruva dali ima vrska od jazelot so
  187.         // indeks x do jazelot so indeks y
  188.         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  189.     }
  190.  
  191.     void addEdge(int x, int y) {
  192.         // dodava vrska od jazelot so indeks x do jazelot so indeks y
  193.         if (!adjList[x].containsNeighbor(adjList[y])) {
  194.             adjList[x].addNeighbor(adjList[y]);
  195.         }
  196.     }
  197.  
  198.     void deleteEdge(int x, int y) {
  199.         adjList[x].removeNeighbor(adjList[y]);
  200.     }
  201.    
  202.     int [] najdikomponenti(int start){
  203.      
  204.         //vasiot kod tuka
  205.          ArrayList<Integer> poseteni=new ArrayList<>();
  206.         poseteni.add(start);
  207.         poseteni=najdikomponentiR(start,poseteni);
  208.         int [] a=new int[poseteni.size()];
  209.         Collections.sort(poseteni);
  210.        for(int i=0;i<poseteni.size();i++){
  211.            a[i]=poseteni.get(i);
  212.        }
  213.         return a;  
  214.     }
  215.    
  216.     ArrayList<Integer> najdikomponentiR(int start,ArrayList<Integer> poseteni){
  217.         ArrayList<Integer> rez=new ArrayList<>();
  218.         for(int i=0;i<num_nodes;i++){
  219.             if(adjacent(start,i)>0&&!poseteni.contains(i)){
  220.                 poseteni.add(i);
  221.              
  222.                 rez = najdikomponentiR(i,poseteni);
  223.                 for(int j=0;j<rez.size();j++){
  224.                    if(poseteni.contains(rez.get(j))){
  225.                       // System.out.println("VEKJ POSETENO A NAJDENO KAKO INDIREKTNO: "+rez.get(j));
  226.                    }
  227.                    else{
  228.                        poseteni.add(rez.get(j));
  229.                    }
  230.                 }
  231.             }
  232.         }
  233.         return poseteni;
  234.     }
  235.    
  236.  
  237.     @Override
  238.     public String toString() {
  239.         String ret = new String();
  240.         for (int i = 0; i < this.num_nodes; i++)
  241.             ret += i + ": " + adjList[i] + "\n";
  242.         return ret;
  243.     }
  244.  
  245. }
  246.  
  247.  
  248. public class KomponentiSvrzanost {
  249.    
  250.     public static void main(String[] args) throws Exception {
  251.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  252.         int N = Integer.parseInt(br.readLine());
  253.        
  254.        
  255.         Graph<String> g = new Graph<String>(N);
  256.        
  257.         int sosedIndex;
  258.         int i_num_nodes;
  259.         String pom;
  260.         for(int i=0;i<N;i++)
  261.         {
  262.             i_num_nodes = Integer.parseInt(br.readLine());
  263.             for(int j=0;j<i_num_nodes;j++)
  264.             {
  265.                 pom = br.readLine();
  266.                 sosedIndex = Integer.parseInt(pom);
  267.            
  268.                 g.addEdge(i,sosedIndex);                  
  269.             }
  270.         }      
  271.        
  272.        
  273.         int teme = Integer.parseInt(br.readLine());
  274.        int[] komponenti =g.najdikomponenti(teme);
  275.       //vasiot kod tuka
  276.          for(int i=0;i<komponenti.length;i++){
  277.             System.out.println(komponenti[i]);
  278.         }
  279.        
  280.         br.close();
  281.     }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement