Pagini recente » Cod sursa (job #3215779) | Cod sursa (job #480195) | Cod sursa (job #1614855) | Cod sursa (job #3171661) | Cod sursa (job #2375634)
#include<bits/stdc++.h>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int n,v[10001],m,i,nrc,nr[10001],x,y,j;
vector <int> G[10001],ctc[10001];
stack <int> st;
void dfs1(int nod)
{
v[nod]=1;
for(int i=0;i<G[i].size();i++)
if(!v[nod])
dfs1(G[nod][i]);
st.push(nod);
}
void dfs2(int nod)
{
nr[nod]=nrc;
for(int i=0;i<G[nod].size();i++)
if(nr[G[nod][i]]==0)
dfs2(G[nod][i]);
}
int main()
{
f>>n>>m;
while(m--)
{
f>>x>>y;
G[x].push_back(y);
}
for(i=1;i<=n;i++)
{
if(v[i]==0)
dfs1(i);
}
while(!st.empty())
{
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;
}