-
Notifications
You must be signed in to change notification settings - Fork 14
/
q10169.java
279 lines (234 loc) · 8.54 KB
/
q10169.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.io.*;
import java.util.*;
class Load {
int goal, num;
Load (int goal, int num) {
this.goal = goal;
this.num = num;
}
}
class HLD {
class Segment {
private int[] tree;
int start = 1, end;
Segment(ArrayList<Integer> al) {
while(start < al.size()) start <<= 1;
tree = new int[start << 1];
end = al.size();
Arrays.fill(tree, Integer.MAX_VALUE);
for(int i=0;i<al.size();i++)
nodesIdx[al.get(i)] = i+1;
}
private void propagate (int node) {
if (node >= start) return;
tree[node*2] = Math.min(tree[node*2], tree[node]);
tree[node*2+1] = Math.min(tree[node*2+1], tree[node]);
tree[node] = Integer.MAX_VALUE;
}
public void update(int node, int s, int e, int left, int right, int value) {
if (e < left || right < s) return;
//세그먼트 트리에 대해, 업데이트 쿼리 작성
if (tree[node] != Integer.MAX_VALUE) propagate(node);
if (s <= left && right <= e) {
tree[node] = Math.min(tree[node], value);
return;
}
int mid = left + right >> 1;
update(node<<1, s, e, left, mid, value);
update(node*2+1, s, e, mid+1, right, value);
}
public void set () {
for (int i=1;i<start;i++) {
propagate(i);
}
}
public int getSub (int node) {
return tree[start + nodesIdx[node] - 1];
}
}
class Pair {
int x, y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
int[] mychain, nextChain, chainDepth, level;
public static int[] nodesIdx;
Segment[] chaines;
HLD(ArrayList<ArrayList<Load>> load, int size) {
Queue<Pair> qu = new LinkedList<>();
Stack<Integer> sts = new Stack<>();
nodesIdx = new int[size+1];
qu.offer(new Pair(1, 0));
level = new int[size+1];
while (!qu.isEmpty()) {
Pair n = qu.poll();
boolean s = true;
for (Load go : load.get(n.x)) {
if (go.goal != n.y) {
level[go.goal] = level[n.x] + 1;
qu.offer(new Pair(go.goal, n.x));
s = false;
}
}
if (s) sts.push(n.x);
}
mychain = new int[size+1];
nextChain = new int[sts.size()];
chainDepth = new int[sts.size()];
chaines = new Segment[sts.size()];
Arrays.fill(mychain, -1);
int cha = 0;
while(!sts.isEmpty()) {
int node = sts.pop();
mychain[node] = cha;
ArrayList<Integer> al = new ArrayList<>();
qu.offer(new Pair(node, 0));
while(!qu.isEmpty()) {
Pair n = qu.poll();
al.add(n.x);
for (Load go : load.get(n.x)) {
if (level[go.goal] < level[n.x]) {
if (mychain[go.goal] == -1) {
mychain[go.goal] = cha;
qu.offer(new Pair(go.goal, n.x));
} else {
nextChain[cha] = go.goal;
chainDepth[cha] = chainDepth[mychain[go.goal]] + 1;
}
break;
}
}
}
chaines[cha++] = new Segment(al);
}
}
public void update (int n1, int n2, int value) {
int lca = getLca(n1, n2);
while(mychain[n1] != mychain[lca]) {
chaines[mychain[n1]].update(1, nodesIdx[n1], chaines[mychain[n1]].end
, 1, chaines[mychain[n1]].start, value);
n1 = nextChain[mychain[n1]];
}
while(mychain[n2] != mychain[lca]) {
chaines[mychain[n2]].update(1, nodesIdx[n2], chaines[mychain[n2]].end
, 1, chaines[mychain[n2]].start, value);
n2 = nextChain[mychain[n2]];
}
if (level[n1] > level[n2]) {
int temp = n1;
n1 = n2;
n2 = temp;
}
if (n1 == lca && n2 == lca) return;
chaines[mychain[lca]].update(1, nodesIdx[n2], nodesIdx[lca]-1, 1
, chaines[mychain[lca]].start, value);
}
public int getLca(int n1, int n2) {
//n1, n2관계에 대해, 일반성을 유지하기 위해, 깊이를 바꾸어준다.
if(chainDepth[mychain[n1]] > chainDepth[mychain[n2]]) {
int temp = n1;
n1 = n2;
n2 = temp;
}
while(chainDepth[mychain[n1]] != chainDepth[mychain[n2]]) {
n2 = nextChain[mychain[n2]];
}
while(mychain[n1] != mychain[n2]) {
n1 = nextChain[mychain[n1]];
n2 = nextChain[mychain[n2]];
}
return level[n1] > level[n2] ? n2 : n1;
}
}
public class Main {
static class Line {
int n1, n2, idx;
long cost;
boolean used;
Line (int n1, int n2,int cost, int idx) {
this.cost = cost;
this.n1= n1;
this.n2 = n2;
this.idx = idx;
}
}
public static ArrayList<ArrayList<Load>> load;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
ArrayList<Line> lines = new ArrayList<>(M);
for (int i=0;i<M;i++) {
st = new StringTokenizer(br.readLine());
lines.add(new Line(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())
, Integer.parseInt(st.nextToken()), i));
}
lines.sort(new Comparator<Line>() {
@Override
public int compare(Line o1, Line o2) {
return Long.compare(o1.cost, o2.cost);
}
});
load = new ArrayList<>(N+1);
for (int i=0;i<=N;i++) load.add(new ArrayList<>());
for (int i=0;i<MAX;i++) parent[i] = i;
answer = new long[M];
long sum = 0;
int connectedCount = 0;
for (Line line : lines) {
if (find(line.n1) != find(line.n2)) {
union(line.n1, line.n2);
line.used = true;
sum += line.cost;
connectedCount++;
load.get(line.n1).add(new Load(line.n2, line.idx));
load.get(line.n2).add(new Load(line.n1, line.idx));
}
}
if (connectedCount != N-1) {
for (int i=0;i<M;i++) bw.write(-1+"\n");
bw.flush();
return;
}
replaced = new int[M];
dfs(1, 0);
HLD hld = new HLD(load, N);
for (Line line : lines) {
if (!line.used) {
answer[line.idx] = sum;
hld.update(line.n1, line.n2, (int) line.cost);
}
}
for (int i=0;i<hld.chaines.length;i++) hld.chaines[i].set();
for (Line line : lines) {
if (line.used) {
int k = hld.chaines[hld.mychain[replaced[line.idx]]].getSub(replaced[line.idx]);
answer[line.idx] = k == Integer.MAX_VALUE ? -1 : sum - line.cost + k;
}
}
for(long e : answer) bw.write(e+"\n");
bw.flush();
}
public static void dfs (int node, int prev) {
for (Load go : load.get(node)) if (go.goal != prev) {
replaced[go.num] = go.goal;
dfs(go.goal, node);
}
}
public final static int MAX = 100001;
public static int[] parent = new int[MAX], replaced;
public static long[] answer;
public static int find (int node) {
if (parent[node] == node) return node;
return parent[node] = find(parent[node]);
}
public static void union (int n1, int n2) {
int p1 = find(n1);
int p2 = find(n2);
parent[p1] = p2;
}
}