Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Tuple {
- public int a, b;
- public Tuple(int a, int b) {
- this.a=a;
- this.b=b;
- }
- }
- public class mootube {
- public static int dfs(int v, int k, ArrayList<Tuple>[] matrix) {
- int result = 0;
- LinkedList<Integer> explore = new LinkedList<Integer>();
- // Set<Integer> visited = new HashSet<Integer>();
- boolean[] visited = new boolean[matrix.length];
- explore.add(v);
- while (!explore.isEmpty()) {
- int node = explore.remove();
- visited[node] = true;
- for (Tuple tup: matrix[node]) {
- int cost = tup.b;
- int u = tup.a;
- if (!visited[u] && cost>=k) {
- explore.add(u);
- result += 1;
- }
- }
- }
- return result;
- }
- public static void main(String[] args) throws IOException {
- BufferedReader f = new BufferedReader(new FileReader("mootube.in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("mootube.out")));
- StringTokenizer st = new StringTokenizer(f.readLine());
- int n = Integer.parseInt(st.nextToken());
- int q = Integer.parseInt(st.nextToken());
- ArrayList<Tuple>[] matrix = new ArrayList[n];
- for(int i = 0; i < n-1; i++) {
- st = new StringTokenizer(f.readLine());
- int a = Integer.parseInt(st.nextToken())-1;
- int b = Integer.parseInt(st.nextToken())-1;
- int cost = Integer.parseInt(st.nextToken());
- if (matrix[a] == null) matrix[a] = new ArrayList<Tuple>();
- if (matrix[b] == null) matrix[b] = new ArrayList<Tuple>();
- matrix[a].add(new Tuple(b, cost));
- matrix[b].add(new Tuple(a, cost));
- }
- for(int j = 0; j < q; j++) {
- st = new StringTokenizer(f.readLine());
- int k = Integer.parseInt(st.nextToken());
- int v = Integer.parseInt(st.nextToken())-1;
- out.println(dfs(v, k, matrix));
- }
- out.close();
- f.close();
- }
- }
Add Comment
Please, Sign In to add comment