-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlmostAcyclic.java
More file actions
56 lines (49 loc) · 1.78 KB
/
Copy pathAlmostAcyclic.java
File metadata and controls
56 lines (49 loc) · 1.78 KB
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
/**
* Created by MalhotR1 on 02/06/2018.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class AlmostAcyclic {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String[] input = br.readLine().trim().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[0]);
int[] in = new int[n+1];
int[] out = new int[n+1];
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<>();
for (int i = 0; i < m; i++) {
String[] temp = br.readLine().trim().split(" ");
int v1 = Integer.parseInt(temp[0]);
int v2 = Integer.parseInt(temp[1]);
out[v1]++;
in[v2]++;
if (hm.containsKey(v1)) {
ArrayList<Integer> al = hm.get(v1);
al.add(v2);
} else {
ArrayList<Integer> al = new ArrayList<>();
al.add(v2);
hm.put(v1, al);
}
}
boolean flag = false;
for (int i = 1; i < in.length && !flag; i++) {
if (in[i] == 1 && hm.containsKey(i)) {
ArrayList<Integer> al = hm.get(i);
for (Integer v : al) {
if (out[v] == 1) {
flag = true;
break;
}
}
}
}
if (flag) System.out.println("YES");
else System.out.println("NO");
}
}
}