Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <algorithm>
- #include <cstdio>
- #include <math.h>
- #include <string>
- #include <stdio.h>
- #include <string.h>
- #include <string>
- #include <stdlib.h>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- #define N 100
- int g[N][N];
- bool visited[N];
- int n, s;
- vector <int> d;
- void dfs (int u) {
- d.push_back(u); //remember path
- visited[u] = true; //put visited immediately
- for (int i = 1; i <= n; i++) {
- if (!visited[i] && g[u][i] == 1) { //if next is not visited & there is a way between
- dfs (i); //dfs again
- }
- }
- }
- int main(int argc, const char * argv[]) {
- cin >> n >> s; //take size and starting point
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- cin >> g[i][j]; //input matrix
- }
- }
- dfs (s); //start dfs
- for (int i = 0; i < n; i++) {
- cout << d[i] << " "; //output path
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement