Pagini recente » Cod sursa (job #426406) | Cod sursa (job #1519947) | Istoria paginii runda/vot | Cod sursa (job #757136) | Cod sursa (job #846479)
Cod sursa(job #846479)
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <deque>
using namespace std;
#define Max 100001
vector<int>g[Max],t[Max],c[Max];
bool viz[Max],gviz[Max],tviz[Max];
int n;
void dfs(int x,vector<int>g[],bool viz[])
{
int y;
viz[x]=1;
for(int i=0;i<g[x].size();i++)
{
y=g[x][i];
if(!viz[y])dfs(y,g,viz);
}
}
void ctc()
{
int nr=0;
for(int i=1;i<=n;i++)
if(!viz[i])
{
memset(gviz,0,sizeof(gviz));
memset(tviz,0,sizeof(tviz));
dfs(i,g,gviz);
dfs(i,t,tviz);
nr++;
for(int i=1;i<=n;i++)
if(gviz[i] && tviz[i])
{
viz[i]=1;
c[nr].push_back(i);
}
}
printf("%d\n",nr);
for(int i=1;i<=nr;i++)
{
for(int j=0;j<c[i].size();j++)printf("%d ",c[i][j]); printf("\n");
}
}
int main()
{
int m,x,y;
freopen("ctc.in","r",stdin);
freopen("ctc.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d",&x,&y);
g[x].push_back(y);
t[y].push_back(x);
}
ctc();
return 0;
}