题目描述 分析:利用map存储配偶,利用set存储单身人数,假设此人没有配偶则直接插入set,假设此人有配偶,判断set中是否已有该人的配偶,没有的话插入此人,有的话删除此人的配偶。 #include<unordered_map> #include<set> #include<algorithm> #include<cstdio> using namespace std; typedef long long ll; unordered_map<ll,ll> um; set<ll> s; int main(){ ll x,y; int n; scanf("%d",&n); while(n--){ scanf("%lld %lld",&x,&y); um[x]=y; um[y]=x; } scanf("%d",&n); while (n--) { scanf("%lld",&x); if(!um.count(x)){ s.insert(x); } else if(!s.count(um[x])){ s.insert(x); } else{ s.erase(um[x]); } } printf("%d\n",s.size()); for(auto i=s.begin();i!=s.end();i++){ if(i==s.begin()){ printf("%05lld",*i); } else{ printf(" %05lld",*i); } } }