aboutsummaryrefslogtreecommitdiff
path: root/Practical1/src/nl/camilstaps/cs/GarbageCollectionHelper.java
blob: ec1506d9193c60aae382800219401eeff9fa3c36 (plain) (blame)
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
57
package nl.camilstaps.cs;

import nl.camilstaps.cs.graphs.Graph;
import nl.camilstaps.cs.graphs.Node;

import java.util.Scanner;

/**
 * Solution for the Garbage Collection problem.
 *
 * @author Camil Staps
 */
class GarbageCollectionHelper {

    /**
     * Read in a Graph from stdin and print whether we can place N bins to stdout
     * @param args Command line arguments (are ignored)
     */
    public static void main(String[] args) {
        Graph graph = new Graph<Integer>();
        int n_bins = readGraph(new Scanner(System.in), graph);
        System.out.println(graph.maximumIndependentSetSize() >= n_bins ? "possible" : "impossible");
    }

    /**
     * Read a Graph in the defined format:
     *
     *      [n_edges] [n_nodes] [n_bins]
     *      [fst] [snd]
     *      [fst] [snd]
     *      [fst] [snd]
     *      ...
     *
     * @param sc the Scanner to use
     * @param graph the Graph to build
     * @return the number of bins
     */
    private static int readGraph(Scanner sc, Graph<Integer> graph) {
        int n_edges = sc.nextInt();
        int n_nodes = sc.nextInt();
        int n_bins  = sc.nextInt();

        for (int i = 1; i <= n_nodes; i++) {
            graph.addNode(new Node<>(i));
        }

        for (int i = 0; i < n_edges; i++) {
            int fst = sc.nextInt();
            int snd = sc.nextInt();
            graph.getNode(fst - 1).getNeighbourhood().add(graph.getNode(snd - 1));
            graph.getNode(snd - 1).getNeighbourhood().add(graph.getNode(fst - 1));
        }

        return n_bins;
    }

}