#!/usr/bin/python
import sys

if (len(sys.argv) != 4):
    print "usage: convert <input graph> <edges file> <ids file>"
    sys.exit(0)

fin = open(sys.argv[1],'r')
ALL = {}
CLU = {}
index = 0
edges = 0
# Create the dictionary
line = fin.readline()
line = fin.readline()
while line != '-2\n':
    node = line.split()
    edges += int(node[1])
    # place in dictionary the main node
    if node[0] not in ALL:
        ALL[node[0]] = index
        CLU[node[0]] = index
        index += 1
    for x in range(2,len(node)):
        if node[x] not in ALL:
            ALL[node[x]] = index
            index += 1
    line = fin.readline()
fin.close()

# write the two description files

fout = open(sys.argv[2],'w')
fin = open(sys.argv[1],'r')

fout.write(str( len(ALL) ) + ' ' + str( edges ) + '\n')

line = fin.readline()
line = fin.readline()
while line != '-2\n':
    node = line.split()
    for x in range(2,len(node)):
        if (ALL[node[x]] == 9614):
            print "shit", index, node[x], str(len(ALL) + len(CLU))
        fout.write(str(ALL[node[0]]) + ' ' + str(ALL[node[x]]) + '\n')
    line = fin.readline()

fin.close()
fout.close()

fout = open(sys.argv[3],'w')
fout.write(str(len(CLU)) + '\n')
for x in CLU.iterkeys():
    fout.write(str(CLU[x]) + ' ' + x + '\n')
fout.close()
