| Task: | Course Schedule |
| Sender: | aalto26fh_027 |
| Submission time: | 2026-10-06 19:32:52 +0300 |
| Language: | Java |
| Status: | READY |
| Result: | TIME LIMIT EXCEEDED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.08 s | details |
| #2 | ACCEPTED | 0.08 s | details |
| #3 | ACCEPTED | 0.07 s | details |
| #4 | ACCEPTED | 0.08 s | details |
| #5 | ACCEPTED | 0.05 s | details |
| #6 | TIME LIMIT EXCEEDED | -- | details |
| #7 | TIME LIMIT EXCEEDED | -- | details |
| #8 | TIME LIMIT EXCEEDED | -- | details |
| #9 | TIME LIMIT EXCEEDED | -- | details |
| #10 | ACCEPTED | 0.50 s | details |
| #11 | TIME LIMIT EXCEEDED | -- | details |
| #12 | ACCEPTED | 0.05 s | details |
| #13 | ACCEPTED | 0.05 s | details |
| #14 | TIME LIMIT EXCEEDED | -- | details |
| #15 | TIME LIMIT EXCEEDED | -- | details |
| #16 | ACCEPTED | 0.05 s | details |
| #17 | ACCEPTED | 0.44 s | details |
Compiler report
Note: input/CourseSchedule.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class CourseSchedule {
public static void main(String[] args) {
FastScanner input = new FastScanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int[][] requirements = new int[m][2];
for(int i = 0; i < m; i++){
requirements[i][0] = input.nextInt();
requirements[i][1] = input.nextInt();
}
int[] output = new int[n];
ArrayList<Integer>[] graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
int[] indegree = new int[n];
for (int i = 0; i < m; i++) {
int a = requirements[i][0] - 1;
int b = requirements[i][1] - 1;
graph[a].add(b);
indegree[b]++;
}
ArrayDeque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
if (indegree[i] == 0) {
queue.add(i);
}
}
int count = 0;
while (!queue.isEmpty()) {
int course = queue.poll();
output[count] = course + 1;
count++;
for (int next : graph[course]) {
indegree[next]--;
if (indegree[next] == 0) {
queue.add(next);
}
}
}
if (count != n) {
System.out.println("IMPOSSIBLE");
} else {
for (int i = 0; i < n; i++) {
System.out.print(output[i] + " ");
}
}
}
static class FastScanner { //static class fastScanner
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 20 5 2 2 4 8 9 6 4 ... |
| correct output |
|---|
| 5 7 10 2 1 8 3 9 6 4 |
| user output |
|---|
| 5 7 10 2 1 8 3 9 6 4 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 20 2 7 1 10 9 5 9 7 ... |
| correct output |
|---|
| 1 8 3 6 10 2 9 4 5 7 |
| user output |
|---|
| 1 8 3 6 10 2 9 4 5 7 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 20 8 5 2 3 10 1 9 1 ... |
| correct output |
|---|
| 4 6 7 9 10 2 8 3 1 5 |
| user output |
|---|
| 4 6 7 9 10 2 8 3 1 5 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 20 5 10 10 3 9 10 6 2 ... |
| correct output |
|---|
| 7 8 6 4 2 1 5 9 10 3 |
| user output |
|---|
| 7 8 6 4 2 1 5 9 10 3 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 20 2 9 4 8 9 1 10 6 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 6
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 78359 8853 18190 30703 11401 30087 34627 11535 ... |
| correct output |
|---|
| 2 3 8 9 16 18 21 22 27 34 36 4... |
| user output |
|---|
| (empty) |
Test 7
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 32395 2098 67067 31866 31867 67167 78488 33397 ... |
| correct output |
|---|
| 9 11 13 16 22 35 37 38 40 44 5... |
| user output |
|---|
| (empty) |
Test 8
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 19035 36947 13730 46121 99449 77790 15626 11731 ... |
| correct output |
|---|
| 1 7 15 17 18 34 38 41 48 49 51... |
| user output |
|---|
| (empty) |
Test 9
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 14188 9709 46541 20871 32203 88809 99879 54779 ... |
| correct output |
|---|
| 6 10 11 16 17 19 21 22 23 28 3... |
| user output |
|---|
| (empty) |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 41882 61162 28138 18053 74649 74863 69760 74508 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 11
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 199998 1 100000 1 100000 2 100000 2 100000 ... |
| correct output |
|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| user output |
|---|
| (empty) |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 2 2 1 2 2 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 6 6 1 2 2 3 4 3 4 5 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 14
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 99999 149997 1 3 3 5 5 7 7 9 ... |
| correct output |
|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| user output |
|---|
| (empty) |
Test 15
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 149998 2 1 3 2 4 3 5 4 ... |
| correct output |
|---|
| 100000 99999 99998 99997 99996... |
| user output |
|---|
| (empty) |
Test 16
Verdict: ACCEPTED
| input |
|---|
| 6 6 1 2 1 3 2 4 3 5 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 17
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 1 1 1 1 2 2 2 2 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
