-
Notifications
You must be signed in to change notification settings - Fork 0
/
AncientBerlandRoads.java
280 lines (202 loc) · 5.86 KB
/
AncientBerlandRoads.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
280
package problems.codechef;
import sun.reflect.generics.tree.Tree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* Created by Arpit on 31-Dec-15.
*/
public class AncientBerlandRoads {
static int region[];
static TreeSet<Pair<Integer,Integer>>max;
public static void main(String[] args) throws IOException {
int n,m,q,query[][];
String s[];
City city[];
Road road[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
q=Integer.parseInt(s[2]);
query=new int[q+1][];
city=new City[n+1];
road=new Road[m+1];
region=new int[n+1];
/* max=new TreeSet<>( new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
if (o1.getLeft()==o2.getLeft()){
o2.setRight(o1.getRight());
return 0;
}
else {
if (o1.getRight() < o2.getRight()) return -1;
else if (o1.getRight() > o2.getRight()) return 1;
else return 1;
}
}
});*/
s=br.readLine().split("\\s");
for (int i = 1; i <=n; i++) {
city[i]=new City(Integer.parseInt(s[i-1]),i);
region[i]=city[i].population;
}
int from,to;
for (int i = 1; i <=m; i++) {
s=br.readLine().split("\\s");
from=Integer.parseInt(s[0]);
to=Integer.parseInt(s[1]);
road[i]=new Road(from,to,false);
}
for (int i = 0; i <q; i++) {
s=br.readLine().split("\\s");
query[i]=new int[s.length];
int temp;
char ch=s[0].charAt(0);
if (ch=='P'){
query[i][0]='P';
query[i][1]=Integer.parseInt(s[1]);
temp=Integer.parseInt(s[2]);
query[i][2]=city[query[i][1]].population;
city[query[i][1]].population=temp;
region[query[i][1]]=temp;
}
else {
query[i][0]='D';
query[i][1]=Integer.parseInt(s[1]);
road[query[i][1]].setStatus(true);
}
}
for (int i = 1; i <=m; i++) {
if (!road[i].getStatus()) {
City x,y;
from=road[i].getFrom();
to=road[i].getTo();
x=city[from].getParent();
y=city[to].getParent();
calculateUnion(x, y);
}
}
Stack<Integer>ans=new Stack<>();
for (int i = q-1; i>=0; i--) {
ans.push(getMax(region));
if (query[i][0]=='P'){
int x=city[query[i][1]].getParent().data;
region[x]-=city[query[i][1]].population-query[i][2];
city[query[i][1]].population=query[i][2];
}
else {
City x,y;
int a,b;
a=road[query[i][1]].getFrom();
b=road[query[i][1]].getTo();
x=city[a].getParent();
y=city[b].getParent();
calculateUnion(x, y);
}
}
Collections.reverse(ans);
Iterator<Integer>itr=ans.iterator();
while (itr.hasNext()) System.out.println(itr.next());
}
static int getMax(int a[]){
int big=a[0];
for (int i = 1; i < a.length; i++) {
if (a[i]>big)big=a[i];
}
return big;
}
static void calculateUnion(City c1,City c2){
if (c1==c2)return ;
else if (c1.rank<c2.rank){
c1.parent=c2;
region[c2.data]+=region[c1.data];
}
else if (c1.rank==c2.rank){
c2.parent=c1;
c1.rank++;
region[c1.data]+=region[c2.data];
}
else {
c2.parent=c1;
region[c1.data]+=region[c2.data];
}
}
}
class City{
int population;
City parent;
int rank,data;
public City(int population,int index) {
this.population = population;
this.data=index;
parent=this;
}
public City getParent() {
City temp=this;
while (temp.parent!=temp){
temp=temp.parent;
}
return temp;
}
}
class Road{
private int from,to;
private boolean status;
public Road(int from, int to, boolean status) {
this.from = from;
this.to = to;
this.status = status;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getTo() {
return to;
}
public void setTo(int to) {
this.to = to;
}
}
class CityComparator implements Comparator<City>{
@Override
public int compare(City o1, City o2) {
if (o1.population<o2.population)return 1;
else if(o1.population>o2.population) return -1;
else return 0;
}
}
class Pair<L,R>{
private L left;
private R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
public void setLeft(L left) {
this.left = left;
}
public void setRight(R right) {
this.right = right;
}
@Override
public String toString() {
return "Pair{" +
"left=" + left +
", right=" + right +
'}';
}
}