Pagini recente » Cod sursa (job #2219790) | Cod sursa (job #1371667) | Cod sursa (job #1012348) | Cod sursa (job #3153747) | Cod sursa (job #1774360)
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
void DFS (unsigned int node);
void DFST (unsigned int node);
unsigned int N, M;
unsigned int x, y;
unsigned int *G[100001], *GT[100001];
bool seen[100001];
unsigned int postorder[100001];
unsigned int i, j, k;
unsigned int no;
vector < unsigned int > sol[100001];
int main ()
{
ifstream fin ("ctc.in");
fin >> N >> M;
for (i=1; i<=N; i++)
{
G[i] = (unsigned int *) realloc (G[i], sizeof (unsigned int));
G[i][0] = 0;
GT[i] = (unsigned int *) realloc (GT[i], sizeof (unsigned int));
GT[i][0] = 0;
}
for (i=1; i<=M; i++)
{
fin >> x >> y;
G[x][0]++;
G[x] = (unsigned int *) realloc (G[x], (G[x][0]+1) * sizeof (unsigned int));
G[x][G[x][0]] = y;
G[y][0]++;
G[y] = (unsigned int *) realloc (G[y], (G[y][0]+1) * sizeof (unsigned int));
G[y][G[y][0]] = x;
}
fin.close();
for (i=1; i<=N; i++)
if (!seen[i])
DFS(i);
for (i=N; i>=1; i--)
if (seen[postorder[i]])
{
no++;
DFST(postorder[i]);
}
ofstream fout ("ctc.out");
fout << no << '\n';
for (i=1; i<=no; i++)
{
for (j=0; j<sol[i].size(); i++)
fout << sol[i][j] << ' ';
fout << '\n';
}
fout.close();
return 0;
}
void DFS (unsigned int node)
{
unsigned int i;
seen[node] = 1;
for (i=1; i<=G[node][0]; i++)
if (!seen[G[node][i]])
DFS(G[node][i]);
k++;
postorder[k] = node;
}
void DFST (unsigned int node)
{
unsigned int i;
seen[node] = 0;
sol[no].push_back(node);
for (i=1; i<=GT[node][0]; i++)
if (seen[GT[node][i]])
DFST(GT[node][i]);
}