#include #include #include #include #include #include #include #include #include #include using namespace std; class Graph { public: vector < vector > Adj; vector Paths; vector Incident; long long Num_Paths(int from, int to); }; long long Graph::Num_Paths(int from, int to) { int i, n, t; deque zero_inc; /* Set all paths to zero, except for node from, where there is one. */ for (i = 0; i < Paths.size(); i++) Paths[i] = 0; Paths[from] = 1; /* You need a deque of nodes which have zero incident edges. The only node on this deque when you start is node from. */ zero_inc.push_back(from); /* Process the list. */ while (!zero_inc.empty()) { /* Pull the first node off, and delete it */ n = zero_inc[0]; zero_inc.pop_front(); /* If this is to, then we're done -- return the number of paths to to that is held in the vector Paths. */ if (n == to) return Paths[to]; /* Add the current node's number of paths to each node adjacent to it. Then "delete" the edge by decrementing that node's indicent edges. When that value hits zero, pust the node onto the deque. */ for (i = 0; i < Adj[n].size(); i++) { t = Adj[n][i]; Paths[t] += Paths[n]; Incident[t]--; if (Incident[t] == 0) zero_inc.push_back(t); } } return Paths[to]; /* This shouldn't happen because all nodes are reachable from node "from" */ } int main() { Graph G; int N; int from, to; cin >> N; G.Adj.resize(N); G.Paths.resize(N); G.Incident.resize(N, 0); while (cin >> from >> to) { G.Adj[from].push_back(to); G.Incident[to]++; } cout << G.Num_Paths(0, N-1) << endl; exit(0); }