CSES - E4590 2016 2 - Results
Submission details
Task:Freshman's Database
Sender:Peng Liu
Submission time:2016-09-24 15:40:01 +0300
Language:Java
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.10 sdetails
#2ACCEPTED0.10 sdetails
#3ACCEPTED0.86 sdetails
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--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:

input
1000000
227998 891986 290950 887622 37...

correct output
6736

user output
(empty)

Test 5

Verdict:

input
1000000
832833 455297 341097 88590 258...

correct output
16

user output
(empty)

Test 6

Verdict:

input
1000000
635299 635243 476863 88031 195...

correct output
73

user output
(empty)

Test 7

Verdict:

input
1000000
444011 366349 710148 901981 81...

correct output
244

user output
(empty)

Test 8

Verdict:

input
1000000
248398 271880 881725 521008 33...

correct output
332

user output
(empty)

Test 9

Verdict:

input
999999
938280 731633 536902 381480 65...

correct output
6771

user output
(empty)

Test 10

Verdict:

input
999999
196127 288846 245904 406819 13...

correct output
105

user output
(empty)