CSES - KILO 2016 3/5 - Results
Submission details
Task:Ice cream
Sender:michaeljackson123
Submission time:2016-09-20 18:20:58 +0300
Language:Java
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.32 sdetails
#2ACCEPTED0.32 sdetails
#3ACCEPTED0.46 sdetails
#4ACCEPTED0.34 sdetails
#5ACCEPTED0.11 sdetails
#6ACCEPTED0.37 sdetails
#7ACCEPTED0.28 sdetails
#8ACCEPTED0.41 sdetails
#9ACCEPTED0.36 sdetails
#10ACCEPTED0.38 sdetails
#11ACCEPTED0.35 sdetails
#12ACCEPTED0.41 sdetails
#13ACCEPTED0.43 sdetails
#14ACCEPTED0.49 sdetails
#15ACCEPTED0.40 sdetails
#16ACCEPTED0.41 sdetails
#17ACCEPTED0.42 sdetails
#18ACCEPTED0.43 sdetails
#19ACCEPTED0.47 sdetails
#20ACCEPTED0.43 sdetails

Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//package javaapplication8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

/**
 *
 * @author tuukkatu
 */
public class JavaApplication8 {

    public static int[] visited;
    public static ArrayList<ArrayList> graph;
    //public static IO io;
    public static int count;
    public static int[] id;
    public static ArrayList<HashSet> iceCreamsInComponent;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        IO io = new IO();
	
	//String a = io.next(); // Lukee seuraavan välein erotellun merkkijonon.
	//int b = io.nextInt(); // Lukee seuraavan välein erotellun int-kokonaisluvun.
	//long c = io.nextLong(); // Lukee seuraavan välein erotellun long-kokonaisluvun.
	//double d = io.nextDouble(); // Lukee seuraavan välein erotellun double-liukuluvun.
	
	// Toimii kuten System.out.println.
        
        int n = io.nextInt();
        int m = io.nextInt();
        
        int i = 0;
        
        ArrayList<Integer> iceCreams = new ArrayList();
        iceCreamsInComponent = new ArrayList();
        graph = new ArrayList();
        
        
        for (int j = 0; j <= n; j++) {
            ArrayList<Integer> listToAdd = new ArrayList();
            HashSet<Integer> setToAdd = new HashSet();
            if (j > 0) iceCreams.add(io.nextInt());
            else iceCreams.add(0);
            
            graph.add(listToAdd);
            iceCreamsInComponent.add(setToAdd);
        }
        
        //while (true) {
        //    if (i == n + 1) {
        //        break;
        //    }
            
        //    iceCreams.add(io.nextInt());
            
        //    i++;
        //}
        
        /* for (int j = 1; j <= n; j++) {
            iceCreams.add(io.nextInt());
        } */
        
        i = 0;
        
        //System.out.println("mau");
        
        /*while (true) {
            if (i == m + 1) {
                break;
            }
            
            int a = io.nextInt();
            int b = io.nextInt();
            
            graph.get(a).add(b);
            graph.get(b).add(a);
            
            i++;
        }*/
        
        for (int j = 1; j <= m; j++) {
            
            int a = io.nextInt();
            int b = io.nextInt();
            
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
            
        
        //for (ArrayList node : graph) {
        //    io.println(node);
        //}
        
        visited = new int[n + 1];
        id = new int[n + 1]; // id of component
        count = 1;
        
        for (int j = 1; j < graph.size(); j++) {
            if (visited[j] == 1) {
                continue;
            }
            
            dfs(j);
            count++;
        }
        
        //io.println("komponentit");
        //io.println(Arrays.toString(id));
        
        for (int j = 1; j <= n; j++) {
            int iceCream = iceCreams.get(j);
            
            int component = id[j];
            
            //io.println("component of " + j + " is " + component);
            
            iceCreamsInComponent.get(component).add(iceCream);
        }
                
        //io.println(iceCreamsInComponent);
        
        for (int j = 1; j <= n; j++) {
            // j = kaupunki
            
            int component = id[j];
            
            io.println(iceCreamsInComponent.get(component).size());
        }
        
        
        //for (ArrayList node : graph) {
        //    
        //}
        
        io.close(); // TÄYTYY KUTSUA LOPUKSI, muuten tuloste voi jäädä kirjoittamatta

    }
    
