Pagini recente » Cod sursa (job #2284496) | Cod sursa (job #1808037) | Cod sursa (job #2740363) | Cod sursa (job #1706022) | Cod sursa (job #1494961)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> G[100000];
vector <int> Gi[100000];
vector <int> Go[100000];
vector <int> used;
vector <int> where;
vector <int> discovered;
void DFP(const int n ,vector <int> *G)
{
vector<int>::iterator it;
used[n]=true;
for(it=G[n].begin();it!=G[n].end();it++)
if(used[*it]==false)
DFP(*it,G);
discovered.push_back(n);
}
void DFM(const int n ,const int which, vector <int> *G)
{
vector<int>::iterator it;
where[n]=which;
for(it=G[n].begin();it!=G[n].end();it++)
if(where[*it]==-1)
DFM(*it,which,G);
}
int main()
{
int n,m,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+1);
used.assign(n+1,0);
where.resize(n+1);
where.assign(n+1,-1);
for(int i=0;i<n;i++)
if(used[i]==false)
DFP(i,Go);
int coun=0;
for(;discovered.size();discovered.pop_back())
if(where[discovered.back()]==-1){
DFM(discovered.back(),coun,Gi);
coun++;
}
for(int i=0;i<n;i++)
G[where[i]].push_back(i);
vector<int>::iterator it;
fout<<coun<<'\n';
for(int i=0;i<coun;i++)
{
for(it=G[i].begin();it!=G[i].end();it++)
fout<<*it+1<<' ';
fout<<'\n';
}
}