Pagini recente » Cod sursa (job #1963615) | Cod sursa (job #728916) | Cod sursa (job #1707575) | Cod sursa (job #3358273) | Cod sursa (job #2038510)
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <vector>
using namespace std;
FILE *f=fopen("ctc.in","r");
FILE *g=fopen("ctc.out","w");
int id[100005];
vector <int> G[100005];
vector<vector<int> > compo;
int low[100005];
int stiva[100005];
bool instiva[100005];
int i,N,M,x,y,nr;
int lastid;
void dfs(int nod)
{
low[nod]=id[nod]=++lastid;
stiva[++stiva[0]]=nod;
instiva[nod]=1;
for(auto it:G[nod])
{
if(!id[it])dfs(it);
if(instiva[it])low[nod]=min(low[nod],low[it]);
}
if(low[nod]==id[nod])
{
vector<int> V;
while(stiva[stiva[0]]!=nod)
{
/// which[stiva[stiva[0]]]=compo.size()+1;
instiva[stiva[stiva[0]]]=0;
V.push_back(stiva[stiva[0]]);
stiva[0]--;
}
///which[nod]=compo.size()+1;
instiva[nod]=0;
V.push_back(nod);
stiva[0]--;
compo.push_back(V);
}
}
int main()
{
fscanf(f,"%d %d",&N,&M);
for(i=1;i<=M;i++)
{
fscanf(f,"%d %d",&x,&y);
G[x].push_back(y);
}
for(int i=1;i<=N;i++)if(!id[i])dfs(i);
fprintf(g,"%d\n",compo.size());
for(auto i:compo)
{
for(auto it:i)
fprintf(g,"%d ",it);
fprintf(g,"\n");
}
fclose(f);
fclose(g);
return 0;
}