fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<sstream>
  4. #include<fstream>
  5. #include <unordered_map>
  6. #include <utility>
  7. #include<queue>
  8. using namespace std;
  9.  
  10. struct Match {
  11. string Hometeam;
  12. vector<string> Homesquad;
  13. string Homecountry;
  14. string Vistorteams;
  15. vector<string> Vistorsquad;
  16. string vistorcountry;
  17. string date;
  18. string score;
  19. int spectators;
  20. string stadium, stadiumcountry, leauge;
  21.  
  22. };
  23. vector<string> get_player(string line) {
  24. stringstream ss(line);
  25. string s;
  26. vector<string> v;
  27. while (getline(ss, s, ';')) {
  28. v.push_back(s);
  29. }
  30. return v;
  31. }
  32. Match readline(string line) {
  33. stringstream ss(line);
  34. string s;
  35. Match m;
  36. getline(ss, s, ',');
  37. m.Hometeam = s;
  38. getline(ss, s, ',');
  39. m.Homesquad = get_player(s);
  40. getline(ss, s, ',');
  41. m.Homecountry = s;
  42. getline(ss, s, ',');
  43. m.Vistorteams = s;
  44. getline(ss, s, ',');
  45. m.Vistorsquad = get_player(s);
  46. getline(ss, s, ',');
  47. m.vistorcountry = s;
  48. getline(ss, s, ',');
  49. m.date = s;
  50. getline(ss, s, ',');
  51. m.score = s;
  52. getline(ss, s, ',');
  53. m.spectators = stoi(s);
  54. getline(ss, s, ',');
  55. m.stadium = s;
  56. getline(ss, s, ',');
  57. m.stadiumcountry = s;
  58. getline(ss, s, ',');
  59. m.leauge = s;
  60. return m;
  61. }
  62. vector<Match> readfile(string filename) {
  63. ifstream fi(filename);
  64. if (!fi) {
  65. cout << "cannot read file!!";
  66. return{};
  67. }
  68. string line;
  69. getline(fi, line);
  70. vector<Match> v;
  71. while (getline(fi, line)) {
  72. Match m = readline(line);
  73. v.push_back(m);
  74. }
  75. fi.close();
  76. return v;
  77.  
  78. }
  79. struct team{
  80. string name;
  81. vector<string> squad;
  82. };
  83. struct graph {
  84. vector<team> T;
  85. vector<vector<int>> adjlist;
  86. };
  87. unordered_map<string, int>mp;
  88. int numteam = 0;
  89. vector<team> matches_to_team(vector<Match> m) {
  90. vector<team> v;
  91. for (Match i : m) {
  92. if (mp.find(i.Hometeam) == mp.end()) {
  93. mp[i.Hometeam] = numteam++;
  94. v.push_back({ i.Hometeam, i.Homesquad });
  95. }
  96. if (mp.find(i.Vistorteams) == mp.end()) {
  97. mp[i.Vistorteams] = numteam++;
  98. v.push_back({ i.Vistorteams ,i.Vistorsquad });
  99. }
  100. }
  101. return v;
  102. }
  103. team idx_to_team(int i, vector<team>& T) {
  104. return T[i];
  105. }
  106. graph buildgraph(vector<Match> M) {
  107. graph G;
  108. G.T = matches_to_team(M);
  109. G.adjlist.assign(G.T.size(), vector<int>());
  110. for (Match m : M) {
  111. G.adjlist[mp[m.Hometeam]].push_back(mp[m.Vistorteams]);
  112. }
  113. return G;
  114. }
  115. void bfs(graph& G, int sta , vector<int>& v, vector<bool>& vis) {
  116. queue<int> q;
  117. q.push(sta);
  118. vis[sta] = true;
  119. while (!q.empty()) {
  120. int u = q.front();
  121. q.pop();
  122. v.push_back(u);
  123. for (int v : G.adjlist[u]) {
  124. if (!vis[v]) {
  125. vis[v] = true;
  126. q.push(v);
  127. }
  128. }
  129. }
  130. }
  131. int main() {
  132. string filename = "inp.txt";
  133. vector<Match> m = readfile(filename);
  134. graph G = buildgraph(m);
  135.  
  136. vector<int> v;
  137. vector<bool> vis(G.T.size(), false);
  138.  
  139. for (int i = 0; i < G.T.size(); i++) {
  140. if (!vis[i]) {
  141. bfs(G, i, v, vis);
  142. }
  143. }
  144.  
  145. for (int i : v) {
  146. cout << idx_to_team(i, G.T).name << endl;
  147. }
  148. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
cannot read file!!