Pagini recente » Cod sursa (job #2264717) | Cod sursa (job #2242396) | Cod sursa (job #1769804) | Cod sursa (job #2255579) | Cod sursa (job #1977028)
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
ofstream fout("x.txt");
struct graph
{
vector <int> v;
};
graph G[Nmax],GG[Nmax];
bitset <Nmax> viz;
int nr;
int pord[Nmax];
void DFSG(int x)
{
int i;
viz[x]=true;
for(i=0;i<G[x].v.size();i++)
if(!viz[G[x].v[i]]) DFSG(G[x].v[i]);
pord[++nr]=x;
}
void DFSGG(int x)
{
int i;
fout<<x<<' ';
viz[x]=false;
for(i=0;i<GG[x].v.size();i++)
if(viz[GG[x].v[i]]) DFSGG(GG[x].v[i]);
}
int main()
{int n,m,k,i,j;
f>>n>>m;
for(k=1;k<=m;k++)
{
f>>i>>j;
G[i].v.push_back(j);
GG[j].v.push_back(i);
}
for(i=1;i<=n;i++)
if(!viz[i]) DFSG(i);
nr=0;
for(i=n;i>0;i--)
if(viz[pord[i]])
{
nr++;
DFSGG(pord[i]);
fout<<'\n';
}
g<<nr<<'\n';
fout.close();
ifstream fin("x.txt");
char ch;
while(fin>>noskipws>>ch)
g<<ch;
return 0;
}