Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.6 kB
2
Indexable
Never
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Graph {
    public HashMap<Integer, List<Integer>> map = new HashMap<>();

    public void addVertices(int n) {
        for (int i = 0; i < n; i++) {
            map.put(i, new LinkedList<Integer>());
        }
    }

    public void addEdge(int v1, int v2) {
        map.get(v1).add(v2);
        map.get(v2).add(v1);
    }

    public int[] bfs(int n, int v) {
        LinkedList<Integer> queue = new LinkedList<>();
        int[] distance = new int[n];

        distance[v] = 0;
        queue.add(v);

        while (!queue.isEmpty()) {
            int cur = queue.poll();

            for (int x : map.get(cur)) {
                if (x != v && distance[x] == 0) {
                    distance[x] = distance[cur] + 1;
                    queue.add(x);
                }
            }
        }

        return distance;
    }
}

class Result {
    public static List<Integer> bfs(int n, int m, List<List<Integer>> edges, int s) {
        ArrayList<Integer> res = new ArrayList<>();
        Graph graph = new Graph();

        graph.addVertices(n);

        for (List<Integer> edge : edges) {
            graph.addEdge(edge.get(0) - 1, edge.get(1) - 1);
        }

        int[] distance = graph.bfs(n, s - 1);

        for (int i = 0; i < n; i++) {
            if (i != s - 1) {
                if (distance[i] > 0) {
                    res.add(distance[i] * 6);
                } else {
                    res.add(-1);
                }
            }
        }

        return res;
    }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int q = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, q).forEach(qItr -> {
            try {
                String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

                int n = Integer.parseInt(firstMultipleInput[0]);

                int m = Integer.parseInt(firstMultipleInput[1]);

                List<List<Integer>> edges = new ArrayList<>();

                IntStream.range(0, m).forEach(i -> {
                    try {
                        edges.add(
                            Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                                .map(Integer::parseInt)
                                .collect(toList())
                        );
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                });

                int s = Integer.parseInt(bufferedReader.readLine().trim());

                List<Integer> result = Result.bfs(n, m, edges, s);

                bufferedWriter.write(
                    result.stream()
                        .map(Object::toString)
                        .collect(joining(" "))
                    + "\n"
                );
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}