SHARE
TWEET

Untitled

a guest May 20th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void dfs(int **matr, int *viewed, int v, int n, int **matrN) {
  8.     viewed[v] = 1;
  9.  
  10.     for (int i = 0; i < n; ++i) {
  11.         if (matr[v][i] and !viewed[i]) {
  12.             matrN[v][i] = 1;
  13.             dfs(matr, viewed, i, n, matrN);
  14.         }
  15.     }
  16. }
  17.  
  18. int main() {
  19.     int n, m;
  20.     cin >> n >> m;
  21.     int **matr = new int *[n];
  22.     for (int i = 0; i < n; ++i) {
  23.         matr[i] = new int[n];
  24.     }
  25.  
  26.     int **matrN = new int *[n];
  27.     for (int i = 0; i < n; ++i) {
  28.         matrN[i] = new int[n];
  29.     }
  30.     for (int i = 0; i < n; ++i) {
  31.         for (int j = 0; j < n; ++j) {
  32.             matrN[i][j] = 0;
  33.         }
  34.     }
  35.  
  36.     int *viewed = new int[n];
  37.     for (int i = 0; i < n; ++i) {
  38.         viewed[i] = 0;
  39.     }
  40.  
  41.  
  42.     int I, J;
  43.     for (int i = 0; i < m; ++i) {
  44.         cin >> I >> J;
  45.         matr[I - 1][J - 1] = 1;
  46.     }
  47.  
  48.     dfs(matr, viewed, 0, n, matrN);
  49.  
  50.     for (int i = 0; i < n; ++i) {
  51.         for (int j = 0; j < n; ++j) {
  52.             if (matrN[i][j]) {
  53.                 cout << min(i, j) + 1 << " " << max(i, j) + 1 << endl;
  54.             }
  55.         }
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top