add graph

mail@pastecode.io avatar
unknown
python
a year ago
1.5 kB
2
Indexable
Never
import sys

def add_n(mt, dic, x, output=True):
	if x in dic:
		sys.stdout.write("DUP\n")
	else:
		dic[x]=len(dic)
		mt.append([])
		if output==True:
			sys.stdout.write("ADD\n")

def add_e(mt, dic, a, b):
	if a not in dic or b not in dic:
		if a not in dic:
			add_n(mt, dic, a, False)
		if b not in dic:
			add_n(mt,dic,b,False)
		mt[dic[a]].append(b)
		sys.stdout.write("ADD2\n")
	else:
		if b in mt[dic[a]]:
			sys.stdout.write("DUP2\n")
			return
		else:
			mt[dic[a]].append(b)
			sys.stdout.write("ADD3\n")
			return

def check_connect(mt, dic, a, b):
	try:
		if b in mt[dic[a]]:
			sys.stdout.write("TRUE\n")
			return
		else:
			sys.stdout.write("FALSE\n")
	except:
		sys.stdout.write("FALSE\n")

def check_num_connect(mt,dic,a):
	try:
		count=len(mt[dic[a]])
		sys.stdout.write(str(count)+"\n")
	except:
		sys.stdout.write('0\n')

def operation(mt):
	inp=[0,'A']
	dic={}
	while len(inp) != 1:
	#while True:
		inp = list(map(str, sys.stdin.readline().strip().split()))
		if inp[0] == "1":
			add_n(mt, dic, inp[1])
		elif inp[0] == "2":
			add_e(mt, dic,inp[1], inp[2])
		elif inp[0] == "3":
			check_connect(mt, dic, inp[1], inp[2])
		elif inp[0] == "4":
			check_num_connect(mt, dic, inp[1])
		elif inp[0] == "5":
			print(mt)

mt=[]
operation(mt)

"""
2 M I
3 W M
2 L P
4 L
2 B Q
2 N G
2 R G
4 B
1 T
2 Z H
4 Y
3 I T
2 Q E
4 C
2 Y T
4 K
1 F
4 P
4 J
2 O R
2 P V
0
"""