Pagini recente » Cod sursa (job #2488335) | Cod sursa (job #1582851) | Cod sursa (job #2482645) | Statistici Oncescu Andreea (etgeniuand) | Cod sursa (job #2781114)
#include <iostream>
#include <vector>
#include <fstream>
#define VMAX 100000
#define EMAX 200000
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> adj[VMAX];
vector <int> aux[VMAX];
vector <int> toate[EMAX];
int V, E, x, y;
//stack <int> st;
bool vis[VMAX];
int stiva[VMAX], lg_stiva;
void DFS(int u)
{
vis[u] = 1;
for (int w = 0; w < adj[u].size(); ++w)
if (!vis[adj[u][w]]) DFS(adj[u][w]);
stiva[lg_stiva++] = u;
}
void DFS_2(int u, int contor)
{
vis[u] = true;
toate[contor].push_back(u);
for (int w = 0; w < aux[u].size(); ++w)
if (vis[aux[u][w]] == 0) DFS_2(aux[u][w], contor);
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> V >> E;
for (int i = 0; i < E; i++)
{
fin >> x >> y;
x--, y--;
adj[x].push_back(y);
aux[y].push_back(x);
}
for (int i = 0; i < V; i++)
if (vis[i] == false) DFS(i);
for (int i = 0; i < V; i++)
vis[i] = false;
int nr = 0, contor = 0;
for (int i = lg_stiva - 1; i >= 0; i--)
{
x = stiva[i];
if (vis[x] == false)
{
DFS_2(x, contor);
contor++;
}
}
/*while (!st.empty())
{
x = st.top();
if (vis[x] == false)
{
DFS_2(x, contor);
contor++;
}
st.pop();
}*/
fout << contor << endl;
for (int i = 0; i < contor; ++i)
{
for (int j = 0; j < toate[i].size(); ++j)
fout << toate[i][j] + 1 << " ";
fout << endl;
}
fin.close();
fout.close();
return 0;
}