mvCode

DedoMrazIRibite AIPS Дедо Мраз на распуст

Jan 19th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 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. boolean visited[] = new boolean[num_nodes]; // cuvam dali nekoj node e poseten
  186. for ( int i = 0; i < num_nodes; i++ ) {
  187. visited[i] = false;
  188. }
  189.  
  190. LinkedQueue< Integer > queue = new LinkedQueue();
  191. queue.enqueue( start );
  192. visited[ start ] = true;
  193. int numLakes = 0;
  194.  
  195. while ( !queue.isEmpty() ) {
  196. int temp = queue.dequeue(); // momentalen jazol
  197. for ( int i = 0; i < num_nodes; i++ ) {
  198. if( adjMat[temp][i] != 1 || visited[i]) // ako ne e soseden ili e prethodno poseten prodolzi so sleden i
  199. continue;
  200. // ako e soseden i neposeten:
  201. queue.enqueue( i );
  202. visited[i] = true;
  203. numLakes++;
  204. }
  205. }
  206. return numLakes;
  207. }
  208. }
  209.  
  210. public class DedoMrazIRibite {
  211.  
  212. public static void main(String[] args) {
  213. Scanner in = new Scanner(System.in);
  214. int N = in.nextInt();
  215. int U = in.nextInt();
  216. Graph<Integer> g = new Graph<>(N);
  217. for (int i = 0; i < U; i++) {
  218. int r = in.nextInt();
  219. int q = in.nextInt();
  220. g.addEdge(r, q);
  221.  
  222. }
  223. int L = in.nextInt();
  224. System.out.println(g.sendFishes(L));
  225. System.out.println("Srekna Nova Godina");
  226. in.close();
  227. }
  228.  
  229. }
Add Comment
Please, Sign In to add comment