Finding Handsome Loners Without Friends
When everyone is busy posting photos on social media, there are always some people who are too handsome to have friends. This problem asks you to identify those who are too handsome to have friends.
Input Format:
The first line contains a positive integer ( N ) (≤100), the number of known friend circles. Then ( N ) lines follow, each starting with a positive integer ( K ) (≤1000), the number of people in the circle, followed by ( K ) IDs (5-digit numbers from 00000 to 99999) separated by spaces. After that, a positive integer ( M ) (≤10000) is given, the number of queries. The next line contains ( M ) IDs to be queried, separated by spaces.
Note: A person without friends can be someone who never installed the social app, or someone who is the only person in their own circle. Although some narcissists may repeatedly add themselves to circles, it is guaranteed that any circle with ( K > 1 ) contains at least two distinct people.
Output Format:
Output the IDs of those who are too handsome to have friends, in the order they appear in the query list. IDs must be separated by exactly one space, with no leading or trailing spaces. If no one is handsome, output No one is handsome.
Note: A person can be queried multiple times, but should be output only once.
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
Approach:
First, we read the input and store all people from friend circles in a set. Since duplicates may appear, a set is appropriate. Note: if a circle has only one person (K==1), that person is friendless and should not be added to the set.
set<int> friends;
int N; // number of friend circles
cin >> N;
for (int i = 0; i < N; i++) {
int k; // number of people in the circle
cin >> k;
for (int j = 0; j < k; j++) {
int id;
cin >> id;
if (k == 1) continue; // skip circles with only one person
friends.insert(id);
}
}
Then, for each query ID, we check if it is in the friends set. If not, it is a "handsome loner" and should be output. To avoid duplicate outputs for repeated queries, we also maintain a set of already output IDs.
set<int> outputSet; // IDs already output
int flag = 0; // indicates if it's the first output
int M; // number of queries
cin >> M;
for (int i = 0; i < M; i++) {
int id;
cin >> id;
if (outputSet.count(id)) continue; // already output
if (friends.count(id) == 0) {
if (flag == 0) {
printf("%05d", id);
flag = 1;
} else {
printf(" %05d", id);
}
outputSet.insert(id);
}
}
if (outputSet.empty()) {
cout << "No one is handsome";
}
Complete Code:
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
int main() {
set<int> friends;
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int id;
cin >> id;
if (k == 1) continue;
friends.insert(id);
}
}
int M;
cin >> M;
set<int> outputSet;
int flag = 0;
for (int i = 0; i < M; i++) {
int id;
cin >> id;
if (outputSet.count(id)) continue;
if (friends.count(id) == 0) {
if (flag == 0) {
printf("%05d", id);
flag = 1;
} else {
printf(" %05d", id);
}
outputSet.insert(id);
}
}
if (outputSet.empty()) {
cout << "No one is handsome";
}
return 0;
}