title: How to find of what vertices is made cycle in undirected graph if there is only one cycle in graph?
body: <p>how to find of what vertices is made cycle in undirected graph if there is only one cycle in graph?</p>
  
  <p>I have code for finding cycle in graph, but right now I need code that will find of what vertices cycle is made.</p>
  
  <p>Here is code(in C++) for finding cycle:</p>
  
  <pre><code>bool dfs(int x)
  {
      state[x] = 1;
      for(int j = 0; j &lt; ls[x].size(); j++)
      {
          if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
          {
              t = 0; // Graph contains cycle.
              return t;
          }
          if(state[ls[x][j]] == 0)
          {
              parent[ls[x][j]] = x;
              dfs(ls[x][j]);
          }
      }
  }
  
  void detect_cycle()
  {
      memset(state, 0, sizeof state);
      memset(parent, 0, sizeof parent);
      for(int i = 1; i &lt;= n; i++)
          if(state[i] == false)
              dfs(i);
  }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>Here is the final code. Thanks guys.</p>
  
  <pre><code>bool dfs(int x)
  {
      state[x] = 1;
      for(int j = 0; j &lt; ls[x].size(); j++)
      {
          if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
          {
              if(t)
              {
                  printf("Cycle entry: %d\n", ls[x][j]);
                  printf("Cycle contains: %d, %d ", ls[x][j], x);
                  int cycleNode = parent[x];
                  while(cycleNode != ls[x][j])
                  {
                      printf("%d ", cycleNode);
                      cycleNode = parent[cycleNode];
                  }
              }
              t = 0;
              return t;
          }
          if(state[ls[x][j]] == 0)
          {
              parent[ls[x][j]] = x;
              dfs(ls[x][j]);
          }
      }
  }
  </code></pre>