#include #include #include using namespace std;

int main() { int n, m; cin >> n >> m; vector head(n + 1, 0); vector next(n + 1, 0); for (int i = 1; i <= n; i++) { head[i] = i; next[i] = 0; } for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; next[b] = head[a]; head[a] = b; } vector queue; for (int i = 1; i <= n; i++) { if (head[i] == i) { queue.push_back(i); int j = head[i]; while (next[j] != 0) { j = next[j]; queue.push_back(j); } } } cout << queue.size() << endl; for (int i = 0; i < queue.size(); i++) { cout << queue[i] << " "; } cout << endl; return 0; }


原文地址: https://www.cveoy.top/t/topic/qyF6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录