Identifying Individuals with No Social Connections
Problem Description
While most people actively share photos in social networks, some individuals are so attractive that they have no friends. This problem requires identifying those who are "handsome" due to having no social connections.
Input Format
The first line contains a positive integer N (≤100), representing the number of known social circles. This is followed by N lines, each starting with a positive integer K (≤1000) indicating the number of people in that circle, followed by K 5-digit ID numbers (ranging frrom 00000 to 99999) separated by spaces. After processing all circles, a positive integer M (≤10000) is given, representing the number of queries, followed by M ID numbers to check, separated by spaces.
Note: Individuals with no friends may either not have enstalled the social app or only appear in circles with themselves. While some narcissistic users might add themselves multiple times, all circles with more than one member are guaranteed to contain at least two distinct individuals.
Output Format
Output the IDs of individuals who are handsome (have no social connections) in the order they appear in the query list. Separate IDs with single spaces, ensuring no leading or trailing spaces. If no one qualifies, output No one is handsome.
Note: Duplicate queries for the same person should result in only one output.
Sample Input 1
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
Sample Output 1
10000 88888 23333
Sample Input 2
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
Sample Output 2
No one is handsome
Solution
circle_count = int(input())
social_ids = set()
for _ in range(circle_count):
data = input().split()
members = int(data[0])
# Only consider circles with 2 or more distinct members
if members > 1:
social_ids.update(data[1:members+1])
query_count = int(input())
query_list = input().split()
# Find individuals not in any social circle
unique_loners = []
found_ids = set()
for person_id in query_list:
if person_id not in social_ids and person_id not in found_ids:
unique_loners.append(person_id)
found_ids.add(person_id)
if unique_loners:
print(' '.join(unique_loners))
else:
print("No one is handsome")
Implemantation Notes
Key considerations:
- Social circles with 0 or 1 members are excluded from the friend network
- Only circles with 2+ distinct members contribute to the social connections
- Using a set for storing social IDs provides efficient lookup operations
- Tracking already found individuals prevents duplicate outputs
- Proper formatting ensures no extra spaces in the output