fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m, a, b, ret;
  4. vector<int> adj[10004], v;
  5.  
  6. int go(int here){
  7. int cnt = 0;
  8. for(int there : adj[here]){
  9. cnt += go(there);
  10. cnt++;
  11. }
  12. return cnt;
  13. }
  14. int main(){
  15. ios_base::sync_with_stdio(false);
  16. cin.tie(NULL); cout.tie(NULL);
  17. cin >> n >> m;
  18. for(int i = 0; i < m; i++){
  19. cin >> a >> b;
  20. adj[b].push_back(a);
  21. }
  22. for(int i = 0; i < m; i++){
  23. ret = max(ret, go(i));
  24. }
  25. for(int i = 0; i < m; i++){
  26. if(ret == go(i)) v.push_back(i);
  27. }
  28.  
  29. for(int c : v) cout << c << ' ';
  30. }
Success #stdin #stdout 0.01s 5268KB
stdin
5 4
3 1
3 2
4 3
5 3
stdout
1 2