fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //✅ Accepted
  5.  
  6. class Edge{
  7. public:
  8. int to ,cost;
  9. };
  10. using GRAPH2 = vector<unordered_set<int>>;
  11. using GRAPH = vector<vector<Edge>>;
  12.  
  13. void add_edge(GRAPH &graph , int from , int to ,int cost){
  14. graph[from].push_back({to,cost});
  15. //graph[to].push_back({from,cost});
  16. }
  17.  
  18. void print_graph(GRAPH &graph){
  19. for(int i = 1 ; i < graph.size() ; i++){
  20. cout << i << " : ";
  21. for(auto j : graph[i]){
  22. cout << j.to << " " << j.cost << " ";
  23. }
  24. cout << endl;
  25. }
  26. }
  27.  
  28. //Hw 2 : Prolem 1
  29. void solve() {
  30. int n,e;
  31. cin >> n >> e;
  32.  
  33. unordered_map<string, vector<pair<string, int>>> g;
  34. set<string> nodes;
  35.  
  36. while (e--) {
  37. string from, to;
  38. int cost;
  39. cin >> from >> to >> cost;
  40. g[from].push_back({to, cost});
  41. nodes.insert(from);
  42. nodes.insert(to);
  43. }
  44.  
  45. for (const auto &node : nodes) {
  46. auto &edges = g[node];
  47. sort(edges.begin(), edges.end(), [](const auto &a, const auto &b) {
  48. if (a.first == b.first)
  49. return a.second < b.second;
  50. return a.first < b.first;
  51. });
  52. cout <<"\n";
  53. if(edges.size() == 0) continue;
  54. cout <<"Flights From "<< node << " : " << '\n';
  55. for (const auto &edge : edges) {
  56. cout <<setw(10)<<"To "<< edge.first << " with cost : " << edge.second << endl;
  57. }
  58. cout << endl;
  59. }
  60. }
  61. void add_edge2(GRAPH &graph, int from, int to, int cost) {
  62. graph[from].push_back({to, cost});
  63.  
  64. }
  65.  
  66. void print_graph2(const GRAPH &graph, const vector<string> &index_to_name) {
  67. for (int i = 0; i < graph.size(); i++) {
  68. if (graph[i].empty()) continue;
  69.  
  70. cout << "Flights from " << index_to_name[i] << ":\n";
  71. for (const auto &edge : graph[i]) {
  72. cout << setw(10) << "To " << index_to_name[edge.to] << " with cost " << edge.cost << '\n';
  73. }
  74. cout << endl;
  75. }
  76. }
  77.  
  78. //Hw 2 : Prolem 2
  79. void solve2() {
  80. int n, e;
  81. cin >> n >> e;
  82.  
  83. map<string, int> name_to_index;
  84. vector<string> index_to_name;
  85. int idx = 0;
  86.  
  87. GRAPH g(n);
  88.  
  89. while (e--) {
  90. string from, to;
  91. int cost;
  92. cin >> from >> to >> cost;
  93.  
  94. if (name_to_index.find(from) == name_to_index.end()) {
  95. name_to_index[from] = idx++;
  96. index_to_name.push_back(from);
  97. }
  98.  
  99. if (name_to_index.find(to) == name_to_index.end()) {
  100. name_to_index[to] = idx++;
  101. index_to_name.push_back(to);
  102. }
  103.  
  104. int u = name_to_index[from];
  105. int v = name_to_index[to];
  106.  
  107. add_edge2(g, u, v, cost);
  108. }
  109.  
  110. print_graph2(g, index_to_name);
  111. }
  112. //Hw 2 : Prolem 3
  113. void solve3() {
  114. int r,c;
  115. cin >> r >> c;
  116. int img[r][c] = {0};
  117.  
  118. int val =0;
  119. for(int i = 0 ; i < r ; i++){
  120. for(int j = 0 ; j < c ; j++){
  121. img[i][j] = val++;
  122. }
  123. }
  124.  
  125. vector<vector<vector<int>>> neighbors(r, vector<vector<int>>(c));
  126.  
  127. for(int i = 0 ; i < r ; i++){
  128. for(int j = 0 ; j < c ; j++){
  129. if(i > 0) neighbors[i][j].push_back(img[i-1][j]);
  130. if(i < r-1) neighbors[i][j].push_back(img[i+1][j]);
  131. if(j > 0) neighbors[i][j].push_back(img[i][j-1]);
  132. if(j < c-1) neighbors[i][j].push_back(img[i][j+1]);
  133. }
  134. }
  135.  
  136. for(int i = 0 ; i < r ; i++){
  137. for(int j = 0 ; j < c ; j++){
  138. cout <<"Node : "<< img[i][j] << " has neighbors : ";
  139. for(auto x : neighbors[i][j]){
  140. cout << x << " ";
  141. }
  142. cout << endl;
  143. }
  144. }
  145. }
  146. //Hw 2 :Prolem 4
  147. void solve4() {
  148. int n, e;
  149. cin >> n >> e;
  150. list<int> g[n];
  151.  
  152. while (e--) {
  153. int from, to;
  154. cin >> from >> to;
  155. g[from].push_back(to);
  156. }
  157.  
  158. int q;
  159. cin >> q;
  160. while (q--) {
  161. int node;
  162. cin >> node;
  163.  
  164. vector<bool> visited(n, false);
  165. list<int> queue;
  166. queue.push_back(node);
  167. visited[node] = true;
  168.  
  169. while (!queue.empty()) {
  170. int current = queue.front();
  171. queue.pop_front();
  172.  
  173. cout << current << " ";
  174.  
  175. for (int neighbor : g[current]) {
  176. if (!visited[neighbor]) {
  177. visited[neighbor] = true;
  178. queue.push_back(neighbor);
  179. }
  180. }
  181. }
  182. cout << "\n";
  183. }
  184. /*
  185. 6 5
  186. 4 1
  187. 1 2
  188. 5 3
  189. 0 5
  190. 3 4
  191. 4
  192. 0
  193. 3
  194. 1
  195. 2
  196. */
  197. }
  198. //Hw 2 :Problem 5
  199. void solve5() {
  200. //print_paths_len_2
  201. int n,e;
  202. cin >> n >> e;
  203. vector<vector<int>> g(n+1);
  204. while(e--){
  205. int from ,to ;
  206. cin >> from >> to ;
  207. g[from].push_back(to);
  208. }
  209.  
  210. for(int i = 0 ; i <(int) g.size() ; i++){
  211. for(int j = 0 ; j <(int) g[i].size() ; j++){
  212. for(int k = 0 ; k <(int) g[g[i][j]].size() ; k++){
  213. cout << i << " " << g[i][j] << " " << g[g[i][j]][k] << endl;
  214. }
  215. }
  216. }
  217.  
  218. }
  219. int main(){
  220. //solve();
  221. //solve2();
  222. //solve3();
  223. //solve4();
  224. //solve5();
  225. return 0;
  226. }
  227.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty