1121 Damn Single (25 分)
"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤50,000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (≤ 10,000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.
Output Specification:
First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.
Sample Input:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
Sample Output:
5
10000 23333 44444 55555 88888
//初始化用用-1 //因为有人的编号可能是00000 //所以第二个点不过 #include<cstdio> const int maxn = 100010; int spouse[maxn] = {0}; int main(){ int n,id1,id2; for(int i = 0; i < maxn; i++) spouse[i] = -1; scanf("%d",&n); while(n--){ scanf("%d%d",&id1,&id2); spouse[id1] = id2; spouse[id2] = id1; } scanf("%d",&n); while(n--){ scanf("%d",&id1); if(spouse[id1] >= 0 && spouse[spouse[id1]] >= 0){ spouse[id1] = -2; }else if(spouse[id1] >= 0 &&spouse[spouse[id1]] == -2){ spouse[spouse[id1]] = 2; }else{ spouse[id1] = -3; } } int cnt = 0; for(int i = 0; i < maxn; i++){ if(spouse[i] < -1) cnt++; } printf("%d\n",cnt); for(int i = 0; i < maxn; i++){ if(spouse[i] < -1){ printf("%05d",i); cnt--; if(cnt > 0) printf(" "); } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)