Pagini recente » Produse | Cod sursa (job #33519) | Cod sursa (job #2270367) | Cod sursa (job #3227302) | Cod sursa (job #2866895)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("strazi.in");
ofstream out("strazi.out");
const int N = 255;
int n, m;
bool edge[N + 5][N + 5], fr[N + 5];
stack<int> s;
vector<int> adj[N + 5], ans;
vector<pair<int, int>> add;
void dfs(int node) {
fr[node] = true;
for (auto it: adj[node])
if (!fr[it])
dfs(it);
s.push(node);
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
in >> x >> y;
adj[x].push_back(y);
edge[x][y] = true;
}
for (int i = 1; i <= n; i++)
if (!fr[i])
dfs(i);
while (!s.empty()) {
ans.push_back(s.top());
s.pop();
}
for (int i = 0; i < ans.size() - 1; i++) {
int x = ans[i], y = ans[i + 1];
if (!edge[x][y])
add.emplace_back(x, y);
}
out << add.size() << '\n';
for (auto it: add)
out << it.first << ' ' << it.second << '\n';
for (auto it: ans)
out << it << ' ';
out << '\n';
return 0;
}