fork download
  1. //this program has been specifically designed not to run on java..so this is the cpp equivalent of the java code
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. int main()
  6. {
  7. ios::sync_with_stdio(false);
  8. cin.tie(NULL);
  9. int t,cnt=1;
  10. scanf("%d",&t);
  11. while(t--)
  12. {
  13. int n,r,i;
  14. scanf("%d%d",&n,&r);
  15. vector<int> g[n]; bool vis[n]; int deg[n];
  16. for(i=0;i<n;i++)
  17. {
  18. g[i].clear();
  19. vis[i]=false;
  20. deg[i]=0;
  21. }
  22.  
  23. for(i=0;i<r;i++)
  24. {
  25. int l,f;
  26. scanf("%d%d",&l,&f);
  27. g[f].push_back(l);
  28. deg[l]++;
  29. }
  30.  
  31. queue<pair<int,int>> q;
  32. vector<pair<int,int>> ans;
  33. for(i=0;i<n;i++)
  34. if(deg[i]==0)
  35. {
  36. q.push({i,1});
  37. vis[i]=true;
  38. }
  39.  
  40. while(!q.empty())
  41. {
  42. pair<int,int> x;
  43. x.first=q.front().first;
  44. x.second=q.front().second;
  45. q.pop();
  46. ans.push_back({x.second,x.first});
  47. for(i=0;i<g[x.first].size();i++)
  48. if(!vis[g[x.first][i]])
  49. {
  50. deg[g[x.first][i]]--;
  51. if(deg[g[x.first][i]]==0)
  52. {
  53. q.push({g[x.first][i],x.second+1});
  54. vis[g[x.first][i]]=true;
  55. }
  56. }
  57. }
  58.  
  59. sort(ans.begin(),ans.end());
  60. printf("Scenario #%d:\n",cnt);
  61. cnt++;
  62. for(i=0;i<ans.size();i++)
  63. printf("%d %d\n",ans[i].first,ans[i].second);
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 4380KB
stdin
2
5 6
2 0
2 4
1 4
1 2
3 2
4 0
5 4
1 0
2 0
3 2
4 2
stdout
Scenario #1:
1 0
2 4
3 2
4 1
4 3
Scenario #2:
1 0
2 1
2 2
3 3
3 4