Pagini recente » Cod sursa (job #1306756) | Cod sursa (job #1508534) | Clasamentul arhivei Infoarena Monthly | Cei mai harnici utilizatori info-arena | Cod sursa (job #3216116)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
struct nod
{
vector <int> v;
};
nod g[100041], g2[100041];
vector <int> viz2;
stack <int> s;
bool viz[100041], viz3[100041];
int n, m, x, y;
void read()
{
fin >> n >> m;
for (int i=0;i<m;++i)
{
fin >> x >> y;
g[x].v.push_back(y);
g2[y].v.push_back(x);
}
}
void dfs1(int x)
{
// cout << x << " ";
if (viz[x])
return;
else
{
viz[x]=true;
for (int i=0;i<g[x].v.size();++i)
dfs1(g[x].v[i]);
s.push(x);
}
}
void dfs2(int x)
{
if (viz3[x])
return;
else
{
viz3[x]=true;
viz2.push_back(x);
for (int i=0;i<g2[x].v.size();++i)
if (!viz3[g2[x].v[i]])
dfs2(g2[x].v[i]);
}
}
int main()
{
read();
int c=0;
for (int i=1;i<=n;++i)
if (!viz[i])
dfs1(i);
while (!s.empty())
{
if (!viz3[s.top()]){
c++;
dfs2(s.top());
viz2.push_back(-1);
}
s.pop();
}
fout << c << "\n";
for (auto i:viz2)
{
if (i!=-1)
fout << i << " ";
else
fout << "\n";
}
return 0;
}