Code to calculate minimum number of pushes required to put the whole dominoes set down The question is this one on UVa, and it basically gives us n, the number of dominoes and (a,b) relations such that pushing a will cause b to fall down too. First t, the test cases is inputted and then number of vertices and edges, and the (a,b) relations.



My approach

My approach was I first run a dfs on the graph (till all elements are reached) and maintain an array that shows when each vertex was reached [somewhat inspired from Kosaraju's algo for SCCs]. If a-&gt;b-&gt;c is my dfs tree, reached[c] will be equal to 3, reached[b] be 2, and reached[a] be 1. This step is O(vertices+edges).

Then I run DFSs from vertices starting from those with minimum reached value. Every time the DFS runs, I increment a counter. This is also O(vertices+edges).



I tried for smaller test cases and got the right answer, but submitting on UVa gives me TLE. I don't know if my logic is wrong or if my implementation is messed up somewhere. The code is here:

#include&lt;bits/stdc++.h&gt;
using namespace std;

int n,counter,reached[100002],visited[100002],visiting[100002];
void dfs(int u,vector&lt; vector&lt;int&gt; &gt; graph){
    visiting[u]++;
    for(auto const&amp; i:graph[u]){
        if(!visiting[i])
            dfs(i,graph);
    }
    visited[counter]=u;
    counter--;
}
void dfs2(int u,vector&lt; vector&lt;int&gt; &gt; graph){
    reached[u]++;
    for(auto const&amp; i:graph[u]){
        if(!reached[i])
            dfs2(i,graph);
    }
}
int main()
{
    int t;
    cin&gt;&gt;t;
    while(t--){
        memset(visiting,0,sizeof visiting);
        memset(visited,0,sizeof visited);
        cin&gt;&gt;n;
        vector&lt; vector&lt;int&gt; &gt; graph(n+1);
        int m;
        counter=n;
        cin&gt;&gt;m;
        while(m--)
        {
            int a,b;
            cin&gt;&gt;a&gt;&gt;b;
            graph[a].push_back(b);
        }
        int mcounter=0;
        for(int i=1;i&lt;n+1;i++)if(!visiting[i])dfs(i,graph);
        memset(reached,0,sizeof reached);
        for(int i=1;i&lt;n+1;i++){
            if(!reached[visited[i]]){
                dfs2(visited[i],graph);
                mcounter++;
            }
        }
        cout&lt;&lt;mcounter&lt;&lt;'\n';
    }
    return 0;
}
Alternate algorithm
I would suggest the following:

After reading the list of domino dependencies, you should be able to compute a set of &quot;root&quot; dominos.  A &quot;root&quot; domino is simply a domino that is not pushed by any other domino.
Each root domino must be pushed, since there is no other domino to push it.  So for each root domino, push it, and do a dfs to mark all dominos knocked down by it.
After step #2, there may be dominos left standing.  These dominos must either be part of a cycle, or hanging off of a cycle.
For each remaining domino, do a dfs on it, looking for a cycle.
If the domino did not cycle, then do not increment the push count, but mark all the dominos in the dfs as knocked over.  This was a domino hanging off a cycle, and you know that this domino's parent(s) will eventually get knocked over by some other push.
If the domino did cycle, then keep a list of each domino that was part of its cycle (note that there could be more than one cycle through the same domino).  After doing the dfs and marking all dominos as knocked down, go through the list of dominos that were part of its cycle.  If any of them have a parent that is still standing, then do not increment the push count.  The reason is that the still standing parent will get pushed by some other domino, and it will knock down this whole cycle.  In other words, if a cycle of dominos has an external parent, then it can be ignored because the push from another cycle that triggers the external parent will knock down this cycle.

I haven't actually written a program to test this algorithm, so there could be a flaw in my logic dealing with cycles.  But I think it should work, and it should be fast because you never process a node more than once.