title: Leetcode210. Why my code is wrong in a particular case?
body: <p>Here is the Question(<a href="https://leetcode.com/problems/course-schedule-ii/description/" rel="nofollow noreferrer">https://leetcode.com/problems/course-schedule-ii/description/</a>). 
  And my solution is from the "Introduction to Algorithms".
  Use the DFS algorithm. And color the node with white, grey, and black.</p>
  
  <ol>
  <li>mark all the nodes white.</li>
  <li>when a node is discovered, mark grey.</li>
  <li>when a node is finished, mark black.</li>
  <li>when an edge is backward, which means has a grey adjacent node. It has a circle, it should null.</li>
  </ol>
  
  <p>But I pass most of the cases. But some of them are not.
  Could Anyone help me out? </p>
  
  <p>The Code:</p>
  
  <pre><code>class Solution {
  public:
      enum {WHITE, GRAY, BLACK};
  
      vector&lt;vector&lt;int&gt;&gt; graph;
      vector&lt;int&gt; colors;
      list&lt;int&gt; topo;
  
      void add_edge(vector&lt;vector&lt;int&gt;&gt;&amp; graph, pair&lt;int, int&gt;&amp; p) {
          graph[p.second].push_back(p.first);
      }
      bool visit(vector&lt;vector&lt;int&gt;&gt;&amp; graph, int i) {
          if (colors[i] == WHITE) {
              colors[i] = GRAY;
              bool ret = true;
  
              for (int j = 0; j &lt; graph[i].size(); j++) {
                  if (colors[graph[i][j]] == WHITE)
                      ret = ret &amp;&amp; visit(graph, graph[i][j]);
                  else if (colors[graph[i][j]] == GRAY)
                      ret =  false;
                  else
                      ret =  false;
  
              }
  
              colors[i] = BLACK;
              topo.push_front(i);
              return ret;
          }
  
          return true;
      }
  
      bool dfs(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {
          for (int i = 0; i &lt; graph.size(); i++)
              if (!visit(graph, i))
                  return false;
          return true;
      }
  
  
  
  public:
      vector&lt;int&gt; findOrder(int numCourses, vector&lt;pair&lt;int, int&gt;&gt;&amp; prerequisites) {
          graph.resize(numCourses, {});
          colors.resize(numCourses, WHITE);
  
          for (auto&amp; pre: prerequisites)
              add_edge(graph, pre);
  
          if (!dfs(graph))
              return {};
  
          vector&lt;int&gt; ret (topo.begin(),topo.end());
          return ret;
      }
  };
  </code></pre>