fork(1) download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4.  
  5. #define MAXN 1024
  6.  
  7. using namespace std;
  8.  
  9. int N, M, start;
  10. vector<int> G[MAXN];
  11.  
  12. void read()
  13. {
  14. cin >> N >> M;
  15.  
  16. for(int i = 0; i < M; i++)
  17. {
  18. int u, v;
  19. cin >> u >> v;
  20.  
  21. G[u].push_back(v);
  22. G[v].push_back(u);
  23. }
  24.  
  25. cin >> start;
  26. }
  27.  
  28. int used[MAXN];
  29. bool visited[MAXN];
  30.  
  31. void DFS(int u, int level, int parent)
  32. {
  33. used[u] = level;
  34. visited[u] = true;
  35.  
  36. int sz = G[u].size();
  37. for(int i = 0; i < sz; i++)
  38. {
  39. int v = G[u][i];
  40.  
  41. if(!visited[v])
  42. DFS(v, level + 1, u);
  43.  
  44. if(v != parent && used[v] != 0)
  45. cout << "Length of Cycle from " << v << " to " << u << " is " << level - used[v] + 1 << endl;
  46. }
  47.  
  48. used[u] = 0;
  49. }
  50.  
  51. void solve()
  52. {
  53. DFS(start, 1, -1);
  54. }
  55.  
  56. int main()
  57. {
  58. ios_base::sync_with_stdio(false);
  59. cin.tie(NULL);
  60.  
  61. read();
  62. solve();
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Standard output is empty