题目描述
仅供参考
Java 代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int N = 1010;
static int M = 10010;
static int [] e = new int[M];
static int [] ne = new int[M];
static int [] h = new int[N];
static int idx = 0;
static int n;
static boolean [][] st = new boolean[N][N];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
Arrays.fill(h , -1);
while(m-- != 0){
String[] s1 = br.readLine().split(" ");
int a = Integer.parseInt(s1[0]);
int b = Integer.parseInt(s1[1]);
add(a , b);
}
for(int i = 1;i<=n;i++){
dfs(i,i);
}
int ans =0;
for(int i = 1;i<=n;i++){
if (check(i)){
ans++;
}
}
System.out.println(ans);
}
private static boolean check(int x) {
for(int i = 1; i<=n;i++){
if (!st[x][i] && !st[i][x]){
return false;
}
}
return true;
}
private static void dfs(int u, int fa) {
if (st[fa][u]){
return;
}
st[fa][u] = true;
for(int i = h[u]; i !=-1 ;i = ne[i]){
int j = e[i];
dfs(j,fa);
}
}
private static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
}