Compute the Join and Meet of Partitions Background

In this challenge, a partition of a set S means a collection of nonempty subsets of S, which are mutually disjoint and whose union is S. We say that a partition p is finer than another partition q (and q is coarser than p), if it can be obtained from q by splitting some of its sets into smaller pieces. The join (or least upper bound) of two partitions is the finest partition which is coarser than both of them, and their meet (or greatest lower bound) is the coarsest partition which is finer than both of them. See the Wikipedia pages on partitions and lattices for more information and nice pictures.

One way to compute the join and meet of two partitions p and q is the following. Form a graph whose vertex set is S, and add an edge a - b for each pair a, b that are in the same set in either p or q. Then the join of p and q contains the connected components of this graph. The meet of p and q consists of all nonempty intersections of a set in p with a set in q.

For example, consider the partitions [[0,1],[2,3]] and [[0,1,2],[3]] of S = [0,1,2,3]. The graph mentioned above is

0-1
|/
2-3


where the edge 2 - 3 comes from the first partition (2 and 3 are in the same set), and the triangle comes from the second one (0, 1 and 2 are in the same set). This means that the join is [[0,1,2,3]]. The intersections of all the sets in the partitions are

[0,1] ∩ [0,1,2] = [0,1],
[0,1] ∩ [3]     = [], 
[2,3] ∩ [0,1,2] = [2],
[2,3] ∩ [3]     = [3]


Removing the empty set, we have the meet [[0,1],[2],[3]].

Input

Two partitions of a finite set of integers, given as arrays of arrays.

Output

The join and meet of the input partitions, in this order.

Test Cases

[[1]] [[1]] -&gt; [[1]] [[1]]
[[0,1],[2,3]] [[0,1,2],[3]] -&gt; [[0,1,2,3]] [[0,1],[2],[3]]
[[0],[1],[2,3,4,7,6]] [[2,3],[4,7],[6],[1,0]] -&gt; [[0,1],[2,3,4,6,7]] [[0],[1],[2,3],[4,7],[6]]
[[0],[1,2],[5,4],[3,6]] [[0,3],[6,4],[5,2],[1]] -&gt; [[0,1,2,3,4,5,6]] [[0],[2],[1],[4],[5],[3],[6]]


Detailed Rules

The order of the sets in the output partitions, and of the numbers in those sets, does not matter. You can assume that the inputs are correct, that is, they contain the same numbers, each exactly once, and no empty arrays. Note that the numbers do not necessarily form an interval (the third test case contains no 5).

You can write either a full program or a function. You can also write two programs/functions in the same language, one of which computes the join and other the meet, but there's a bonus of -10 bytes if you don't choose this option. The shortest byte count wins, and standard loopholes are disallowed.
CJam, 32 - 10 = 22

q_~{_{T&amp;},:U-U:+a+}fTp~m*::&amp;La-p


::&amp; is newer than this question.

CJam, 36 - 10 = 26

q_~{:T;_{T&amp;},:U-U:+a+}/p~m*{~&amp;}%La-p


Input:

[[0] [1] [2 3 4 7 6]]
[[2 3] [4 7] [6] [1 0]]


Output:

[[2 3 4 7 6] [0 1]]
[[0] [1] [2 3] [4 7] [6]]


Explanation

q                 " Read the input. ";
_~                " Evaluate a copy. ";
{:T;              " For each list T in the second partition: ";
    _{T&amp;},        " Select all lists in the first partition which share items with T. ";
    :U-           " Save to U and remove them from the first partition. ";
    U:+a+         " Append the union of U to the first partition. ";
}/
p                 " Print the meet. ";
~                 " Evaluate another copy. ";
m*                " Get the Cartesian product. ";
{~&amp;}%             " Get the intersections of each pair of lists. ";
La-               " Remove empty lists. ";
p                 " Print. ";