teodor_dalavera

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

Dec 23rd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.47 KB | None | 0 0
  1. package DedoMrazIRibite;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.NoSuchElementException;
  7. import java.util.Stack;
  8.  
  9. class LinkedQueue<E> implements Queue<E> {
  10.  
  11.     // Redicata e pretstavena na sledniot nacin:
  12.     // length go sodrzi brojot na elementi.
  13.     // Elementite se zachuvuvaat vo jazli dod SLL
  14.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  15.     SLLNode<E> front, rear;
  16.     int length;
  17.  
  18.     // Konstruktor ...
  19.  
  20.     public LinkedQueue () {
  21.         clear();
  22.     }
  23.  
  24.     public boolean isEmpty () {
  25.         // Vrakja true ako i samo ako redicata e prazena.
  26.         return (length == 0);
  27.     }
  28.  
  29.     public int size () {
  30.         // Ja vrakja dolzinata na redicata.
  31.         return length;
  32.     }
  33.  
  34.     public E peek () {
  35.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  36.         if (front == null)
  37.             throw new NoSuchElementException();
  38.         return front.element;
  39.     }
  40.  
  41.     public void clear () {
  42.         // Ja prazni redicata.
  43.         front = rear = null;
  44.         length = 0;
  45.     }
  46.  
  47.     public void enqueue (E x) {
  48.         // Go dodava x na kraj od redicata.
  49.         SLLNode<E> latest = new SLLNode<E>(x, null);
  50.         if (rear != null) {
  51.             rear.succ = latest;
  52.             rear = latest;
  53.         } else
  54.             front = rear = latest;
  55.         length++;
  56.     }
  57.  
  58.     public E dequeue () {
  59.         // Go otstranuva i vrakja pochetniot element na redicata.
  60.         if (front != null) {
  61.             E frontmost = front.element;
  62.             front = front.succ;
  63.             if (front == null)  rear = null;
  64.             length--;
  65.             return frontmost;
  66.         } else
  67.             throw new NoSuchElementException();
  68.     }
  69.  
  70. }
  71.  
  72. interface Queue<E> {
  73.  
  74.     // Elementi na redicata se objekti od proizvolen tip.
  75.  
  76.     // Metodi za pristap:
  77.  
  78.     public boolean isEmpty ();
  79.         // Vrakja true ako i samo ako redicata e prazena.
  80.  
  81.     public int size ();
  82.         // Ja vrakja dolzinata na redicata.
  83.  
  84.     public E peek ();
  85.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  86.  
  87.     // Metodi za transformacija:
  88.  
  89.     public void clear ();
  90.         // Ja prazni redicata.
  91.  
  92.     public void enqueue (E x);
  93.         // Go dodava x na kraj od redicata.
  94.  
  95.     public E dequeue ();
  96.         // Go otstranuva i vrakja pochetniot element na redicata.
  97.  
  98. }
  99.  
  100. class SLLNode<E> {
  101.     protected E element;
  102.     protected SLLNode<E> succ;
  103.  
  104.     public SLLNode(E elem, SLLNode<E> succ) {
  105.         this.element = elem;
  106.         this.succ = succ;
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return element.toString();
  112.     }
  113. }
  114.  
  115.  
  116. class Graph<E> {
  117.    
  118.     int num_nodes; // broj na jazli
  119.     E nodes[];    // informacija vo jazlite - moze i ne mora?
  120.     int adjMat[][];  // matrica na sosednost
  121.  
  122.     @SuppressWarnings("unchecked")
  123.     public Graph(int num_nodes) {
  124.         this.num_nodes = num_nodes;
  125.         nodes = (E[]) new Object[num_nodes];
  126.         adjMat = new int[num_nodes][num_nodes];
  127.        
  128.         for(int i=0;i<this.num_nodes;i++)
  129.             for(int j=0;j<this.num_nodes;j++)
  130.                 adjMat[i][j]=0;
  131.     }
  132.    
  133.    
  134.    
  135.     public Graph(int num_nodes, E[] nodes) {
  136.         this.num_nodes = num_nodes;
  137.         this.nodes = nodes;
  138.         adjMat = new int[num_nodes][num_nodes];
  139.        
  140.         for(int i=0;i<this.num_nodes;i++)
  141.             for(int j=0;j<this.num_nodes;j++)
  142.                 adjMat[i][j]=0;
  143.     }
  144.  
  145.  
  146.  
  147.     int adjacent(int x,int y)
  148.     {  // proveruva dali ima vrska od jazelot so indeks x do jazelot so indeks y
  149.        return (adjMat[x][y]!=0)?1:0;
  150.     }
  151.    
  152.     void addEdge(int x,int y)
  153.     {  // dodava vrska megu jazlite so indeksi x i y
  154.        adjMat[x][y]=1;
  155.     }
  156.    
  157.     void deleteEdge(int x,int y)
  158.     {
  159.        // ja brise vrskata megu jazlite so indeksi x i y
  160.        adjMat[x][y]=0;
  161.     }
  162.    
  163.     // Moze i ne mora?
  164.     E get_node_value(int x)
  165.     {  // ja vraka informacijata vo jazelot so indeks x
  166.           return nodes[x];
  167.     }
  168.  
  169.     // Moze i ne mora?
  170.     void set_node_value(int x, E a)
  171.     {  // ja postavuva informacijata vo jazelot so indeks na a
  172.        nodes[x]=a;
  173.     }
  174.    
  175.     public int getNum_nodes() {
  176.         return num_nodes;
  177.     }
  178.  
  179.     public void setNum_nodes(int num_nodes) {
  180.         this.num_nodes = num_nodes;
  181.     }
  182.  
  183.     void dfsSearch(int node) {
  184.         boolean visited[] = new boolean[num_nodes];
  185.         for (int i = 0; i < num_nodes; i++)
  186.             visited[i] = false;
  187.         dfsRecursive(node, visited);
  188.     }
  189.  
  190.     void dfsRecursive(int node, boolean visited[]) {
  191.         visited[node] = true;
  192.         System.out.println(node + ": " + nodes[node]);
  193.  
  194.         for (int i = 0; i < this.num_nodes; i++) {
  195.             if(adjacent(node, i)==1){
  196.                 if (!visited[i])
  197.                     dfsRecursive(i, visited);
  198.             }
  199.         }
  200.     }
  201.    
  202.     void dfsNonrecursive(int node) {
  203.         boolean visited[] = new boolean[num_nodes];
  204.         for (int i = 0; i < num_nodes; i++)
  205.             visited[i] = false;
  206.         visited[node] = true;
  207.         System.out.println(node + ": " + nodes[node]);
  208.         Stack<Integer> s = new Stack<Integer>();
  209.         s.push(node);
  210.  
  211.         int pom;
  212.  
  213.         while (!s.isEmpty()) {
  214.             pom = s.peek();
  215.             int pom1 = pom;
  216.             for (int i = 0; i < num_nodes; i++) {
  217.                 if(adjacent(pom,i)==1){
  218.                     pom1 = i;
  219.                     if(!visited[i])
  220.                         break;
  221.                 }          
  222.             }
  223.             if(!visited[pom1]){
  224.                 visited[pom1] = true;
  225.                 System.out.println(pom1 + ": " + nodes[pom1]);
  226.                 s.push(pom1);
  227.             }
  228.             else
  229.                 s.pop();
  230.         }
  231.  
  232.     }
  233.    
  234.     void bfs(int node){
  235.         boolean visited[] = new boolean[num_nodes];
  236.         for (int i = 0; i < num_nodes; i++)
  237.             visited[i] = false;
  238.         visited[node] = true;
  239.         System.out.println(node+": " + nodes[node]);
  240.         Queue<Integer> q = new LinkedQueue<Integer>();
  241.         q.enqueue(node);
  242.        
  243.         int pom;
  244.        
  245.         while(!q.isEmpty()){
  246.             pom = q.dequeue();
  247.             for (int i = 0; i < num_nodes; i++) {
  248.                 if(adjacent(pom, i)==1){
  249.                     if (!visited[i]){
  250.                         visited[i] = true;
  251.                         System.out.println(i+": " + nodes[i]);
  252.                         q.enqueue(i);
  253.                     }
  254.                    
  255.                 }
  256.             }
  257.            
  258.            
  259.         }
  260.        
  261.     }
  262.  
  263.     @Override
  264.     public String toString() {
  265.         String ret="  ";
  266.         for(int i=0;i<num_nodes;i++)
  267.             ret+=nodes[i]+" ";
  268.         ret+="\n";
  269.         for(int i=0;i<num_nodes;i++){
  270.             ret+=nodes[i]+" ";
  271.             for(int j=0;j<num_nodes;j++)
  272.                 ret+=adjMat[i][j]+" ";
  273.             ret+="\n";
  274.         }
  275.         return ret;
  276.     }
  277.    
  278.     int ribi ( int start ) {
  279.         int num = 0;
  280.        
  281.         boolean[] visited = new boolean[this.num_nodes];
  282.        
  283.         for(int i=0; i<this.num_nodes; i++){
  284.             visited[i] = false;
  285.         }
  286.        
  287.         visited[start] = true;
  288.        
  289.         LinkedQueue<Integer> q = new LinkedQueue<>();
  290.        
  291.         for(int i=0; i<this.num_nodes; i++){
  292.             if(this.adjacent(start, i) == 1){
  293.                 if(!visited[i]){
  294.                     q.enqueue(i);
  295.                     visited[i] = true;
  296.                     num++;
  297.                 }
  298.             }
  299.         }
  300.        
  301.         while(!q.isEmpty()){
  302.             int node = q.dequeue();
  303.             for(int i=0; i<this.num_nodes; i++){
  304.                 if(this.adjacent(node, i) == 1){
  305.                     if(!visited[i]){
  306.                         q.enqueue(i);
  307.                         visited[i] = true;
  308.                         num++;
  309.                     }
  310.                 }
  311.             }
  312.         }
  313.        
  314.         return num;
  315.     }
  316.    
  317. }
  318.  
  319. public class DedoMrazIRibite {
  320.  
  321.     public static void main(String[] args) throws IOException {
  322.  
  323.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  324.         String expr = br.readLine();
  325.         String[] exp;
  326.        
  327.         int teminja = Integer.parseInt(expr);
  328.         Graph<Integer> g = new Graph<>(teminja);
  329.        
  330.         expr = br.readLine();
  331.         int rebra = Integer.parseInt(expr);
  332.        
  333.         for(int i=0; i<rebra; i++){
  334.             expr = br.readLine();
  335.             exp = expr.split(" ");
  336.             int x = Integer.parseInt(exp[0]);
  337.             int y = Integer.parseInt(exp[1]);
  338.             g.addEdge(x, y);
  339.         }
  340.        
  341.         expr = br.readLine();
  342.         int start = Integer.parseInt(expr);
  343.        
  344.         System.out.println(g.ribi(start));
  345.         System.out.println("Srekna Nova Godina");
  346.        
  347.         br.close();
  348.  
  349.     }
  350.  
  351. }
Add Comment
Please, Sign In to add comment