题解 | #火眼金睛#
火眼金睛
http://www.nowcoder.com/practice/d311403b15b8495b81b622edaefd5b5a
这道题更像是设计一个小系统,贴近实际,算法上不难,由于每个id的问题有多个人解答,考虑问题id作为key,解答的人id作为value,用set存储,如果有交集,则两个人都作弊,存到zuobi1集合,然后再查一遍,发现一个问题id有两个作弊的,存到zuobi2里面,合并两个zuobi集合就是最终集合;输出的时候注意格式就行
#include <vector>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main(){
int N;
while(cin>>N) {
map<int,set<int> > id_all;
while (N--) {
int ans_id;
cin>>ans_id;
int num;
cin>>num;
set<int> id;
while (num--) {
int tmp;cin>>tmp;
id.insert(tmp);
}
if (id_all.find(ans_id)!=id_all.end()) {
set<int> tmp=id_all[ans_id];
id.insert(tmp.begin(),tmp.end());
}
id_all[ans_id]=id;
}
set<int> zuobi;
for (auto iter=id_all.begin(); iter!=id_all.end(); iter++) {
int ans1=iter->first; set<int> tmp=iter->second;
for (auto iter_id=tmp.begin(); iter_id!=tmp.end(); iter_id++) {
int id=*iter_id;
if (id!=ans1) {
if (id_all.find(id)!=id_all.end()) {
set<int> tmp2=id_all[id];
if (tmp2.find(ans1)!=tmp2.end()) {
zuobi.insert(ans1);zuobi.insert(id);
}
}
}
}
}
set<int> zuobi2;
for (auto iter=id_all.begin(); iter!=id_all.end(); iter++) {
int check=0;
int ans=iter->first;
set<int> tmp=iter->second;
for (auto iter_id=tmp.begin(); iter_id!=tmp.end(); iter_id++) {
int id=*iter_id;
if (zuobi.find(id)!=zuobi.end()) {
check+=1;
}
}
if (check>=2) {
zuobi2.insert(ans);
}
}
int num=zuobi.size()+zuobi2.size();
set<int> final_set;
zuobi.insert(zuobi2.begin(), zuobi2.end());
cout<<zuobi.size()<<endl;
for (auto iter=zuobi.begin(); iter!=zuobi.end(); iter++) {
cout<<*iter<<" ";
}
if (zuobi.size()!=0) {
cout<<endl;
}
}
return 0;
}