Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Identifying Individuals with No Social Connections

Tech 2

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:

  1. Social circles with 0 or 1 members are excluded from the friend network
  2. Only circles with 2+ distinct members contribute to the social connections
  3. Using a set for storing social IDs provides efficient lookup operations
  4. Tracking already found individuals prevents duplicate outputs
  5. Proper formatting ensures no extra spaces in the output
Tags: Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.