搜狗2016研发工程师编程题

来源:互联网 发布:金自天正押注人工智能 编辑:程序博客网 时间:2024/06/10 19:39

#include<iostream>#include<vector>using namespace std;int main(){int n;while (cin >> n){vector<int>p(n, 0);vector<vector<int>>vec;for (int i = 0; i < n; i++){vector<int>temp;int id, num;cin >> id >> num;temp.push_back(id);temp.push_back(num);while (num--){int st;cin >> st;temp.push_back(st);}vec.push_back(temp);}vector<vector<int>>vec1(n, vector<int>(n, 0));for (int i = 0; i < vec.size(); i++){for (int j = 2; j < vec[i].size(); j++){vec1[i][vec[i][j] - 1] = 1;}}for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (vec1[i][j] == 1 && vec1[j][i] == 1){p[i] = 1;p[j] = 1;}}}vector<int>idcheat;vector<int>idcheck;int count = 0;for (int i = 0; i < n; i++){if (p[i] == 1){count++;idcheat.push_back(i);}else{idcheck.push_back(i);}}for (int i = 0; i < idcheck.size(); i++){            int temp = 0;for (int j = 0; j < idcheat.size(); j++){if (vec1[idcheck[i]][idcheat[j]] == 1){++temp;//cout << temp << endl;}if (temp >= 2){p[idcheck[i]] = 1;//cout << idcheck[i] << endl;count++;break;}}}cout << count << endl;for (int i = 0; i < n; i++){if (p[i] == 1){cout << i + 1<<" ";}}cout << endl;}system("pause");return 0;}

现在我们需要查出一些作弊的问答社区中的ID,作弊有两种:1.A回答了B的问题,同时B回答了A的问题。那么A和B都是作弊。2.作弊ID用户A和作弊ID用户B同时回答了C的问题,那么C也是作弊。已知每个用户的ID是一串数字,一个问题可能有多个人回答。

输入描述:
每组数据第一行为总问题数N(N小于等于200000),第二行开始每行一个问题,第一个数字为提问人ID,第二个数字为回答人数,后面则为所有回答人的ID。(ID均为0-1000000的整数)
输出描述:
第一行为作弊ID数量,第二行开始为从小到大的每行一个作弊ID。

输入例子:
31 1 22 1 13 2 1 2

输出例子:
31 2 3



ps:上述自己编写的程序,验证没有问题,但没有AC,可能使用的空间太多了


2016/9/12  am 1:17

又弄了一个多小时,终于ac了,累死了;下面是修改的完整代码:

#include<iostream>#include<vector>using namespace std;int main(){int n;while (cin >> n){vector<int>p(n, 0);vector<vector<int>>vec;for (int i = 0; i < n; i++){vector<int>temp;int id, num;cin >> id >> num;temp.push_back(id);temp.push_back(num);while (num--){int st;cin >> st;temp.push_back(st);}vec.push_back(temp);}vector<vector<int>>vec1(n, vector<int>(n, 0));for (int i = 0; i < vec.size(); i++){for (int j = 2; j < vec[i].size(); j++){if ((vec[i][0]-1) != (vec[i][j] - 1)){vec1[vec[i][0]-1][vec[i][j] - 1] = 1;}}}for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (vec1[i][j] == 1 && vec1[j][i] == 1){p[i] = 1;p[j] = 1;}}}vector<int>idcheat;vector<int>idcheck;int count = 0;for (int i = 0; i < n; i++){if (p[i] == 1){count++;idcheat.push_back(i);}else{idcheck.push_back(i);}}for (int i = 0; i < idcheck.size(); i++){            int temp = 0;for (int j = 0; j < idcheat.size(); j++){if (vec1[idcheck[i]][idcheat[j]] == 1){++temp;//cout << temp << endl;}if (temp >= 2){p[idcheck[i]] = 1;//cout << idcheck[i] << endl;count++;break;}}}cout << count << endl;if (count == 0) continue;else{//cout << count << endl;for (int i = 0; i < n; i++){if (p[i] == 1){if (--count){cout << i + 1 << " ";}elsecout << i + 1;//cout << i + 1<<" ";}}cout << endl;}}//system("pause");return 0;} 



0 0