Skip to content

Commit b9937cc

Browse files
authored
[20251102] BOJ / P1 / Imperial roads / 권혁준
1 parent c6541df commit b9937cc

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens())
24+
nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static int N, R, Q;
58+
static int[][] edges;
59+
static List<int[]>[] graph;
60+
static int[] root, dep;
61+
static int[][] par, max;
62+
static TreeMap<Integer, TreeMap<Integer, Integer>> help;
63+
64+
public static int f(int x) { return x == root[x] ? x : (root[x] = f(root[x])); }
65+
66+
public static void dfs(int n, int p, int d) {
67+
par[n][0] = p;
68+
dep[n] = d;
69+
for(int[] e:graph[n]) if(e[0] != p) {
70+
dfs(e[0], n, d+1);
71+
max[e[0]][0] = e[1];
72+
}
73+
}
74+
75+
public static void main(String[] args) throws Exception {
76+
77+
io = new IOController();
78+
79+
N = io.nextInt();
80+
R = io.nextInt();
81+
edges = new int[R][];
82+
for(int i=0;i<R;i++) edges[i] = new int[]{io.nextInt(),io.nextInt(),io.nextInt()};
83+
Arrays.sort(edges, (a,b) -> a[2]-b[2]);
84+
85+
graph = new List[N+1];
86+
root = new int[N+1];
87+
for(int i=1;i<=N;i++) {
88+
graph[i] = new ArrayList<>();
89+
root[i] = i;
90+
}
91+
92+
help = new TreeMap<>();
93+
int mst = 0;
94+
for(int[] edge : edges) {
95+
int a = edge[0], b = edge[1], c = edge[2];
96+
if(!help.containsKey(a)) help.put(a, new TreeMap<>());
97+
help.get(a).put(b, c);
98+
if(!help.containsKey(b)) help.put(b, new TreeMap<>());
99+
help.get(b).put(a, c);
100+
int x = f(a), y = f(b);
101+
if(x == y) continue;
102+
mst += c;
103+
root[x] = y;
104+
graph[a].add(new int[]{b,c});
105+
graph[b].add(new int[]{a,c});
106+
}
107+
108+
par = new int[N+1][17];
109+
dep = new int[N+1];
110+
max = new int[N+1][17];
111+
dfs(1,0,0);
112+
for(int k=1;k<17;k++) for(int i=1;i<=N;i++) {
113+
par[i][k] = par[par[i][k-1]][k-1];
114+
max[i][k] = Math.max(max[i][k-1], max[par[i][k-1]][k-1]);
115+
}
116+
117+
for(Q = io.nextInt(); Q-->0;) {
118+
int a = io.nextInt(), b = io.nextInt();
119+
int c = help.get(a).get(b);
120+
121+
int rem = 0;
122+
int diff = Math.abs(dep[a] - dep[b]);
123+
for(int k=0;k<17;k++) if((diff & (1<<k)) != 0) {
124+
if(dep[a] > dep[b]) {
125+
rem = Math.max(rem, max[a][k]);
126+
a = par[a][k];
127+
}
128+
else {
129+
rem = Math.max(rem, max[b][k]);
130+
b = par[b][k];
131+
}
132+
}
133+
134+
for(int k=16;k>=0;k--) if(par[a][k] != par[b][k]) {
135+
rem = Math.max(rem, Math.max(max[a][k], max[b][k]));
136+
a = par[a][k];
137+
b = par[b][k];
138+
}
139+
140+
if(a != b) {
141+
rem = Math.max(rem, Math.max(max[a][0], max[b][0]));
142+
}
143+
144+
io.write((mst + c - rem) + "\n");
145+
}
146+
147+
io.close();
148+
149+
}
150+
151+
}
152+
```

0 commit comments

Comments
 (0)