fork(1) download
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. class B
  8. {
  9. public:
  10. vector<int> b;
  11. B()
  12. {
  13. // b.reserve(301); not really needed
  14. }
  15. };
  16. int count(vector<int>u, vector<int>v) //To count the number of integers one vector has with
  17. //the other
  18. {
  19. sort(u.begin(),u.end());
  20. sort(v.begin(),v.end());
  21. int i=0,j=0,ctr=0;
  22. while(i<u.size()&&j<v.size())
  23. if(u[i]==v[j])
  24. ctr++,i++,j++;
  25. else if(u[i]>v[j])
  26. j++;
  27. else
  28. i++;
  29. return ctr;
  30. }
  31.  
  32. int main() {
  33.  
  34. int n,k,p,temp,ctr=0; //n is number of people and k is the threshold value
  35. cin>>n>>k;
  36. vector<B> id;
  37. id.resize(n);
  38. for(int i=0;i<n;i++)
  39. {
  40. cin>>p; //p is the number of integers for the person i
  41. for(int j=0;j<p;j++)
  42. {
  43. cin>>temp;
  44. id[i].b.push_back(temp); //If this line is removed, there is no segmentation fault
  45. }
  46. }
  47. vector<bool> isfriend(n,false);
  48. isfriend[0]=true;
  49. queue<int> q;
  50. q.push(0);
  51. while(q.empty()==false)
  52. {
  53. p=q.front();
  54. q.pop();
  55. for(int i=0;i<n;i++)
  56. if(isfriend[i]==false)
  57. {
  58. temp=count(id[p].b,id[i].b);
  59. if(temp>=k)
  60. {
  61. ctr++;
  62. q.push(i);
  63. isfriend[i]=true;
  64. }
  65. }
  66. }
  67. cout<<ctr; //No. of friends of person 0
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0s 4536KB
stdin
10 2 
3 1 2 3
4 1 2 3 4 
5 2 3 9 10 1000
3 8 10 11
5 5 6 7 8 9
0
0
0
0
0

stdout
2