    private static void dfs(int s) {
        if (visited[s] == 1) {
            return;
        }
        
        visited[s] = 1;
        id[s] = count;
        
        for (int i = 0; i < graph.get(s).size(); i++) {
            int node = (int) graph.get(s).get(i);
            dfs(node);
        }
        
    }
    
}


Test details

Test 1

Verdict: ACCEPTED

input
47623 1000
6085 3581 2784 1594 9991 7789 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 2

Verdict: ACCEPTED

input
29933 10000
37075 97477 24892 91827 86196 ...

correct output
2 6 1 1 5 1 1 7 9 2 2 2 1 1 3 ...

user output
2
6
1
1
5
...

Test 3

Verdict: ACCEPTED

input
82771 50000
84 19 10 43 44 51 77 9 4 1 21 ...

correct output
2 1 1 1 100 1 1 1 100 100 1 1 ...

user output
2
1
1
1
100
...

Test 4

Verdict: ACCEPTED

input
11645 100000
3041 8953 9167 1268 2312 5911 ...

correct output
6836 6836 6836 6836 6836 6836 ...

user output
6836
6836
6836
6836
6836
...

Test 5

Verdict: ACCEPTED

input
1041 100
43553 8035 14355 19335 24786 7...

correct output
2 1 1 3 1 1 1 2 1 1 1 1 3 1 1 ...

user output
2
1
1
3
1
...

Test 6

Verdict: ACCEPTED

input
55898 1000
73 44 15 26 63 51 15 71 73 48 ...

correct output
1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 7

Verdict: ACCEPTED

input
12027 10000
2456 845 5196 3813 3093 2331 2...

correct output
2 5542 5542 1 5542 5542 5542 5...

user output
2
5542
5542
1
5542
...

Test 8

Verdict: ACCEPTED

input
55977 50000
79310 84043 72794 74090 312 15...

correct output
1 33498 33498 33498 33498 1 1 ...

user output
1
33498
33498
33498
33498
...

Test 9

Verdict: ACCEPTED

input
16237 100000
57 43 3 66 59 10 24 41 56 23 5...

correct output
100 100 100 100 100 100 100 10...

user output
100
100
100
100
100
...

Test 10

Verdict: ACCEPTED

input
93552 100
31 7952 1106 2779 1980 8776 56...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 11

Verdict: ACCEPTED

input
100000 1000
99793 93845 56091 42020 27225 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 12

Verdict: ACCEPTED

input
100000 10000
45 12 5 45 21 73 12 34 97 14 6...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 13

Verdict: ACCEPTED

input
100000 50000
3967 8608 4873 6065 3304 1388 ...

correct output
27 10 2 1 205 2 1 5 752 9 2 26...

user output
27
10
2
1
205
...

Test 14

Verdict: ACCEPTED

input
70000 100000
9570 57890 88434 83611 76325 3...

correct output
1 1 1 47966 47966 47966 47966 ...

user output
1
1
1
47966
47966
...

Test 15

Verdict: ACCEPTED

input
100000 100
84 34 32 36 64 83 29 4 36 57 9...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...

Test 16

Verdict: ACCEPTED

input
100000 1000
7080 7863 895 4001 3806 8637 7...

correct output
1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
2
1
1
...

Test 17

Verdict: ACCEPTED

input
100000 10000
13821 30085 25414 18462 40885 ...

correct output
1 1 3 1 1 2 1 1 1 2 1 1 1 1 1 ...

user output
1
1
3
1
1
...

Test 18

Verdict: ACCEPTED

input
100000 50000
41 89 12 33 73 79 38 48 90 68 ...

correct output
4 1 21 1 2 2 1 1 3 2 3 7 6 20 ...

user output
4
1
21
1
2
...

Test 19

Verdict: ACCEPTED

input
100000 100000
4325 2304 2625 9099 4244 1618 ...

correct output
9994 5 9994 9994 9994 1 9994 9...

user output
9994
5
9994
9994
9994
...

Test 20

Verdict: ACCEPTED

input
100000 100
68725 58766 50437 78768 4415 2...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1
1
1
1
1
...