Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- lista na sosedstvo
- */
- import java.lang.reflect.Array;
- import java.util.*;
- class GNode<E>{
- public int num;
- public E info;
- public LinkedList<Integer> list;
- public GNode(){
- list = new LinkedList();
- }
- boolean hasNeighbor(int node){
- return list.contains(node);
- }
- void addNeighbor(int node){
- if(!list.contains(node)){
- list.add(node);
- }
- }
- void deleteNeighbor(GNode<E> node){
- if(list.contains(node)){
- list.remove(node);
- }
- }
- }
- class Graph<E> {
- public int n;
- public GNode<E> graph[];
- public Graph(int n) {
- this.n = n;
- graph = new GNode[n];
- for(int i = 0; i < n; i++) {
- graph[i] = new GNode<>();
- }
- }
- boolean neighbotss(int x, int y) {
- return graph[x].hasNeighbor(y);
- }
- void addEdge(int x, int y) {
- graph[x].addNeighbor(y);
- graph[y].addNeighbor(x);
- } // ako bese nenasocen + kje imavme graph[y].addNeighbor(graph[x]);
- void deleteEdge(int x, int y) {
- graph[x].deleteNeighbor((graph[y]));
- }// ako bese nenasocen + kje imavme graph[y].deleteNeighbor(graph[x])
- void dfsListRek(int poseteni[], int start) {
- poseteni[start] = 1;
- System.out.print(start + " ");
- for (int i = 0; i < graph[start].list.size(); i++) {
- if (poseteni[graph[start].list.get(i)] == 0) {
- dfsListRek(poseteni, graph[start].list.get(i));
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int gradovi = scanner.nextInt();
- int br_patista = scanner.nextInt();
- Graph<Integer> graph = new Graph<>(gradovi);
- Map<String, Integer> vrski = new HashMap<>();
- int indeksiranje = 0;
- for(int i = 0; i < br_patista; i++) {
- String a = scanner.next();
- String b = scanner.next();
- if(!vrski.containsKey(a)) {
- vrski.put(a, indeksiranje);
- indeksiranje++;
- }
- if(!vrski.containsKey(b)) {
- vrski.put(b, indeksiranje);
- indeksiranje++;
- }
- System.out.println(vrski.get(a) + " " + vrski.get(b));
- graph.addEdge(vrski.get(a), vrski.get(b));
- }
- int kolku = scanner.nextInt();
- for(int i = 0; i < kolku; i++) {
- String grad = scanner.next();
- int[] poseteni = new int[gradovi];
- graph.dfsListRek(poseteni, vrski.get(grad));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement