fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. vector<vector<int>> edges;
  6. vector<int> dist;
  7. queue<int> to_visit;
  8.  
  9. int bfs(int u)
  10. {
  11. dist.assign(1 + n, 0);
  12. dist[u] = 1;
  13. to_visit.push(u);
  14. while (!to_visit.empty()) {
  15. u = to_visit.front();
  16. to_visit.pop();
  17. for (int v: edges[u]) {
  18. if (dist[v] == 0) {
  19. dist[v] = dist[u] + 1;
  20. to_visit.push(v);
  21. }
  22. }
  23. }
  24. return *max_element(dist.begin(), dist.end());
  25. }
  26.  
  27. int main()
  28. {
  29. cin >> n;
  30. edges.assign(1 + n, vector<int>());
  31. for (int u = 1; u <= n; ++u) {
  32. int m;
  33. cin >> m;
  34. for (int j = 0; j < m; ++j) {
  35. int v;
  36. cin >> v;
  37. edges[u].push_back(v);
  38. }
  39. }
  40. int answer = 0;
  41. for (int u = 1; u <= n; ++u) {
  42. answer = max(answer, bfs(u));
  43. }
  44. cout << answer << endl;
  45. return 0;
  46. }
Success #stdin #stdout 0s 4168KB
stdin
13
1 2
2 3 4
2 5 6
1 7
0
2 8 9
0
1 10
0
0
1 12
1 13
0
stdout
6