import java.util.ArrayList;
public class stations {
int[] label(int n, int k, int[] u, int[] v) {
ArrayList<Integer>[] G = new ArrayList[n];
for (int i = 0; i < G.length; i++) {
G[i] = new ArrayList<>();
}
for (int i = 0; i < u.length; i++) {
G[u[i]].add(v[i]);
G[v[i]].add(u[i]);
}
int maxSize = 0;
for (int i = 0; i < n; i++) {
maxSize = Math.max(maxSize, G[i].size());
}
int[] labels = new int[n];
if (maxSize > 0) {
for (int i = 0; i < n; i++) {
labels[i] = i;
}
return labels;
}
int leaf = -1;
for (int i = 0; i < n; i++) {
if (G[i].size() == 1) {
leaf = i;
break;
}
}
int cur = 0;
int node = leaf;
int prev = -1;
while (true) {
labels[node] = cur;
cur++;
boolean found = false;
for (int to : G[node]) {
if (to == prev) continue;
prev = node;
node = to;
found = true;
break;
}
if (!found) break;
}
return labels;
}
boolean isAncestor(int a, int b) {
if (a == b) return true;
while (a > 0) {
a = (a - 1) / 2;
if (a == b) return true;
}
return false;
}
int find_next_station(int s, int t, int[] c) {
if (!isAncestor(t, s)) {
return (s - 1) / 2;
}
for (int nd : c) {
if (nd < s) continue;
if (isAncestor(t, nd)) {
return nd;
}
}
// for (int nd : c) {
// if (s <= nd && nd <= t) return nd;
// if (s >= nd && nd >= t) return nd;
// }
return -1;
}
}
Compilation message
Note: stations.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1277 ms |
35900 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1181 ms |
38232 KB |
Output is correct |
2 |
Correct |
1194 ms |
34232 KB |
Output is correct |
3 |
Correct |
1688 ms |
38872 KB |
Output is correct |
4 |
Correct |
1450 ms |
36324 KB |
Output is correct |
5 |
Correct |
1366 ms |
36232 KB |
Output is correct |
6 |
Correct |
1177 ms |
32256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1442 ms |
36656 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1720 ms |
38956 KB |
Output is correct |
2 |
Incorrect |
1367 ms |
33916 KB |
Wrong query response. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1232 ms |
34344 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |