Consider a simplified social-network model with these properties:
Your task is to support the following two query forms.
For user, output at most k of their strongest friends.
Only direct friends of user may be selected. Order those friends according to these rules:
When user has fewer than k direct friends, return every direct friend.
Find up to k potential new friends for user.
Apply the following traversal procedure:
user's best friends in the order produced by BEST user.bf, visit the users returned by BEST bf, preserving that order.cand as a recommendation only when all of the following hold:
cand is different from user;cand is not a direct friend of user;cand has not already been recommended.cand is already directly connected to user, ignore it and keep looking at later candidates.k recommendations have been collected or there are no candidates left to inspect.F L Q
Next F lines: userA userB, meaning userA and userB are friends
Next L lines: user item, meaning user liked item
Next Q lines: either BEST user k or RECOMMEND user k
Additional input details:
Write one line for every query:
, between adjacent ids.EMPTY.0 <= F <= 2 * 10^50 <= L <= 2 * 10^51 <= Q <= 2 * 10^5Input:
4 9 2
alice bob
alice carol
bob dave
bob erin
alice x
alice y
bob x
bob z
carol y
carol z
dave x
dave z
erin z
BEST alice 2
RECOMMEND alice 2
Output:
bob,carol
dave,erin