Pagini recente » Cod sursa (job #1511699) | Cod sursa (job #1550237) | Cod sursa (job #3267176) | Cod sursa (job #2637226) | Cod sursa (job #935621)
Cod sursa(job #935621)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const static int nmax = 5001;
int n , m;
int mat[nmax][nmax];
bool sel[nmax];
void DF(int nod)
{
sel[nod] = true;
cout << nod << " ";
for (int i = 1;i<=n;i++)
{
if (mat[nod][i] && mat[i][nod] && !sel[i])
{
DF(i);
}
}
}
int main()
{
in >> n >> m;
for (int i =1;i<=n;i++)
for (int j =1;j<=n;j++)
mat[i][j] = 0;
for (int i =0;i<m;i++)
{
int x , y;
in >> x >> y;
mat[x][y] = 1;
}
for (int i =1;i<=n;i++)
for (int j =1;j<=n;j++)
for (int k =1;k<=n;k++)
{
if (mat[i][k] && mat[k][j] && i !=j) mat[i][j] = 1;
}
fill(sel,sel+nmax,false);
for (int i =1;i<=n;i++)
{
if (!sel[i])
{
DF(i);
cout << "\n";
}
}
return 0;
}