Pagini recente » Cod sursa (job #318216) | Cod sursa (job #380154) | Cod sursa (job #119860) | Cod sursa (job #519000) | Cod sursa (job #935622)
Cod sursa(job #935622)
#include <iostream>
#include <fstream>
#include <vector>
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];
vector <int> numere;
void DF(int nod)
{
sel[nod] = true;
numere.push_back(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);
int nr = 0;
for (int i =1;i<=n;i++)
{
if (!sel[i])
{
nr++;
DF(i);
numere.push_back(-1);
}
}
out << nr << "\n";
for (int i =0;i<numere.size();i++)
{
if (numere[i] == -1)
{
out << "\n";
}
else
{
out << numere[i] << " ";
}
}
in.close();
out.close();
return 0;
}