Pagini recente » Cod sursa (job #1056222) | Cod sursa (job #945198) | Cod sursa (job #2421074) | Cod sursa (job #1521654) | Cod sursa (job #1638696)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> G[100005];
vector <int> Go[100005];
vector <int> Gi[100005];
vector <int> where;
vector <int> discovered;
vector <bool> used;
void DFP(const int n, vector <int> *G)
{
vector <int> :: iterator it;
used[n]=1;
for(it=G[n].begin();it!=G[n].end();it++)
{
if(used[*it]==0)
DFP(*it,G);
}
discovered.push_back(n);
}
void DFM(const int n, const int witch, vector <int> *G)
{
vector <int> :: iterator it;
where[n]=witch;
for(it=G[n].begin();it!=G[n].end();it++)
{
if(where[*it]==0)
DFM(*it,witch,G);
}
}
int main()
{
int n,m, cons=1,x,y;
fin>>n>>m;
for(int i=1; i <= m; i++)
{
fin>>x>>y;
Go[x-1].push_back(y-1);
Gi[y-1].push_back(x-1);
}
used.resize(n);
used.assign(n,0);
for(int i=0;i<n;i++)
if(used[i]==0)
DFP(i,Go);
where.resize(n);
where.assign(n,0);
for(;discovered.size();discovered.pop_back())
if(where[discovered.back()]==0)
{
DFM(discovered.back(),cons,Gi);
cons++;
}
for(int i=0;i<n;i++)
G[where[i]].push_back(i);
vector <int> :: iterator it;
fout<<cons-1<<'\n';
for(int i=1;i<cons;i++)
{
for(it=G[i].begin();it!=G[i].end();it++)
fout<<*it+1<<' ';
fout<<'\n';
}
}