teodor_dalavera

Компоненти на сврзаност

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