fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void DFS(int start, int count, vector<int>* vec, bool* visited, int* order) {
  8. visited[start] = true;
  9. order[start] = count;
  10.  
  11. for (int i = 0; i < vec[start].size(); i++) {
  12. int next = vec[start][i];
  13.  
  14. if (!visited[next]) {
  15. count++;
  16. DFS(next, count, vec, visited, order);
  17. }
  18.  
  19. }
  20. }
  21.  
  22. int main() {
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(NULL);
  25. cout.tie(NULL);
  26.  
  27. int N, M, R;
  28. cin >> N >> M >> R;
  29.  
  30. vector<int>* graph = new vector<int>[N];
  31. int* order = new int[N]();
  32. bool* visited = new bool[N]();
  33.  
  34. while (M--) {
  35. int u, v;
  36. cin >> u >> v;
  37. graph[u - 1].push_back(v - 1);
  38. graph[v - 1].push_back(u - 1);
  39. }
  40.  
  41. for (int i = 0; i < N; i++)
  42. sort(graph[i].begin(), graph[i].end());
  43.  
  44. DFS(R - 1, 1, graph, visited, order);
  45.  
  46. for (int i = 0; i < N; i++)
  47. cout << order[i] << "\n";
  48.  
  49. delete[] graph;
  50. delete[] order;
  51. delete[] visited;
  52.  
  53. return 0;
  54. }
Runtime error #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
Standard output is empty