Pagini recente » Cod sursa (job #691328) | Cod sursa (job #716729) | Cod sursa (job #1920074) | Cod sursa (job #2554619) | Cod sursa (job #1568585)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
vector <int> v[100001], vt[100001];
bitset <100001> viz;
stack <int> S, ST;
int x, n, m, i, xc, y, nr;
void DFS (int x)
{
viz[x]=1;
for (vector <int> ::iterator it=v[x].begin(); it!=v[x].end(); ++it)
{
if (viz[*it]==0)
{
DFS (*it);
}
}
S.push(x);
}
void DFST (int x)
{
viz[x]=1;
for (vector <int> ::iterator it=vt[x].begin(); it!=vt[x].end(); ++it)
{
if (viz[*it]==0)
{
DFST (*it);
}
}
ST.push(x);
}
int main()
{
fin >> n >> m;
for (i=1; i<=m; i++)
{
fin >> x >> y;
v[x].push_back(y);
vt[y].push_back(x);
}
for (i=1; i<=n; i++)
{
if (viz[i]==0) DFS (i);
}
viz.reset();
while (!S.empty())
{
xc = S.top(); S.pop();
if (viz[xc]==0)
{
ST.push (-1);
nr++;
//fout << "CTC" << nr << ": ";
DFST (xc);
//fout << "\n";
}
}
fout << nr << "\n";
while (!ST.empty())
{
xc=ST.top();
if(xc==-1) fout << "\n";
else fout << xc << " ";
ST.pop();
}
}