Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class Graph {
- private Map<Integer, Node> nodes = new HashMap<Integer, Node>();
- public Graph() {
- }
- public void addEdge(Integer nodeName1, Integer nodeName2) {
- Node node1 = nodes.get(nodeName1);
- if (node1 == null) {
- node1 = new Node(nodeName1);
- }
- Node node2 = nodes.get(nodeName2);
- if (node2 == null) {
- node2 = new Node(nodeName2);
- }
- node1.addNeighbor(node2);
- node2.addNeighbor(node1);
- nodes.put(nodeName1, node1);
- nodes.put(nodeName2, node2);
- }
- public List<Integer> shortestPath(Integer startNodeName, Integer endNodeName) {
- Map<Integer, Integer> parents = new HashMap<Integer, Integer>();
- List<Node> temp = new ArrayList<Node>();
- Node start = nodes.get(startNodeName);
- temp.add(start);
- parents.put(startNodeName, null);
- while (temp.size() > 0) {
- Node currentNode = temp.get(0);
- List<Node> neighbors = currentNode.getNeighbors();
- for (int i = 0; i < neighbors.size(); i++) {
- Node neighbor = neighbors.get(i);
- Integer nodeName = neighbor.getName();
- boolean visited = parents.containsKey(nodeName);
- if (visited) {
- continue;
- } else {
- temp.add(neighbor);
- parents.put(nodeName, currentNode.getName());
- if (nodeName.equals(endNodeName)) {
- // System.out.println(parents);
- return getPath(parents, endNodeName);
- }
- }
- }
- temp.remove(0);
- }
- return null;
- }
- private List<Integer> getPath(Map<Integer, Integer> parents, Integer endNodeName) {
- List<Integer> path = new ArrayList<Integer>();
- Integer node = endNodeName;
- while (node != null) {
- path.add(0, node);
- Integer parent = parents.get(node);
- node = parent;
- }
- return path;
- }
- }
- class Node {
- Integer name;
- List<Node> neighbors = new ArrayList<Node>();
- public Node(Integer name) {
- this.name = name;
- }
- public Integer getName() {
- return this.name;
- }
- public void addNeighbor(Node neighbor) {
- neighbors.add(neighbor);
- }
- public List<Node> getNeighbors() {
- return neighbors;
- }
- public String toString() {
- return Integer.toString(this.name);
- }
- }
- class FastScanner {
- public BufferedReader br;
- public StringTokenizer st;
- public FastScanner(InputStream is) throws IOException {
- br = new BufferedReader(new InputStreamReader(is), 32768);
- st = null;
- }
- public String next() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return st.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public String nextLine() {
- String str = "";
- try {
- str = br.readLine();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return str;
- }
- }
- public class Main {
- public static void main(String[] args) throws IOException {
- InputStream is = new FileInputStream("milkvisits.in");
- PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milkvisits.out")));
- FastScanner sc = new FastScanner(is);
- int N = sc.nextInt();
- int M = sc.nextInt();
- int[] types = new int[N];
- Graph g = new Graph();
- int[] fr = new int[M];
- for (int i = 0; i < N; i++) {
- types[i] = sc.nextInt();
- }
- for (int i = 0; i < N - 1; i++) {
- g.addEdge(sc.nextInt(), sc.nextInt());
- }
- int n1, n2, cow;
- List<Integer> path;
- for (int i = 0; i < M; i++) {
- n1 = sc.nextInt();
- n2 = sc.nextInt();
- cow = sc.nextInt();
- if (n1 == n2) {
- if (cow == types[n1 - 1]) {
- fr[i] = 1;
- }
- continue;
- }
- path = g.shortestPath(n1, n2);
- if (check(path, types, cow)) {
- fr[i] = 1;
- }
- }
- String out = "";
- for (int i : fr) {
- out += i;
- }
- System.out.println(out);
- pw.write(out);
- pw.close();
- }
- public static boolean check(List<Integer> path, int[] s, int cow) {
- for (int i : path) {
- if (s[i - 1] == cow) {
- return true;
- }
- }
- return false;
- }
- private static void print(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement