Filip_Markoski

[ADSx] - Santa Claus on Holiday

Jan 15th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.87 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.NoSuchElementException;
  3. import java.util.Scanner;
  4.  
  5. class SLLNode<E> {
  6.     protected E element;
  7.     protected SLLNode<E> succ;
  8.  
  9.     public SLLNode(E elem, SLLNode<E> succ) {
  10.         this.element = elem;
  11.         this.succ = succ;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return element.toString();
  17.     }
  18. }
  19.  
  20. // Implementacija na redica
  21.  
  22. interface Queue<E> {
  23.  
  24.     // Elementi na redicata se objekti od proizvolen tip.
  25.  
  26.     // Metodi za pristap:
  27.  
  28.     public boolean isEmpty();
  29.  
  30.     // Vrakja true ako i samo ako redicata e prazena.
  31.  
  32.     public int size();
  33.  
  34.     // Ja vrakja dolzinata na redicata.
  35.  
  36.     public E peek();
  37.  
  38.     // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  39.  
  40.     // Metodi za transformacija:
  41.  
  42.     public void clear();
  43.  
  44.     // Ja prazni redicata.
  45.  
  46.     public void enqueue(E x);
  47.  
  48.     // Go dodava x na kraj od redicata.
  49.  
  50.     public E dequeue();
  51.     // Go otstranuva i vrakja pochetniot element na redicata.
  52.  
  53. }
  54.  
  55. class LinkedQueue<E> implements Queue<E> {
  56.  
  57.     // Redicata e pretstavena na sledniot nacin:
  58.     // length go sodrzi brojot na elementi.
  59.     // Elementite se zachuvuvaat vo jazli dod SLL
  60.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  61.     SLLNode<E> front, rear;
  62.     int length;
  63.  
  64.     // Konstruktor ...
  65.  
  66.     public LinkedQueue() {
  67.         clear();
  68.     }
  69.  
  70.     public boolean isEmpty() {
  71.         // Vrakja true ako i samo ako redicata e prazena.
  72.         return (length == 0);
  73.     }
  74.  
  75.     public int size() {
  76.         // Ja vrakja dolzinata na redicata.
  77.         return length;
  78.     }
  79.  
  80.     public E peek() {
  81.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  82.         if (front == null)
  83.             throw new NoSuchElementException();
  84.         return front.element;
  85.     }
  86.  
  87.     public void clear() {
  88.         // Ja prazni redicata.
  89.         front = rear = null;
  90.         length = 0;
  91.     }
  92.  
  93.     public void enqueue(E x) {
  94.         // Go dodava x na kraj od redicata.
  95.         SLLNode<E> latest = new SLLNode<E>(x, null);
  96.         if (rear != null) {
  97.             rear.succ = latest;
  98.             rear = latest;
  99.         } else
  100.             front = rear = latest;
  101.         length++;
  102.     }
  103.  
  104.     public E dequeue() {
  105.         // Go otstranuva i vrakja pochetniot element na redicata.
  106.         if (front != null) {
  107.             E frontmost = front.element;
  108.             front = front.succ;
  109.             if (front == null)
  110.                 rear = null;
  111.             length--;
  112.             return frontmost;
  113.         } else
  114.             throw new NoSuchElementException();
  115.     }
  116.  
  117. }
  118.  
  119. class Graph<E> {
  120.  
  121.     int num_nodes; // broj na jazli
  122.     E nodes[]; // informacija vo jazlite - moze i ne mora?
  123.     int adjMat[][]; // matrica na sosednost
  124.  
  125.     @SuppressWarnings("unchecked")
  126.     public Graph(int num_nodes) {
  127.         this.num_nodes = num_nodes;
  128.         nodes = (E[]) new Object[num_nodes];
  129.         adjMat = new int[num_nodes][num_nodes];
  130.  
  131.         for (int i = 0; i < this.num_nodes; i++)
  132.             for (int j = 0; j < this.num_nodes; j++)
  133.                 adjMat[i][j] = 0;
  134.     }
  135.  
  136.     int adjacent(int x, int y) { // proveruva dali ima vrska od jazelot so
  137.         // indeks x do jazelot so indeks y
  138.         return (adjMat[x][y] != 0) ? 1 : 0;
  139.     }
  140.  
  141.     void addEdge(int x, int y) { // dodava vrska megu jazlite so indeksi x i y
  142.         adjMat[x][y] = 1;
  143.     }
  144.  
  145.     void deleteEdge(int x, int y) {
  146.         // ja brise vrskata megu jazlite so indeksi x i y
  147.         adjMat[x][y] = 0;
  148.         adjMat[y][x] = 0;
  149.     }
  150.  
  151.     E get_node_value(int x) { // ja vraka informacijata vo jazelot so indeks x
  152.         return nodes[x];
  153.     }
  154.  
  155.     void set_node_value(int x, E a) { // ja postavuva informacijata vo jazelot
  156.         // so indeks na a
  157.         nodes[x] = a;
  158.     }
  159.  
  160.     @Override
  161.     public String toString() {
  162.         String ret = "  ";
  163.         for (int i = 0; i < num_nodes; i++)
  164.             ret += nodes[i] + " ";
  165.         ret += "\n";
  166.         for (int i = 0; i < num_nodes; i++) {
  167.             ret += nodes[i] + " ";
  168.             for (int j = 0; j < num_nodes; j++)
  169.                 ret += adjMat[i][j] + " ";
  170.             ret += "\n";
  171.         }
  172.         return ret;
  173.     }
  174.  
  175.     public void printMatrix() {
  176.         for (int i = 0; i < num_nodes; i++) {
  177.             for (int j = 0; j < num_nodes; j++)
  178.                 System.out.print(adjMat[i][j] + " ");
  179.             System.out.println();
  180.         }
  181.     }
  182.  
  183.     int sendFishes(int start) {
  184.         //VASIOT KOD OVDE, ILI TAMU, VAS IZBOR
  185.         /**
  186.          * https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
  187.          * It's like BFS for a graph
  188.          * but with the added predicate of the previous being adjacent with the current
  189.          * adjacent(dequeue, i) == 1
  190.          * */        
  191.        
  192.         boolean[] visited = new boolean[num_nodes];
  193.         visited[start] = true;
  194.         Queue<Integer> nodes = new LinkedQueue<>();
  195.         nodes.enqueue(start);
  196.         int sum = 0;
  197.         while (!nodes.isEmpty()) {
  198.             Integer dequeue = nodes.dequeue();
  199.             for (int i = 0; i < num_nodes; i++) {
  200.                 if (!visited[i] && adjacent(dequeue, i) == 1) {
  201.                     nodes.enqueue(i);
  202.                     visited[i] = true;
  203.                     sum++;
  204.                 }
  205.             }
  206.         }
  207.         return sum;
  208.     }
  209. }
  210.  
  211. public class DedoMrazIRibite {
  212.  
  213.     public static void main(String[] args) {
  214.         Scanner in = new Scanner(System.in);
  215.         int N = in.nextInt();
  216.         int U = in.nextInt();
  217.         Graph<Integer> g = new Graph<>(N);
  218.         for (int i = 0; i < U; i++) {
  219.             int r = in.nextInt();
  220.             int q = in.nextInt();
  221.             g.addEdge(r, q);
  222.  
  223.         }
  224.  
  225.         //System.out.println(g.toString());
  226.  
  227.         int L = in.nextInt();
  228.         System.out.println(g.sendFishes(L));
  229.         System.out.println("Srekna Nova Godina");
  230.         in.close();
  231.     }
  232.  
  233. }
  234. /**
  235.  * Santa Claus has a hobby of breeding fish.
  236.  * He keeps them in natural lakes which are connected between with rivers. The fish can go from one lake to another using the rivers.
  237.  * Each spring, Santa Claus wants to breed new fish.
  238.  * You task is to tell Santa Claus, if he releases fish in lake X, what other lakes can the fish go to, using the rivers, eliminating the need to release fish in those lakes, too.
  239.  * <p>
  240.  * Input: the first line contains the number N < 15, number of lakes.
  241.  * The second line the number U < 20, the number of rivers.
  242.  * The next U lines contain two numbers R, Q, ids of the lakes, which are connected by a river.
  243.  * The last line contains the number L, the lake where Santa will release the new fish.
  244.  * <p>
  245.  * Output: The first line contains the number of lakes which the fish can go to
  246.  * (excluding the initial). The second line should print "Srekna Nova Godina"
  247.  */
Advertisement
Add Comment
Please, Sign In to add comment