fork download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 1234567890
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-6
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. using namespace std;
  28. // mylittledoge
  29.  
  30. int main() {
  31. cin.sync_with_stdio(0);
  32. cin.tie(0);
  33. int N,M;
  34. cin >> N >> M;
  35. map<string,int> L,R;
  36. vector<string> S(N);
  37. vector< pair<string,string> > V(N);
  38. for(int i =0; i < N; i++) {
  39. string s;
  40. cin >> s;
  41. S[i] =s;
  42. for(uint j =0; j <= s.length(); j++) if(j == s.length() || s[j] == '.') {
  43. V[i].ff =s.substr(0,j);
  44. if(j != s.length()) V[i].ss =s.substr(j+1,s.length()-j);
  45. break;}
  46. L[V[i].ff] =0;
  47. R[V[i].ss] =0;}
  48. int l =0, r =0;
  49. ALL_THE(L,it) it->ss =l++;
  50. ALL_THE(R,it) it->ss =r++;
  51.  
  52. vector< set< pair<int,int> > > Gl(L.size());
  53. vector< set< pair<int,int> > > Gr(R.size());
  54. for(int i =0; i < N; i++) {
  55. Gl[L[V[i].ff]].insert(make_pair(R[V[i].ss],i));
  56. Gr[R[V[i].ss]].insert(make_pair(L[V[i].ff],i));}
  57.  
  58. set<int> ans;
  59. for(int i =0; i < N; i++) ans.insert(i);
  60. for(int k =0; k < M; k++) {
  61. vector< set< pair<int,int> > > Gl0 =Gl, Gr0 =Gr;
  62. if(k%2 != 0) {
  63. for(int i =0; i < l; i++) if(Gl0[i].size() == 1) {
  64. int x =Gl[i].begin()->ss;
  65. int a =L[V[x].ff], b =R[V[x].ss];
  66. Gl[a].erase(make_pair(b,x));
  67. Gr[b].erase(make_pair(a,x));
  68. ans.erase(x);}
  69. }
  70. else {
  71. for(int i =0; i < r; i++) if(Gr0[i].size() == 1) {
  72. int x =Gr[i].begin()->ss;
  73. int a =L[V[x].ff], b =R[V[x].ss];
  74. Gl[a].erase(make_pair(b,x));
  75. Gr[b].erase(make_pair(a,x));
  76. ans.erase(x);}
  77. }
  78. }
  79.  
  80. cout << ans.size() << "\n";
  81. ALL_THE(ans,it) cout << S[*it] << "\n";
  82. return 0;}
  83.  
  84. // look at my code
  85. // my code is amazing
Runtime error #stdin #stdout #stderr 0s 3492KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc