Pagini recente » Cod sursa (job #1887272) | Istoria paginii documentatie/arhiva-educationala | Cod sursa (job #3153291) | Cod sursa (job #3121542) | Cod sursa (job #2375647)
#include<bits/stdc++.h>
using namespace std;
ifstream f("arhiva.in");
ofstream g("arhiva.out");
int n,v[100001],m,i,nrc,nr[100001],x,y,j;
vector <int> G[100001],ctc[100001],Gt[100001];
stack <int> st;
void dfs1(int nod)
{
v[nod]=1;
for(int i=0;i<G[nod].size();i++)
if(!v[G[nod][i]])
dfs1(G[nod][i]);
st.push(nod);
}
void dfs2(int nod)
{
nr[nod]=nrc;
for(int i=0;i<Gt[nod].size();i++)
if(nr[Gt[nod][i]]==0)
dfs2(Gt[nod][i]);
}
int main()
{
f>>n>>m;
while(m--)
{
f>>x>>y;
G[x].push_back(y);
Gt[y].push_back(x);
}
for(i=1;i<=n;i++)
{
if(v[i]==0)
dfs1(i);
}
while(!st.empty())
{
int x=st.top();
st.pop();
if(nr[x]==0)
{
nrc++;
dfs2(x);
}
}
for(i=1;i<=n;i++)
ctc[nr[i]].push_back(i);
g<<nrc<<'\n';
for(i=1;i<=nrc;i++)
{
for(j=0;j<ctc[i].size();j++)
g<<ctc[i][j]<<" ";
g<<endl;
}
return 0;
}