DFS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
//ifstream cin("input.txt");
//ofstream cout("output.txt");
int a[110][110];
bool used [120];
void dfs (int x)
{
used [x]=true;
cout<<x<<" ";
for (int i=1;i<110;i++)
if ((a[x][i]==1)&&(!used[i])) dfs (i);
}
int main()
{ int n;
cin>>n;
for (int k=1;k<n;k++)
for (int j=1;j<n;j++)
cin>>a[k][j];
for (int l=1;l<n;l++)
if (!used[l]) dfs(l);
return 0;
}