1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class Main{ static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static List<List<Integer>> g = new ArrayList<>(); static boolean[] vis = new boolean[501]; static int[] step ; static boolean flag = false; static int a , b , u , v , n , m ; public static void main(String[] args) throws IOException { String[] split = in.readLine().split(" "); n = Integer.parseInt(split[0]); m = Integer.parseInt(split[1]); step = new int[n+1]; for (int i = 0; i <= n; i++) { g.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { split = in.readLine().split(" "); u = Integer.parseInt(split[0]); v = Integer.parseInt(split[1]); g.get(u).add(v); } split = in.readLine().split(" "); a = Integer.parseInt(split[0]); b = Integer.parseInt(split[1]); Arrays.fill(step , 0); step[b] = 1; vis[b] = true; dfs(a); System.out.print(step[a]); if (flag){ System.out.println(" No"); }else { System.out.println(" Yes"); } } private static void dfs(int cur) { vis[cur] = true; if (g.get(cur).size() == 0 && cur != b){ flag = true; return; } for (int next : g.get(cur)) { if (!vis[next]){ dfs(next); } step[cur] += step[next]; } } }
|