Advertisement
Josif_tepe

Untitled

Jun 26th, 2023
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. /*
  2. lista na sosedstvo
  3.  */
  4.  
  5. import java.lang.reflect.Array;
  6. import java.util.*;
  7.  
  8. class GNode<E>{
  9.     public int num;
  10.     public E info;
  11.     public LinkedList<Integer> list;
  12.  
  13.     public GNode(){
  14.  
  15.         list = new LinkedList();
  16.     }
  17.  
  18.     boolean hasNeighbor(int node){
  19.         return list.contains(node);
  20.     }
  21.  
  22.     void addNeighbor(int node){
  23.         if(!list.contains(node)){
  24.             list.add(node);
  25.         }
  26.     }
  27.     void deleteNeighbor(GNode<E> node){
  28.         if(list.contains(node)){
  29.             list.remove(node);
  30.         }
  31.     }
  32. }
  33.  
  34. class Graph<E> {
  35.     public int n;
  36.     public GNode<E> graph[];
  37.  
  38.     public Graph(int n) {
  39.         this.n = n;
  40.         graph = new GNode[n];
  41.         for(int i = 0; i < n; i++) {
  42.             graph[i] = new GNode<>();
  43.         }
  44.  
  45.     }
  46.  
  47.     boolean neighbotss(int x, int y) {
  48.         return graph[x].hasNeighbor(y);
  49.     }
  50.  
  51.     void addEdge(int x, int y) {
  52.         graph[x].addNeighbor(y);
  53.         graph[y].addNeighbor(x);
  54.     } // ako bese nenasocen + kje imavme graph[y].addNeighbor(graph[x]);
  55.  
  56.     void deleteEdge(int x, int y) {
  57.         graph[x].deleteNeighbor((graph[y]));
  58.     }// ako bese nenasocen + kje imavme graph[y].deleteNeighbor(graph[x])
  59.  
  60.     void dfsListRek(int poseteni[], int start) {
  61.         poseteni[start] = 1;
  62.         System.out.print(start +  " ");
  63.         for (int i = 0; i < graph[start].list.size(); i++) {
  64.             if (poseteni[graph[start].list.get(i)] == 0) {
  65.                 dfsListRek(poseteni, graph[start].list.get(i));
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. public class Main {
  73.     public static void main(String[] args) {
  74.  
  75.         Scanner scanner = new Scanner(System.in);
  76.         int gradovi = scanner.nextInt();
  77.         int br_patista = scanner.nextInt();
  78.         Graph<Integer> graph = new Graph<>(gradovi);
  79.         Map<String, Integer> vrski = new HashMap<>();
  80.         int indeksiranje = 0;
  81.         for(int i = 0; i < br_patista; i++) {
  82.             String a = scanner.next();
  83.             String b = scanner.next();
  84.  
  85.             if(!vrski.containsKey(a)) {
  86.                 vrski.put(a, indeksiranje);
  87.                 indeksiranje++;
  88.             }
  89.  
  90.             if(!vrski.containsKey(b)) {
  91.                 vrski.put(b, indeksiranje);
  92.                 indeksiranje++;
  93.             }
  94.             System.out.println(vrski.get(a) + " " + vrski.get(b));
  95.             graph.addEdge(vrski.get(a), vrski.get(b));
  96.         }
  97.         int kolku = scanner.nextInt();
  98.         for(int i = 0; i < kolku; i++) {
  99.             String grad = scanner.next();
  100.             int[] poseteni = new int[gradovi];
  101.             graph.dfsListRek(poseteni, vrski.get(grad));
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement