Pagini recente » Cod sursa (job #385252) | Cod sursa (job #2250203) | Cod sursa (job #1010225) | Cod sursa (job #2338925) | Cod sursa (job #1705547)
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new FileReader("sortaret.in"));
String temp[] = in.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int M = Integer.parseInt(temp[1]);
List<List<Integer>> g = new ArrayList<>();
for (int i = 1; i <= N; i++) {
g.add(i, new ArrayList<Integer>());
}
for (int i = 0; i < M; i++) {
temp = in.readLine().split(" ");
int x = Integer.parseInt(temp[0]);
int y = Integer.parseInt(temp[1]);
g.get(x).add(y);
}
vis = new int[N + 1];
res = new PriorityQueue<Integer>();
for (int i = 1; i <= N; i++) {
if (vis[i] != 1) {
dfs(i, g);
}
}
OutputStream out = new BufferedOutputStream(new FileOutputStream("sortaret.out"));
while (!res.isEmpty()) {
out.write((res.poll() + " ").getBytes());
}
out.write(("\n").getBytes());
out.flush();
out.close();
in.close();
}
static int[] vis;
static PriorityQueue<Integer> res;
static void dfs(int n, List<List<Integer>> g) {
vis[n] = 1;
for (Integer m : g.get(n)) {
if (vis[m] == 0) {
dfs(m, g);
}
}
res.add(n);
}
}