Task: | Freshman's Database |
Sender: | Peng Liu |
Submission time: | 2016-09-24 15:40:01 +0300 |
Language: | Java |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.10 s | details |
#2 | ACCEPTED | 0.10 s | details |
#3 | ACCEPTED | 0.86 s | details |
#4 | TIME LIMIT EXCEEDED | -- | details |
#5 | TIME LIMIT EXCEEDED | -- | details |
#6 | TIME LIMIT EXCEEDED | -- | details |
#7 | TIME LIMIT EXCEEDED | -- | details |
#8 | TIME LIMIT EXCEEDED | -- | details |
#9 | TIME LIMIT EXCEEDED | -- | details |
#10 | TIME LIMIT EXCEEDED | -- | details |
Code
// A Sample Java program for beginners with Competitive Programming import java.util.*; import java.lang.*; import java.io.*; class Freshman { static List<HashSet<Integer>> groups; static int[] db; // This function returns index of element x in arr[] static int countLoops() { for(int i=1;i<db.length;i++){ if(alreadyInGroups(i)) { continue; } else { HashSet<Integer> group = new HashSet<Integer>(); addAll(i, group); groups.add(group); } } return groups.size(); } static boolean alreadyInGroups(int i){ for(HashSet<Integer> set : groups){ if(set.contains(i)&&set.contains(db[i])){ return true; } else if(set.contains(i)||set.contains(db[i])){ addAll(i,set); return true; } } return false; } static void addAll(int i, HashSet<Integer> group){ group.add(i); if(group.contains(db[i])){ return; } else { addAll(db[i],group); } } static void printAllGroups(IO io){ for(HashSet<Integer> set : groups){ String s =""; for(Integer i : set){ s= s+","+i; } io.println(s); } } public static void main (String[] args) { // Note that size of arr[] is considered 100 according to // the constraints mentioned in problem statement. groups = new ArrayList<HashSet<Integer>>(); IO io = new IO(); //String a = io.next(); // Reads the next string separated by spaces. int num = io.nextInt(); // Reads the next int separated by spaces. db = new int[num+1]; for(int i=1;i<=num;i++){ db[i]=io.nextInt(); } //String b = io.next(); // Reads the next int separated by spaces. //long c = io.nextLong(); // Reads the next long separated by spaces. //double d = io.nextDouble(); // Reads the next double separated by spaces. io.println(countLoops()); io.close(); // MUST BE CALLED IN THE END, otherwise some of the output may be missing } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
16
2 3 4 5 6 1 4 7 7 4 12 11 14 1... |
correct output |
---|
3 |
user output |
---|
3 |
Test 2
Verdict: ACCEPTED
input |
---|
2
2 1 |
correct output |
---|
1 |
user output |
---|
1 |
Test 3
Verdict: ACCEPTED
input |
---|
1000000 906853 1 1 1 3 4 3 2 5 5 5 10 ... |
correct output |
---|
1 |
user output |
---|
1 |
Test 4
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000 227998 891986 290950 887622 37... |
correct output |
---|
6736 |
user output |
---|
(empty) |
Test 5
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000 832833 455297 341097 88590 258... |
correct output |
---|
16 |
user output |
---|
(empty) |
Test 6
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000 635299 635243 476863 88031 195... |
correct output |
---|
73 |
user output |
---|
(empty) |
Test 7
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000 444011 366349 710148 901981 81... |
correct output |
---|
244 |
user output |
---|
(empty) |
Test 8
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000 248398 271880 881725 521008 33... |
correct output |
---|
332 |
user output |
---|
(empty) |
Test 9
Verdict: TIME LIMIT EXCEEDED
input |
---|
999999 938280 731633 536902 381480 65... |
correct output |
---|
6771 |
user output |
---|
(empty) |
Test 10
Verdict: TIME LIMIT EXCEEDED
input |
---|
999999 196127 288846 245904 406819 13... |
correct output |
---|
105 |
user output |
---|
(empty) |