Advertisement
MattDovi

#583

Dec 4th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #define NMAX 102
  3.  
  4. using namespace std;
  5.  
  6. int n, m, a[NMAX][NMAX], t[NMAX][NMAX], viz[NMAX], q[NMAX];
  7. int k, nrctc;
  8.  
  9. void df(int x) {
  10.     viz[x] = 1;
  11.     for(int j = 1; j <= n; j++){
  12.         if(a[x][j] == 1 && viz[j] == 0)
  13.             df(j);
  14.     }
  15.     q[++k] = x;
  16. }
  17.  
  18. void dfTranspus(int x) {
  19.     viz[x] = 1;
  20.     for(int j = 1; j <= n; j++){
  21.         if(t[x][j] == 1 && viz[j] == 0)
  22.             dfTranspus(j);
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     int x, y;
  29.     cin >> n >> m;
  30.  
  31.     for(int i = 1; i <= m; i++) {
  32.         cin >> x >> y;
  33.         a[x][y] = 1;
  34.         t[y][x] = 1;
  35.     }
  36.  
  37.     for(int i = 1; i <= n; i++){
  38.         if(viz[i] == 0) df(i);
  39.     }
  40.     for(int i = 1; i <= n; i++) viz[i] = 0;
  41.  
  42.     for(int i = n; i >= 1; i--){
  43.         if(viz[q[i]] == 0){
  44.             dfTranspus(q[i]);
  45.             nrctc++;
  46.         }
  47.     }
  48.  
  49.     cout << nrctc << '\n';
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement