Title: Give you tasks and dependencies on these tasks, and ask for tasks with the longest dependency chain.
Analysis: Search. You can take advantage of Dfs or memory search.
(Because no ring is a tree structure, the solution for each root calculation is the final result and will not be updated).
Description: An array of tokens to be emptied each time.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;int maps[101][101];int used[101];int dfs (int s, int n) {if (Used[s]) r Eturn Used[s];int max = 0;for (int i = 1; I <= n; + + i) if (maps[s][i]) max = max (max, DFS (i, n) +1); return used[s] = M AX;} int main () {int n,m,p;while (~scanf ("%d", &n) && N) {memset (maps, 0, sizeof); memset (used, 0, sizeof (used )); for (int i = 1; I <= n; + + i) {scanf ("%d", &m); for (int j = 1; J <= M; + + j) {scanf ("%d", &p); maps[i][p] = 1;} Maps[0][i] = 1;} DFS (0, n); int space = 1;for (int i = 2; I <= n; + + i) if (Used[space] < used[i]) space = i;printf ("%d\n", Space);} return 0;}
UVa 10926-how many Dependencies?