-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ1240.java
More file actions
85 lines (70 loc) · 2.35 KB
/
BOJ1240.java
File metadata and controls
85 lines (70 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class BOJ1240 {
static int n, m;
static List<Integer>[] tree;
static boolean[] visited;
static int[][] target;
static int[][] weights = new int[1001][1001];
static List<Integer> answer = new ArrayList<>();
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
// set n, m;
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
// set tree
tree = new List[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
st = new StringTokenizer(br.readLine());
int node1 = Integer.parseInt(st.nextToken());
int node2 = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
// set initialize
weights[node1][node2] = weight;
weights[node2][node1] = weight;
// tree
tree[node1].add(node2);
tree[node2].add(node1);
}
target = new int[m][2];
for(int i = 0; i < m; i++){
st = new StringTokenizer(br.readLine());
target[i][0] = Integer.parseInt(st.nextToken());
target[i][1] = Integer.parseInt(st.nextToken());
}
}
public static void main(String[] args) throws IOException {
input();
process();
}
private static void process() {
for(int i = 0; i < m; i++){
visited = new boolean[n+1];
dfs(target[i][0], target[i][1], 0);
}
// print answer
for(int i : answer)
System.out.println(i);
}
private static void dfs(int vertex, int target, int weightSum){
if(vertex == target){
answer.add(weightSum);
return;
}
visited[vertex] = true;
for(int i : tree[vertex]){
if(!visited[i]){
dfs(i, target, weightSum + weights[vertex][i]);
}
}
}
}