Untitled

 avatar
unknown
python
2 years ago
2.5 kB
4
Indexable
#thêm đỉnh
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)

#Tô màu
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)