-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultimoo.java
More file actions
248 lines (245 loc) · 13.9 KB
/
multimoo.java
File metadata and controls
248 lines (245 loc) · 13.9 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
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
import java.io.*;
import java.util.*;
public class multimoo {
static ArrayList<Integer> regsizes;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("multimoo.in"));
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("multimoo.out")));
//Scanner reader = new Scanner(System.in);
int N = Integer.parseInt(reader.readLine());
StringTokenizer st;
int[][] matrix = new int[N][N];
boolean[][] visited = new boolean[N][N];
int[][] regid = new int[N][N];
regsizes = new ArrayList<Integer>();
//TreeSet<Integer> cows = new TreeSet<Integer>();
for(int i = 0; i < N; i++)
{
st = new StringTokenizer(reader.readLine());
for(int j = 0; j < N; j++)
{
matrix[i][j] = Integer.parseInt(st.nextToken());
//cows.add(matrix[i][j]);
}
}
//first part, dfs on each connected component. generate second part graph, have list of adjacent
//identify conncomps with id in order
Stack<Integer> stack;
int curri, currj;
int currregid = 0, regsize;
int maxsize = 0;
for(int i = 0; i < N*N; i++)
{
if(!visited[i/N][i%N])
{
regsize = 1;
stack = new Stack<Integer>();
visited[i/N][i%N] = true;
stack.push(i);
regid[i/N][i%N] = currregid;
while(!stack.isEmpty())
{
curri = stack.peek()/N;
currj = stack.pop()%N;
if(curri > 0)
{
if(!visited[curri-1][currj] && matrix[curri-1][currj] == matrix[curri][currj])
{
visited[curri-1][currj] = true;
stack.push((curri-1)*N + currj);
regsize++;
regid[curri-1][currj] = currregid;
}
}
if(curri < N-1)
{
if(!visited[curri+1][currj] && matrix[curri+1][currj] == matrix[curri][currj])
{
visited[curri+1][currj] = true;
stack.push((curri+1)*N + currj);
regsize++;
regid[curri+1][currj] = currregid;
}
}
if(currj > 0)
{
if(!visited[curri][currj-1] && matrix[curri][currj-1] == matrix[curri][currj])
{
visited[curri][currj-1] = true;
stack.push(curri*N + currj-1);
regsize++;
regid[curri][currj-1] = currregid;
}
}
if(currj < N-1)
{
if(!visited[curri][currj+1] && matrix[curri][currj+1] == matrix[curri][currj])
{
visited[curri][currj+1] = true;
stack.push(curri*N + currj+1);
regsize++;
regid[curri][currj+1] = currregid;
}
}
}
regsizes.add(regsize);
maxsize = Math.max(maxsize, regsize);
currregid++;
}
}
HashMap<multimooPair, HashMap<Integer, HashSet<Integer>>> graphs = new HashMap<multimooPair, HashMap<Integer, HashSet<Integer>>>();
/*
int[] cownums = new int[cows.size()];
for(int i = 0; i < cownums.length; i++)
{
cownums[i] = cows.pollFirst();
}
for(int i = 0; i < cownums.length; i++)
{
for(int j = i+1; j < cownums.length; j++)
{
graphs.put(new multimooPair(cownums[i], cownums[j]), new HashMap<Integer, HashSet<Integer>>());
}
}
*/
for(int i = 0; i < N*N; i++)
{
curri = i/N;
currj = i%N;
if(curri > 0)
{
if(regid[curri-1][currj] != regid[curri][currj])
{
if(!graphs.containsKey(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj])))) graphs.put(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj])), new HashMap<Integer, HashSet<Integer>>());
if(!(graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).containsKey(regid[curri][currj])))
{
graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).put(regid[curri][currj], new HashSet<Integer>());
}
if(!(graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).containsKey(regid[curri-1][currj])))
{
graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).put(regid[curri-1][currj], new HashSet<Integer>());
}
graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).get(regid[curri][currj]).add(regid[curri-1][currj]);
graphs.get(new multimooPair(Math.min(matrix[curri-1][currj], matrix[curri][currj]), Math.max(matrix[curri-1][currj], matrix[curri][currj]))).get(regid[curri-1][currj]).add(regid[curri][currj]);
}
}
if(curri < N-1)
{
if(regid[curri+1][currj] != regid[curri][currj]) {
if(!graphs.containsKey(new multimooPair(Math.min(matrix[curri+1][currj], matrix[curri][currj]), Math.max(matrix[curri+1][currj], matrix[curri][currj])))) graphs.put(new multimooPair(Math.min(matrix[curri+1][currj], matrix[curri][currj]), Math.max(matrix[curri+1][currj], matrix[curri][currj])), new HashMap<Integer, HashSet<Integer>>());
if (!(graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).containsKey(regid[curri][currj]))) {
graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).put(regid[curri][currj], new HashSet<Integer>());
}
if (!(graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).containsKey(regid[curri + 1][currj]))) {
graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).put(regid[curri + 1][currj], new HashSet<Integer>());
}
if (regid[curri + 1][currj] != regid[curri][currj]) {
graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).get(regid[curri][currj]).add(regid[curri + 1][currj]);
graphs.get(new multimooPair(Math.min(matrix[curri + 1][currj], matrix[curri][currj]), Math.max(matrix[curri + 1][currj], matrix[curri][currj]))).get(regid[curri + 1][currj]).add(regid[curri][currj]);
}
}
}
if(currj > 0)
{
if(regid[curri][currj-1] != regid[curri][currj]) {
if(!graphs.containsKey(new multimooPair(Math.min(matrix[curri][currj-1], matrix[curri][currj]), Math.max(matrix[curri][currj-1], matrix[curri][currj])))) graphs.put(new multimooPair(Math.min(matrix[curri][currj-1], matrix[curri][currj]), Math.max(matrix[curri][currj-1], matrix[curri][currj])), new HashMap<Integer, HashSet<Integer>>());
if (!(graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).containsKey(regid[curri][currj]))) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).put(regid[curri][currj], new HashSet<Integer>());
}
if (!(graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).containsKey(regid[curri][currj - 1]))) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).put(regid[curri][currj - 1], new HashSet<Integer>());
}
if (regid[curri][currj - 1] != regid[curri][currj]) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).get(regid[curri][currj]).add(regid[curri][currj - 1]);
graphs.get(new multimooPair(Math.min(matrix[curri][currj - 1], matrix[curri][currj]), Math.max(matrix[curri][currj - 1], matrix[curri][currj]))).get(regid[curri][currj - 1]).add(regid[curri][currj]);
}
}
}
if(currj < N-1)
{
if(regid[curri][currj+1] != regid[curri][currj]) {
if(!graphs.containsKey(new multimooPair(Math.min(matrix[curri][currj+1], matrix[curri][currj]), Math.max(matrix[curri][currj+1], matrix[curri][currj])))) graphs.put(new multimooPair(Math.min(matrix[curri][currj+1], matrix[curri][currj]), Math.max(matrix[curri][currj+1], matrix[curri][currj])), new HashMap<Integer, HashSet<Integer>>());
if (!(graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).containsKey(regid[curri][currj]))) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).put(regid[curri][currj], new HashSet<Integer>());
}
if (!(graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).containsKey(regid[curri][currj + 1]))) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).put(regid[curri][currj + 1], new HashSet<Integer>());
}
if (regid[curri][currj + 1] != regid[curri][currj]) {
graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).get(regid[curri][currj]).add(regid[curri][currj + 1]);
graphs.get(new multimooPair(Math.min(matrix[curri][currj + 1], matrix[curri][currj]), Math.max(matrix[curri][currj + 1], matrix[curri][currj]))).get(regid[curri][currj + 1]).add(regid[curri][currj]);
}
}
}
}
int maxsize2 = 0;
for(HashMap<Integer, HashSet<Integer>> h : graphs.values())
{
maxsize2 = Math.max(maxsize2, dfs(h));
}
writer.println(maxsize);
writer.println(maxsize2);
reader.close();
writer.close();
}
static int dfs(HashMap<Integer, HashSet<Integer>> adj)
{
HashSet<Integer> visited = new HashSet<Integer>();
Stack<Integer> stack;
int curr;
int maxsize = 0;
int size;
for(int i : adj.keySet())
{
if(!visited.contains(i))
{
size = regsizes.get(i);
visited.add(i);
stack = new Stack<Integer>();
stack.push(i);
while(!stack.isEmpty())
{
curr = stack.pop();
for(int j : adj.get(curr))
{
if(!visited.contains(j))
{
visited.add(j);
stack.push(j);
size += regsizes.get(j);
}
}
}
maxsize = Math.max(maxsize, size);
}
}
return maxsize;
}
}
class multimooPair implements Comparable<multimooPair>
{
int a, b;
public multimooPair(int c, int d)
{
a = c;
b = d;
}
@Override
public int compareTo(multimooPair m)
{
return a == m.a ? b - m.b : a - m.a;
}
@Override
public boolean equals(Object m)
{
return m instanceof multimooPair && compareTo((multimooPair) m) == 0;
}
@Override
public int hashCode()
{
int result = 17;
result = 31 * result + a;
result = 31 * result + b;
return result;
}
}