Pagini recente » Cod sursa (job #1610845) | Cod sursa (job #2686400) | Cod sursa (job #3193857) | Cod sursa (job #1285526) | Cod sursa (job #1244754)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <list>
//#include <windows.h>
//#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <limits.h>
#include <string>
#include <math.h>
using namespace std;
#define forA(V,it) for (typeof(V.begin()) it=V.begin();it!=V.end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define MOD 1000000007
#define INF (1<<31)-1
#define MINF -(1<<31)
#define vi vector <int>
#define vll vector <ll>
#define pii pair <int,int>
#define pll pair <ll,ll>
#define newl printf("\n")
#define DIM 100005
int N,M,i;
vi Go[DIM],Gi[DIM],G[DIM],dsc,where;
bool used[DIM];
void DFP(int nod)
{
used[nod]=true;
for (vi::iterator it=Go[nod].begin();it!=Go[nod].end();it++)
if (!used[*it]) DFP(*it);
dsc.pb(nod);
}
void DFM(int nod,int k)
{
where[nod]=k;
for (vi::iterator it=Gi[nod].begin();it!=Gi[nod].end();it++)
if (where[*it]==-1) DFM(*it,k);
}
int main()
{
freopen("ctc.in","r",stdin);
freopen("ctc.out","w",stdout);
scanf("%d %d",&N,&M);
for (i=1;i<=M;i++)
{
int x,y;
scanf("%d %d",&x,&y);
Go[x].pb(y);
Gi[y].pb(x);
}
for (i=1;i<=N;i++) if (!used[i]) DFP(i);
where.resize(N+1); where.assign(where.size(),-1);
int cnt=0;
for (;!dsc.empty();dsc.pop_back())
if (where[dsc.back()]==-1)
DFM(dsc.back(),++cnt);
for (i=1;i<=N;i++) G[where[i]].pb(i);
printf("%d\n",cnt);
for (i=1;i<=cnt;i++,newl)
for (vi::iterator it=G[i].begin();it!=G[i].end();it++)
printf("%d ",*it);
return 0;
}