Advertisement
fensa08

#APS Lab 1/10

Jan 1st, 2020
3,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1.  
  2.  
  3. Избор на предмет на факултет Problem 1 (0 / 0)
  4.  
  5. На еден факултет се поставени предмети кои задолжително треба да се слушаат и изборни предмети. За секој предмет е даден предуслов: кои предмети треба да се положени за да може да се слуша избраниот предмет. Ваша задача е да за даден последен положен предмет, да најдете кој е следен предмет кој може да го слуша студентот.
  6.  
  7. Влез: Во првиот ред е даден број n - број на предмети Во следните n редови се дадени шифрите на предметите. Во следнииот ред е даден број m - број на зависности Во следните m реда се дадени шифри на предмети, разделени со празно место. Првата шифра го означува предметот за кој се дефинира зависност, а следните шифри се предметите кои треба да се положени за да се слуша предметот даден со првата шифра. Во последниот ред е дадена шифрата на последниот слушан предмет.
  8.  
  9. Излез: Се печати шифрата на следниот предмет кој треба да се слуша.
  10. ========================================================================================================================================
  11.  
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.util.LinkedList;
  16.  
  17.  
  18. //import java.util.Iterator;
  19. import java.util.Iterator;
  20. import java.util.Stack;
  21.  
  22. class Graph<E> {
  23.  
  24.     int num_nodes;
  25.     GraphNode<E> adjList[];
  26.  
  27.     @SuppressWarnings("unchecked")
  28.     public Graph(int num_nodes, E[] list) {
  29.         this.num_nodes = num_nodes;
  30.         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  31.         for (int i = 0; i < num_nodes; i++)
  32.             adjList[i] = new GraphNode<E>(i, list[i]);
  33.     }
  34.  
  35.     @SuppressWarnings("unchecked")
  36.     public Graph(int num_nodes) {
  37.         this.num_nodes = num_nodes;
  38.         adjList = (GraphNode<E>[]) new GraphNode[num_nodes];
  39.         for (int i = 0; i < num_nodes; i++)
  40.             adjList[i] = new GraphNode<E>(i, null);
  41.     }
  42.  
  43.     int adjacent(int x, int y) {
  44.         // proveruva dali ima vrska od jazelot so
  45.         // indeks x do jazelot so indeks y
  46.         return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
  47.     }
  48.  
  49.     void addEdge(int x, int y) {
  50.         // dodava vrska od jazelot so indeks x do jazelot so indeks y
  51.         if (!adjList[x].containsNeighbor(adjList[y])) {
  52.             adjList[x].addNeighbor(adjList[y]);
  53.         }
  54.     }
  55.  
  56.     void deleteEdge(int x, int y) {
  57.         adjList[x].removeNeighbor(adjList[y]);
  58.     }
  59.  
  60.     public int findIndex(String element){
  61.  
  62.         for(int i = 0; i < adjList.length; i++){
  63.             if(adjList[i].getInfo().equals(element)){
  64.                 return i;
  65.             }
  66.         }
  67.         return -1;
  68.  
  69.     }
  70.  
  71.     public void addEdgeUpdated(String line){
  72.  
  73.         String pom[] = line.split(" ");
  74.  
  75.         int host = findIndex(pom[0]);
  76.         for(int i = 1; i < pom.length; i++){
  77.             int neigh = findIndex(pom[i]);
  78.             if(host != -1 && neigh != -1){
  79.                 this.addEdge(host,neigh);
  80.             }
  81.             else{
  82.                 System.out.println("Ne moze da go najde hostot");
  83.             }
  84.         }
  85.  
  86.  
  87.  
  88.     }
  89.  
  90.  
  91.  
  92.     /************************TOPOLOGICAL SORT*******************************************************************/
  93.  
  94.     void dfsVisit(Stack<Integer> s, int i, boolean[] visited){
  95.         if(!visited[i]){
  96.             visited[i] = true;
  97.  
  98.             Iterator<GraphNode<E>> it = adjList[i].getNeighbors().iterator();
  99.            // System.out.println("dfsVisit: "+i+" Stack="+s);
  100.             while(it.hasNext()){
  101.                 dfsVisit(s, it.next().getIndex(), visited);
  102.             }
  103.             s.push(i);
  104.           //  System.out.println("--dfsVisit: "+i+" Stack="+s);
  105.         }
  106.     }
  107.  
  108.     void topological_sort_dfs(String find){
  109.         boolean visited[] = new boolean[num_nodes];
  110.         for(int i=0;i<num_nodes;i++){
  111.             visited[i] = false;
  112.         }
  113.  
  114.         Stack<Integer> s = new Stack<Integer>();
  115.  
  116.         boolean flag = false;
  117.         for(int i=0;i<num_nodes;i++){
  118.             dfsVisit(s,i,visited);
  119.         }
  120.  
  121.         Stack<Integer> ns = new Stack<Integer>();
  122.         while(!s.isEmpty()){
  123.            // System.out.print(adjList[s.pop()].getInfo() + " ");
  124.             ns.push(s.pop());
  125.         }
  126.  
  127.         while(!ns.isEmpty()){
  128.             GraphNode<E> prev = adjList[ns.pop()];
  129.             if(prev.getInfo().equals(find)){
  130.                 System.out.println(adjList[ns.pop()].getInfo());
  131.             }
  132.         }
  133.     }
  134.  
  135.     /***********************************************************************************************************/
  136.  
  137.     @Override
  138.     public String toString() {
  139.         String ret = new String();
  140.         for (int i = 0; i < this.num_nodes; i++)
  141.             ret += i + ": " + adjList[i] + "\n";
  142.         return ret;
  143.     }
  144.  
  145. }
  146.  
  147.  
  148. class GraphNode<E> {
  149.     private int index;//index (reden broj) na temeto vo grafot
  150.     private E info;
  151.     private LinkedList<GraphNode<E>> neighbors;
  152.  
  153.     public GraphNode(int index, E info) {
  154.         this.index = index;
  155.         this.info = info;
  156.         neighbors = new LinkedList<GraphNode<E>>();
  157.     }
  158.  
  159.     boolean containsNeighbor(GraphNode<E> o){
  160.         return neighbors.contains(o);
  161.     }
  162.  
  163.     void addNeighbor(GraphNode<E> o){
  164.         neighbors.add(o);
  165.     }
  166.  
  167.  
  168.     void removeNeighbor(GraphNode<E> o){
  169.         if(neighbors.contains(o))
  170.             neighbors.remove(o);
  171.     }
  172.  
  173.  
  174.     @Override
  175.     public String toString() {
  176.         String ret= "INFO:"+info+" SOSEDI:";
  177.         for(int i=0;i<neighbors.size();i++)
  178.             ret+=neighbors.get(i).info+" ";
  179.         return ret;
  180.  
  181.     }
  182.  
  183.     @Override
  184.     public boolean equals(Object obj) {
  185.         @SuppressWarnings("unchecked")
  186.         GraphNode<E> pom = (GraphNode<E>)obj;
  187.         return (pom.info.equals(this.info));
  188.     }
  189.  
  190.     public int getIndex() {
  191.         return index;
  192.     }
  193.  
  194.     public void setIndex(int index) {
  195.         this.index = index;
  196.     }
  197.  
  198.     public E getInfo() {
  199.         return info;
  200.     }
  201.  
  202.     public void setInfo(E info) {
  203.         this.info = info;
  204.     }
  205.  
  206.     public LinkedList<GraphNode<E>> getNeighbors() {
  207.         return neighbors;
  208.     }
  209.  
  210.     public void setNeighbors(LinkedList<GraphNode<E>> neighbors) {
  211.         this.neighbors = neighbors;
  212.     }
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219. public class IzborPredmet {
  220.  
  221.  
  222.     public static void main(String[] args) throws IOException {
  223.  
  224.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  225.         int n = Integer.parseInt(in.readLine());
  226.         Graph<String> g = new Graph<>(n);
  227.  
  228.         for(int i  = 0; i < n; i++){
  229.             String line = in.readLine();
  230.             g.adjList[i].setInfo(line);
  231.         }
  232.  
  233.         n = Integer.parseInt(in.readLine());
  234.         for(int i = 0; i < n; i++){
  235.  
  236.             String line = in.readLine();
  237.             g.addEdgeUpdated(line);
  238.  
  239.         }
  240.  
  241.         String findNext = in.readLine();
  242.         g.topological_sort_dfs(findNext);
  243.  
  244.     }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement