题目描述
blablabla
样例
import java.util.*;
public class Main{
static int N = 100010;
static int[] e = new int[N];
static int[] ne = new int[N];
static int[] h = new int[N];
static int n,idx;
static int[] d = new int[N];
static ArrayList<Integer> array = new ArrayList<>(); // 记录点
static Queue<Integer> q = new LinkedList();
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int m = sc.nextInt();
Arrays.fill(h,-1);
while(m-- > 0){
int a = sc.nextInt();
int b = sc.nextInt();
add(a,b);
}
for(int i = 1;i <= n;i++){
if(d[i] == 0){
q.offer(i);
array.add(i);
}
}
while(!q.isEmpty()){
int x = q.poll();
for(int i = h[x];i != -1;i = ne[i]){
int j = e[i];
d[j]--;
if(d[j] == 0){
q.offer(j);
array.add(j);
}
}
}
if(array.size() >= n){
for(int i = 0;i < n;i++){
System.out.print(array.get(i) + " ");
}
}else{
System.out.print(-1);
}
}
static void add(int a,int b){
e[idx] = b;ne[idx] = h[a];h[a] = idx++;
d[b]++;
}
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla