forked from nivedha-ravi/Code-and-Compile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclic path.java
More file actions
56 lines (54 loc) · 1.05 KB
/
Cyclic path.java
File metadata and controls
56 lines (54 loc) · 1.05 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
/*
Example Input/Output 1:
Input:
4
2 3
5 1
3 5
1 2
Output: YES
Explanation:
The cyclic path formed using the 4 paths is given below.
2 -> 3 -> 5 -> 1 -> 2
So YES is printed as the output.
Example Input/Output 2:
Input:
4
-2 -4
-3 -1
-4 -3
-1 2
Output: NO
*/
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
b[i]=sc.nextInt();
}
int p=b[0];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(a[j]==p && a[j]!=101){
p=b[j];
a[j]=101;b[j]=101;
break;
}
}
}
Set<Integer> l=new TreeSet<Integer>();
for(int i=0;i<n;i++){
l.add(a[i]);
}
if(l.size()==1)
System.out.print("YES");
else
System.out.print("NO");
}
}