my @collect;
while ()
{
@m = split;
my @f;
for $i(0..$#collect)
{
$e = $collect[$i];
if (exists $e->{$m[0]} ||
exists $e->{$m[1]})
{
$e->{$m[1]} = 1;
$e->{$m[0]} = 1;
push @f, $i;
}
}
if ($#f < 0)
{
push @collect, {$m[0] => 1, $m[1] => 1};
}
elsif ($#f > 0)
{
$s = $collect[$f[0]];
for $i(1..$#f)
{
$e = $collect[$f[$i]];
for $me(keys %$e)
{
$s->{$me} = 1 if (!exists $s->{$me});
}
}
for $i(1..$#f)
{
splice @collect, $f[$i] - $i - 1, 1;
}
}
}
for $e(@collect)
{
print "[", join(",", sort keys %$e), "]\n";
}
__DATA__
A B
A C
B D
C F
G H
G I
I J