public class KingdomClient {

    // takes the name of an input file as a command-line argument,
    // creates a Kingdom object,
    // sign the treaties listed in the file, and
    // prints the kingdom and its provinces
    public static void main(String[] args) {

        // read input file and sign the treaties
        In in = new In(args[0]);
        int n = in.readInt();
        Kingdom kingdom = new Kingdom(n);
        while (!in.isEmpty()) {
            int p = in.readInt();
            int q = in.readInt();
            kingdom.signTreaty(p, q);
        }

        // print kingdom and its provinces
        StdOut.println(kingdom);
        for (int p = 0; p < n; p++) {
            if (kingdom.bannerOf(p) == p) {
                StdOut.print(p + ": ");
                for (int q = 0; q < n; q++) {
                    if (kingdom.bannerOf(q) == p) {
                        StdOut.print(q + " ");
                    }
                }
                StdOut.println();
            }
        }

    }
}
