Skip to content

Commit b9e0d0d

Browse files
authored
Merge pull request #501 from AlgorithmWithGod/khj20006
[20250719] BOJ / D4 / 승현이와 승현이 / 권혁준
2 parents c0121ba + 1a911a5 commit b9e0d0d

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
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()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, M;
57+
static int[] cost;
58+
static List<int[]>[] graph;
59+
static List<int[]> edges;
60+
static int[] r, depth;
61+
static int[][] parent, min;
62+
63+
public static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}
64+
65+
public static void main(String[] args) throws Exception {
66+
67+
io = new IOController();
68+
69+
init();
70+
solve();
71+
72+
io.close();
73+
74+
}
75+
76+
public static void init() throws Exception {
77+
78+
N = io.nextInt();
79+
M = io.nextInt();
80+
cost = new int[N];
81+
for(int i=0;i<N;i++) cost[i] = io.nextInt();
82+
graph = new List[N*N];
83+
r = new int[N*N];
84+
depth = new int[N*N];
85+
parent = new int[N*N+1][18];
86+
min = new int[N*N+1][18];
87+
for(int i=0;i<N*N;i++) {
88+
graph[i] = new ArrayList<>();
89+
r[i] = i;
90+
int x = i/N, y = i%N;
91+
}
92+
edges = new ArrayList<>();
93+
while(M-->0) {
94+
int u = io.nextInt() - 1;
95+
int v = io.nextInt() - 1;
96+
for(int i=0;i<N;i++) {
97+
int from = i*N + u;
98+
int to = i*N + v;
99+
int value = cost[i] * Math.max(cost[u], cost[v]);
100+
edges.add(new int[]{from, to, value});
101+
102+
from = u*N + i;
103+
to = v*N + i;
104+
edges.add(new int[]{from, to, value});
105+
}
106+
}
107+
108+
}
109+
110+
static void solve() throws Exception {
111+
112+
constructMST();
113+
dfs(0, N*N, 0);
114+
constructSparseTable();
115+
116+
int Q = io.nextInt();
117+
while(Q-->0) {
118+
int u = io.nextInt() - 1;
119+
int v = io.nextInt() - 1;
120+
int from = u*N + v;
121+
int to = v*N + u;
122+
123+
io.write(getMinValue(from, to) + "\n");
124+
}
125+
126+
}
127+
128+
static void constructMST() {
129+
Collections.sort(edges, (a,b) -> a[2]-b[2]);
130+
for(int[] edge : edges) {
131+
int a = edge[0], b = edge[1], c = edge[2];
132+
int x = f(a), y = f(b);
133+
if(x == y) continue;
134+
r[x] = y;
135+
graph[a].add(new int[]{b,c});
136+
graph[b].add(new int[]{a,c});
137+
}
138+
}
139+
140+
static void dfs(int n, int p, int d) {
141+
depth[n] = d;
142+
parent[n][0] = p;
143+
for(int[] e:graph[n]) if(e[0] != p) {
144+
dfs(e[0], n, d+1);
145+
min[e[0]][0] = e[1];
146+
}
147+
}
148+
149+
static void constructSparseTable() {
150+
for(int k=1;k<18;k++) for(int i=0;i<N*N;i++) {
151+
parent[i][k] = parent[parent[i][k-1]][k-1];
152+
min[i][k] = Math.max(min[i][k-1], min[parent[i][k-1]][k-1]);
153+
}
154+
}
155+
156+
static int getMinValue(int a, int b) {
157+
int depthDiff = Math.abs(depth[a] - depth[b]);
158+
int result = 0;
159+
for(int k=0;k<18;k++) if((depthDiff & (1<<k)) != 0) {
160+
if(depth[a] > depth[b]) {
161+
result = Math.max(result, min[a][k]);
162+
a = parent[a][k];
163+
}
164+
else {
165+
result = Math.max(result, min[b][k]);
166+
b = parent[b][k];
167+
}
168+
}
169+
170+
for(int k=17;k>=0;k--) if(parent[a][k] != parent[b][k]) {
171+
result = Math.max(result, Math.max(min[a][k], min[b][k]));
172+
a = parent[a][k];
173+
b = parent[b][k];
174+
}
175+
if(a != b) {
176+
result = Math.max(result, Math.max(min[a][0], min[b][0]));
177+
}
178+
179+
return result;
180+
181+
}
182+
183+
}
184+
```

0 commit comments

Comments
 (0)