Pagini recente » Borderou de evaluare (job #1252571) | Cod sursa (job #1252571)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
vector <int>v[100000],w[100000];
int n,m,i,j,x,y,q[100000],nr,ss;
int afis[10000];
bool ok[100000],oj[100000];
void df1(int x,int &i)
{vector <int>::iterator it;
ok[x]=true;
for (it=v[x].begin();it!=v[x].end();it++)
if (!ok[*it]) df1(*it,i);
q[++i]=x;
}
void df2(int x)
{vector <int>::iterator it;
oj[x]=true;
for (it=w[x].begin();it!=w[x].end();it++)
if (!oj[*it]) df2(*it);
}
int main()
{f>>n>>m;
for (i=1;i<=m;i++)
{f>>x>>y;
v[x].push_back(y);
w[y].push_back(x);
}
nr=0;
for (i=1;i<=n;i++) if (!ok[i])
df1(i,nr);
for (i=n;i>=1;i--) if (!oj[q[i]])
{afis[++ss]=i;
df2(q[i]);
}
g<<ss<<'\n';
for (j=1;j<=ss-1;j++)
{for (i=afis[j];i>afis[j+1];i--)
g<<q[i]<<" ";
g<<'\n';
}
for (i=afis[ss];i>=1;i--) g<<q[i]<<" ";
f.close();
g.close();
return 0;
}