user={}
follow={}
def AddUser(id1,name):
global user
k = list(user.keys())
if k.count(id1)==0:user[id1] = name
else: print('ID exists.')
def AddFollow(id1,id2):
global follow
k = list(user.keys())
y = list(follow.keys())
if k.count(id2)==0 or k.count(id1)==0:
print('No such user.')
if y.count(id1)==1:
if len(follow[id1])!=0:
if follow[id1].count(id2)!=0:print('Follow exists.')
else:follow[id1].append(id2)
if y.count(id1)==0:follow[id1] = [id2]
def ListFollower(id1):
k = list(user.keys())
k1 = list(follow.keys())
if k.count(id1)==0:print('No such user.')
else:
m = 0
p = []
for i in k1:
if follow[i].count(id1)==0:m +=0
if follow[i].count(id1)!=0:
p.append(i)
m+=1
if m ==0:print('No followers.')
else:
p.sort()
for i in p:print(i,user[i])
def CommonFollow(id1,id2):
k,j = list(user.keys()),list(follow.keys())
if k.count(id1)==0 or k.count(id2)==0:print('No such user.')
else:
if j.count(id1)==0 or j.count(id2)==0:print('No common follow.')
else:
p = []
m = 0
for i in follow[id1]:
for j in follow[id2]:
if i==j:
p.append(i)
m+=1
if m==0:print('No common follow.')
else:
p.sort()
for i in p:print(i,user[i])
def Recommend(id1):
k1 = list(user.keys())
k = list(follow.keys())
if k1.count(id1)==0:print('No such user.')
if k.count(id1)==0:print('No recommendation.')
else:
u = 0
p = []
for i in follow[id1]:
if k.count(i)!=0:
for j in follow[i]:
if j!=id1 and follow[id1].count(j)==0:p.append(j)
p1 = []
for i in p:
o = [i,p.count(i)]
p1.append(o)
for i in p1:
while p1.count(i)!=1:
p1.remove(i)
p1 = sorted(p1,key = lambda k : k[1])[::-1]
p11 = p1[:]
p2 = [i[1] for i in p1]
if len(p2)==1:
print(p1[0][0],user[p1[0][0]],'('+str(p1[0][1])+')')
u = 1
else:
p3 = []
p4 = []
for i in p1:
if p2.count(i[1])==1:
print(i[0],user[i[0]],'('+str(i[1])+')')
u = 1
else:
p3 = []
for j in p1:
if j[1]==i[1]:
p3.append(j)
p3 = sorted(p3,key = lambda k:k[0])
for e in p3:
print(e[0],user[e[0]],'('+str(e[1])+')')
p1.remove(e)
u = 1
if u==0: print('No recommendation.')
def ShortestPath(src,des):
k = list(user.keys())
k1 = list(follow.keys())
visited = {}
previous = {}
if k.count(src)==0 or k.count(des)==0:
print('No such user.')
else:
for i in k:
if i!=src:
visited[i]=False
else: visited[i]=True
q = [src]
y = 0
while len(q)!=0 and visited[des]==False:
current = q.pop(0)
if k1.count(current)==1:
for n in follow[current]:
if visited[n]==False:
q.append(n)
visited[n]=True
previous [n] = current
if visited[des]==True:
break
if len(q)>len(k)**2:break
if visited[des]==True:
road = [des]
u = des
while u!=src:
for i in previous:
if i == u:
road.append(previous[i])
u = previous[i]
if u==src:break
road = road[::-1]
for i in road:print(i,user[i])
else:print('No path found.')
def main():
command=input().split()
while(command[0]!="END"):
if (command[0]=="AddUser"):
AddUser(int(command[1]),command[2])
elif (command[0]=="AddFollow"):
AddFollow(int(command[1]),int(command[2]))
elif (command[0]=="CommonFollow"):
CommonFollow(int(command[1]),int(command[2]))
elif (command[0]=="ListFollower"):
ListFollower(int(command[1]))
elif (command[0]=="Recommend"):
Recommend(int(command[1]))
elif (command[0]=="ShortestPath"):
ShortestPath(int(command[1]),int(command[2]))
command=input().split()
return
main()