import java.util.ArrayList;
import java.util.Arrays;
public class supertrees {
int construct(int[][] p) {
int n = p.length;
if (areAllOnes(p)) {
return constructForAllOnes(n);
}
if (areAllAtMostOnes(p)) {
return constructForAllAtMostOnes(p, n);
}
if (areAllZeroOrTwo(p)) {
return constructAllZeroOrTwo(p, n);
}
if (areAllAtMostTwo(p)) {
return constructAtMostTwo(p, n);
}
int[][] answer = new int[n][n];
grader.build(answer);
return 1;
}
int constructAtMostTwo(int[][] p, int n) {
int[] comp = new int[n];
Arrays.fill(comp, -1);
int c = 0;
for (int i = 0; i < n; i++) {
if (comp[i] != -1) continue;
comp[i] = c;
for (int j = i + 1; j < n; j++) {
if (p[i][j] > 0) {
if (comp[j] != -1) return 0;
comp[j] = c;
}
}
c++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] > 0 && comp[i] != comp[j]) return 0;
if (p[i][j] == 0 && comp[i] == comp[j]) return 0;
}
}
ArrayList<Integer>[] C = new ArrayList[c];
for (int i = 0; i < c; i++) {
C[i] = new ArrayList<>();
}
for (int i = 0; i < n; i++) {
C[comp[i]].add(i);
}
int[][] b = new int[n][n];
for (int i = 0; i < c; i++) {
if (C[i].size() <= 1) continue;
ArrayList<Integer> nodes = C[i];
int first = nodes.get(0);
ArrayList<Integer> d1 = new ArrayList<>();
ArrayList<Integer> d2 = new ArrayList<>();
for (int j = 1; j < nodes.size(); j++) {
int nd = nodes.get(j);
if (p[first][nd] == 1) {
d1.add(nd);
} else {
d2.add(nd);
}
}
for (int dist1 : d1) {
b[dist1][first] = 1;
b[first][dist1] = 1;
}
if (d2.isEmpty()) {
grader.build(b);
return 1;
}
int b1 = d2.get(0);
b[b1][first] = 1;
b[first][b1] = 1;
ArrayList<Integer> B1 = new ArrayList<>();
ArrayList<Integer> B2 = new ArrayList<>();
for (int j = 1; j < d2.size(); j++) {
int nd = d2.get(j);
if (p[nd][b1] == 1) {
B1.add(nd);
} else {
B2.add(nd);
}
}
for (int b2 : B1) {
b[b2][b1] = 1;
b[b1][b2] = 1;
}
B2.add(b1);
for (int j = 0; j < B2.size() - 1; j++) {
int nd1 = B2.get(j), nd2 = B2.get(j + 1);
b[nd1][nd2] = 1;
b[nd2][nd1] = 1;
}
b[first][b1] = 1;
b[b1][first] = 1;
b[first][B2.get(B2.size() - 1)] = 1;
b[B2.get(B2.size() - 1)][first] = 1;
}
grader.build(b);
return 1;
}
int constructAllZeroOrTwo(int[][] p, int n) {
int[] comp = new int[n];
Arrays.fill(comp, -1);
int c = 0;
for (int i = 0; i < n; i++) {
if (comp[i] != -1) continue;
comp[i] = c;
for (int j = i + 1; j < n; j++) {
if (p[i][j] > 0) {
if (comp[j] != -1) return 0;
comp[j] = c;
}
}
c++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] > 0 && comp[i] != comp[j]) return 0;
if (p[i][j] == 0 && comp[i] == comp[j]) return 0;
}
}
ArrayList<Integer>[] C = new ArrayList[c];
for (int i = 0; i < c; i++) {
C[i] = new ArrayList<>();
}
for (int i = 0; i < n; i++) {
C[comp[i]].add(i);
}
int[][] b = new int[n][n];
for (int i = 0; i < c; i++) {
if (C[i].size() <= 1) continue;
if (C[i].size() == 2) return 0;
for (int j = 1; j < C[i].size() - 1; j++) {
b[C[i].get(j)][C[i].get(j + 1)] = 1;
b[C[i].get(j + 1)][C[i].get(j)] = 1;
}
b[C[i].get(0)][C[i].get(1)] = 1;
b[C[i].get(1)][C[i].get(0)] = 1;
b[C[i].get(0)][C[i].get(C[i].size() - 1)] = 1;
b[C[i].get(C[i].size() - 1)][C[i].get(0)] = 1;
}
grader.build(b);
return 1;
}
boolean areAllAtMostTwo(int[][] p) {
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
if (p[i][j] > 2) return false;
}
}
return true;
}
int constructForAllAtMostOnes(int[][] p, int n) {
int[] comp = new int[n];
int[][] b = new int[n][n];
Arrays.fill(comp, -1);
int c = 0;
for (int i = 0; i < n; i++) {
if (comp[i] != -1) continue;
comp[i] = c;
for (int j = i + 1; j < n; j++) {
if (p[i][j] > 0) {
if (comp[j] != -1) return 0;
comp[j] = c;
}
}
c++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] > 0 && comp[i] != comp[j]) return 0;
if (p[i][j] == 0 && comp[i] == comp[j]) return 0;
}
}
ArrayList<Integer>[] C = new ArrayList[c];
for (int i = 0; i < c; i++) {
C[i] = new ArrayList<>();
}
for (int i = 0; i < n; i++) {
C[comp[i]].add(i);
}
for (int i = 0; i < c; i++) {
for (int j = 0; j < C[i].size() - 1; j++) {
b[C[i].get(j)][C[i].get(j + 1)] = 1;
b[C[i].get(j + 1)][C[i].get(j)] = 1;
}
}
grader.build(b);
return 1;
}
boolean areAllZeroOrTwo(int[][] p) {
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
if (j != i && p[i][j] % 2 == 1) return false;
}
}
return true;
}
boolean areAllAtMostOnes(int[][] p) {
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
if (p[i][j] > 1) return false;
}
}
return true;
}
boolean areAllOnes(int[][] p) {
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
if (p[i][j] != 1) return false;
}
}
return true;
}
int constructForAllOnes(int n) {
int[][] res = new int[n][n];
for (int i = 0; i < n - 1; i++) {
res[i][i + 1] = 1;
res[i + 1][i] = 1;
}
grader.build(res);
return 1;
}
}
Compilation message
Note: supertrees.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10340 KB |
Output is correct |
2 |
Correct |
84 ms |
10344 KB |
Output is correct |
3 |
Correct |
80 ms |
10232 KB |
Output is correct |
4 |
Correct |
80 ms |
10232 KB |
Output is correct |
5 |
Correct |
78 ms |
9844 KB |
Output is correct |
6 |
Correct |
190 ms |
15992 KB |
Output is correct |
7 |
Correct |
417 ms |
56772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10340 KB |
Output is correct |
2 |
Correct |
84 ms |
10344 KB |
Output is correct |
3 |
Correct |
80 ms |
10232 KB |
Output is correct |
4 |
Correct |
80 ms |
10232 KB |
Output is correct |
5 |
Correct |
78 ms |
9844 KB |
Output is correct |
6 |
Correct |
190 ms |
15992 KB |
Output is correct |
7 |
Correct |
417 ms |
56772 KB |
Output is correct |
8 |
Correct |
77 ms |
10232 KB |
Output is correct |
9 |
Correct |
83 ms |
10344 KB |
Output is correct |
10 |
Correct |
82 ms |
10344 KB |
Output is correct |
11 |
Correct |
77 ms |
10228 KB |
Output is correct |
12 |
Correct |
144 ms |
15080 KB |
Output is correct |
13 |
Correct |
426 ms |
56332 KB |
Output is correct |
14 |
Correct |
79 ms |
10232 KB |
Output is correct |
15 |
Correct |
81 ms |
10344 KB |
Output is correct |
16 |
Correct |
97 ms |
10976 KB |
Output is correct |
17 |
Correct |
145 ms |
19552 KB |
Output is correct |
18 |
Correct |
81 ms |
10376 KB |
Output is correct |
19 |
Correct |
82 ms |
9976 KB |
Output is correct |
20 |
Correct |
306 ms |
28012 KB |
Output is correct |
21 |
Correct |
417 ms |
57000 KB |
Output is correct |
22 |
Correct |
401 ms |
56052 KB |
Output is correct |
23 |
Correct |
413 ms |
56132 KB |
Output is correct |
24 |
Correct |
423 ms |
57160 KB |
Output is correct |
25 |
Correct |
165 ms |
19296 KB |
Output is correct |
26 |
Correct |
157 ms |
19208 KB |
Output is correct |
27 |
Correct |
421 ms |
57080 KB |
Output is correct |
28 |
Correct |
427 ms |
57324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
10484 KB |
Output is correct |
2 |
Correct |
78 ms |
10344 KB |
Output is correct |
3 |
Correct |
79 ms |
10232 KB |
Output is correct |
4 |
Correct |
76 ms |
10104 KB |
Output is correct |
5 |
Correct |
79 ms |
10100 KB |
Output is correct |
6 |
Correct |
77 ms |
10216 KB |
Output is correct |
7 |
Correct |
82 ms |
10488 KB |
Output is correct |
8 |
Correct |
164 ms |
15560 KB |
Output is correct |
9 |
Correct |
484 ms |
56288 KB |
Output is correct |
10 |
Correct |
79 ms |
10232 KB |
Output is correct |
11 |
Correct |
79 ms |
10216 KB |
Output is correct |
12 |
Correct |
136 ms |
13820 KB |
Output is correct |
13 |
Correct |
469 ms |
57440 KB |
Output is correct |
14 |
Correct |
78 ms |
10340 KB |
Output is correct |
15 |
Correct |
81 ms |
10104 KB |
Output is correct |
16 |
Correct |
93 ms |
10868 KB |
Output is correct |
17 |
Correct |
133 ms |
15224 KB |
Output is correct |
18 |
Correct |
79 ms |
10216 KB |
Output is correct |
19 |
Correct |
78 ms |
10312 KB |
Output is correct |
20 |
Correct |
81 ms |
10216 KB |
Output is correct |
21 |
Correct |
304 ms |
25892 KB |
Output is correct |
22 |
Correct |
463 ms |
57092 KB |
Output is correct |
23 |
Correct |
404 ms |
56508 KB |
Output is correct |
24 |
Correct |
487 ms |
56236 KB |
Output is correct |
25 |
Correct |
172 ms |
19960 KB |
Output is correct |
26 |
Correct |
145 ms |
15100 KB |
Output is correct |
27 |
Correct |
435 ms |
56936 KB |
Output is correct |
28 |
Correct |
454 ms |
57556 KB |
Output is correct |
29 |
Correct |
177 ms |
19944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
10360 KB |
Output is correct |
2 |
Correct |
78 ms |
10352 KB |
Output is correct |
3 |
Correct |
80 ms |
10232 KB |
Output is correct |
4 |
Correct |
298 ms |
25248 KB |
Output is correct |
5 |
Correct |
410 ms |
56352 KB |
Output is correct |
6 |
Correct |
443 ms |
56784 KB |
Output is correct |
7 |
Correct |
459 ms |
57060 KB |
Output is correct |
8 |
Correct |
97 ms |
10360 KB |
Output is correct |
9 |
Correct |
347 ms |
25752 KB |
Output is correct |
10 |
Correct |
434 ms |
56244 KB |
Output is correct |
11 |
Correct |
445 ms |
57596 KB |
Output is correct |
12 |
Correct |
419 ms |
56484 KB |
Output is correct |
13 |
Correct |
77 ms |
10360 KB |
Output is correct |
14 |
Incorrect |
79 ms |
10180 KB |
Too few ways to get from 0 to 1, should be 2 found 1 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10340 KB |
Output is correct |
2 |
Correct |
84 ms |
10344 KB |
Output is correct |
3 |
Correct |
80 ms |
10232 KB |
Output is correct |
4 |
Correct |
80 ms |
10232 KB |
Output is correct |
5 |
Correct |
78 ms |
9844 KB |
Output is correct |
6 |
Correct |
190 ms |
15992 KB |
Output is correct |
7 |
Correct |
417 ms |
56772 KB |
Output is correct |
8 |
Correct |
77 ms |
10232 KB |
Output is correct |
9 |
Correct |
83 ms |
10344 KB |
Output is correct |
10 |
Correct |
82 ms |
10344 KB |
Output is correct |
11 |
Correct |
77 ms |
10228 KB |
Output is correct |
12 |
Correct |
144 ms |
15080 KB |
Output is correct |
13 |
Correct |
426 ms |
56332 KB |
Output is correct |
14 |
Correct |
79 ms |
10232 KB |
Output is correct |
15 |
Correct |
81 ms |
10344 KB |
Output is correct |
16 |
Correct |
97 ms |
10976 KB |
Output is correct |
17 |
Correct |
145 ms |
19552 KB |
Output is correct |
18 |
Correct |
81 ms |
10376 KB |
Output is correct |
19 |
Correct |
82 ms |
9976 KB |
Output is correct |
20 |
Correct |
306 ms |
28012 KB |
Output is correct |
21 |
Correct |
417 ms |
57000 KB |
Output is correct |
22 |
Correct |
401 ms |
56052 KB |
Output is correct |
23 |
Correct |
413 ms |
56132 KB |
Output is correct |
24 |
Correct |
423 ms |
57160 KB |
Output is correct |
25 |
Correct |
165 ms |
19296 KB |
Output is correct |
26 |
Correct |
157 ms |
19208 KB |
Output is correct |
27 |
Correct |
421 ms |
57080 KB |
Output is correct |
28 |
Correct |
427 ms |
57324 KB |
Output is correct |
29 |
Correct |
77 ms |
10484 KB |
Output is correct |
30 |
Correct |
78 ms |
10344 KB |
Output is correct |
31 |
Correct |
79 ms |
10232 KB |
Output is correct |
32 |
Correct |
76 ms |
10104 KB |
Output is correct |
33 |
Correct |
79 ms |
10100 KB |
Output is correct |
34 |
Correct |
77 ms |
10216 KB |
Output is correct |
35 |
Correct |
82 ms |
10488 KB |
Output is correct |
36 |
Correct |
164 ms |
15560 KB |
Output is correct |
37 |
Correct |
484 ms |
56288 KB |
Output is correct |
38 |
Correct |
79 ms |
10232 KB |
Output is correct |
39 |
Correct |
79 ms |
10216 KB |
Output is correct |
40 |
Correct |
136 ms |
13820 KB |
Output is correct |
41 |
Correct |
469 ms |
57440 KB |
Output is correct |
42 |
Correct |
78 ms |
10340 KB |
Output is correct |
43 |
Correct |
81 ms |
10104 KB |
Output is correct |
44 |
Correct |
93 ms |
10868 KB |
Output is correct |
45 |
Correct |
133 ms |
15224 KB |
Output is correct |
46 |
Correct |
79 ms |
10216 KB |
Output is correct |
47 |
Correct |
78 ms |
10312 KB |
Output is correct |
48 |
Correct |
81 ms |
10216 KB |
Output is correct |
49 |
Correct |
304 ms |
25892 KB |
Output is correct |
50 |
Correct |
463 ms |
57092 KB |
Output is correct |
51 |
Correct |
404 ms |
56508 KB |
Output is correct |
52 |
Correct |
487 ms |
56236 KB |
Output is correct |
53 |
Correct |
172 ms |
19960 KB |
Output is correct |
54 |
Correct |
145 ms |
15100 KB |
Output is correct |
55 |
Correct |
435 ms |
56936 KB |
Output is correct |
56 |
Correct |
454 ms |
57556 KB |
Output is correct |
57 |
Correct |
177 ms |
19944 KB |
Output is correct |
58 |
Correct |
80 ms |
10352 KB |
Output is correct |
59 |
Correct |
81 ms |
10232 KB |
Output is correct |
60 |
Correct |
95 ms |
10872 KB |
Output is correct |
61 |
Correct |
137 ms |
15212 KB |
Output is correct |
62 |
Correct |
81 ms |
10488 KB |
Output is correct |
63 |
Incorrect |
82 ms |
10320 KB |
Too few ways to get from 3 to 4, should be 2 found 0 |
64 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10340 KB |
Output is correct |
2 |
Correct |
84 ms |
10344 KB |
Output is correct |
3 |
Correct |
80 ms |
10232 KB |
Output is correct |
4 |
Correct |
80 ms |
10232 KB |
Output is correct |
5 |
Correct |
78 ms |
9844 KB |
Output is correct |
6 |
Correct |
190 ms |
15992 KB |
Output is correct |
7 |
Correct |
417 ms |
56772 KB |
Output is correct |
8 |
Correct |
77 ms |
10232 KB |
Output is correct |
9 |
Correct |
83 ms |
10344 KB |
Output is correct |
10 |
Correct |
82 ms |
10344 KB |
Output is correct |
11 |
Correct |
77 ms |
10228 KB |
Output is correct |
12 |
Correct |
144 ms |
15080 KB |
Output is correct |
13 |
Correct |
426 ms |
56332 KB |
Output is correct |
14 |
Correct |
79 ms |
10232 KB |
Output is correct |
15 |
Correct |
81 ms |
10344 KB |
Output is correct |
16 |
Correct |
97 ms |
10976 KB |
Output is correct |
17 |
Correct |
145 ms |
19552 KB |
Output is correct |
18 |
Correct |
81 ms |
10376 KB |
Output is correct |
19 |
Correct |
82 ms |
9976 KB |
Output is correct |
20 |
Correct |
306 ms |
28012 KB |
Output is correct |
21 |
Correct |
417 ms |
57000 KB |
Output is correct |
22 |
Correct |
401 ms |
56052 KB |
Output is correct |
23 |
Correct |
413 ms |
56132 KB |
Output is correct |
24 |
Correct |
423 ms |
57160 KB |
Output is correct |
25 |
Correct |
165 ms |
19296 KB |
Output is correct |
26 |
Correct |
157 ms |
19208 KB |
Output is correct |
27 |
Correct |
421 ms |
57080 KB |
Output is correct |
28 |
Correct |
427 ms |
57324 KB |
Output is correct |
29 |
Correct |
77 ms |
10484 KB |
Output is correct |
30 |
Correct |
78 ms |
10344 KB |
Output is correct |
31 |
Correct |
79 ms |
10232 KB |
Output is correct |
32 |
Correct |
76 ms |
10104 KB |
Output is correct |
33 |
Correct |
79 ms |
10100 KB |
Output is correct |
34 |
Correct |
77 ms |
10216 KB |
Output is correct |
35 |
Correct |
82 ms |
10488 KB |
Output is correct |
36 |
Correct |
164 ms |
15560 KB |
Output is correct |
37 |
Correct |
484 ms |
56288 KB |
Output is correct |
38 |
Correct |
79 ms |
10232 KB |
Output is correct |
39 |
Correct |
79 ms |
10216 KB |
Output is correct |
40 |
Correct |
136 ms |
13820 KB |
Output is correct |
41 |
Correct |
469 ms |
57440 KB |
Output is correct |
42 |
Correct |
78 ms |
10340 KB |
Output is correct |
43 |
Correct |
81 ms |
10104 KB |
Output is correct |
44 |
Correct |
93 ms |
10868 KB |
Output is correct |
45 |
Correct |
133 ms |
15224 KB |
Output is correct |
46 |
Correct |
79 ms |
10216 KB |
Output is correct |
47 |
Correct |
78 ms |
10312 KB |
Output is correct |
48 |
Correct |
81 ms |
10216 KB |
Output is correct |
49 |
Correct |
304 ms |
25892 KB |
Output is correct |
50 |
Correct |
463 ms |
57092 KB |
Output is correct |
51 |
Correct |
404 ms |
56508 KB |
Output is correct |
52 |
Correct |
487 ms |
56236 KB |
Output is correct |
53 |
Correct |
172 ms |
19960 KB |
Output is correct |
54 |
Correct |
145 ms |
15100 KB |
Output is correct |
55 |
Correct |
435 ms |
56936 KB |
Output is correct |
56 |
Correct |
454 ms |
57556 KB |
Output is correct |
57 |
Correct |
177 ms |
19944 KB |
Output is correct |
58 |
Correct |
80 ms |
10360 KB |
Output is correct |
59 |
Correct |
78 ms |
10352 KB |
Output is correct |
60 |
Correct |
80 ms |
10232 KB |
Output is correct |
61 |
Correct |
298 ms |
25248 KB |
Output is correct |
62 |
Correct |
410 ms |
56352 KB |
Output is correct |
63 |
Correct |
443 ms |
56784 KB |
Output is correct |
64 |
Correct |
459 ms |
57060 KB |
Output is correct |
65 |
Correct |
97 ms |
10360 KB |
Output is correct |
66 |
Correct |
347 ms |
25752 KB |
Output is correct |
67 |
Correct |
434 ms |
56244 KB |
Output is correct |
68 |
Correct |
445 ms |
57596 KB |
Output is correct |
69 |
Correct |
419 ms |
56484 KB |
Output is correct |
70 |
Correct |
77 ms |
10360 KB |
Output is correct |
71 |
Incorrect |
79 ms |
10180 KB |
Too few ways to get from 0 to 1, should be 2 found 1 |
72 |
Halted |
0 ms |
0 KB |
- |