Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried this, its fast but I am not sure whether it is correct. When you reach a black node it means that you have already calculated path from that node so I stored the number of paths in the structure of node and used it.</p> <hr> <pre><code>#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #define WHITE 0 #define GRAY 1 #define BLACK 2 using namespace std; typedef struct vertexstruct{ int color; vector&lt;int&gt; edgelist; long paths; }vertex; int cyclenode; int throughend; long dfs(int v,vertex *vertices,int numvertices){ long paths = 0; if(vertices[v].color == BLACK) return vertices[v].paths; if(vertices[v].color == GRAY) { if(cyclenode == 0)cyclenode = v; } if(vertices[v].color == WHITE){ vertices[v].color = GRAY; for(int i=0;i&lt;vertices[v].edgelist.size();i++){ long x = dfs(vertices[v].edgelist[i],vertices,numvertices); if(cyclenode) vertices[v].cycle=1; if(x == -1) return -1; paths += x; } if(cyclenode &amp;&amp; paths &gt;0) return -1; if(v == numvertices){ if(cyclenode) { return -1; } throughend = 0; vertices[v].paths = 1; return 1; } if(cyclenode ==v &amp;&amp; throughend == 0) cyclenode = 0; vertices[v].color = BLACK; vertices[v].paths = paths % 1000000000; } return paths % 1000000000; } void numpaths(int numvertices,int numedges,vertex *vertices){ long numpaths = 0; cyclenode = 0; throughend =0; numpaths = dfs(1,vertices,numvertices); if(numpaths == -1) printf("INFINITE PATHS\n"); else printf("%ld\n",numpaths); } int main(){ int edgecount=0; int numverts,numedges; fscanf(stdin,"%d %d",&amp;numverts,&amp;numedges); vertex verts[numverts+1]; for(int i =1;i&lt;=numverts;i++){ verts[i].color = WHITE; verts[i].paths = 0; } edgecount = 0; int a,b; while(edgecount &lt; numedges){ scanf("%d %d",&amp;a,&amp;b); verts[a].edgelist.push_back(b); edgecount++; } numpaths(numverts,numedges,verts); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload