#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long long#define mo 998244353usingnamespace std;
constint N = 1e6 + 100;
structnode {
int to, nxt;
}e[N << 1];
int n, m, le[N], KK, dfn[N], low[N], col[N], sz[N];
int ru[N], sta[N], tot, S, T, id[N], xl[N], pre[N], suf[N];
vector <int> G[N], Gv[N];
pair <int, int> b[N];
ll f[N], g[N];
queue <int> q;
bool yes[N];
voidadd(int x, int y){
e[++KK] = (node){y, le[x]}; le[x] = KK;
}
voidtarjan(int now){
dfn[now] = low[now] = ++dfn[0]; sta[++sta[0]] = now;
for (int i = le[now]; i; i = e[i].nxt)
if (!dfn[e[i].to]) tarjan(e[i].to), low[now] = min(low[now], low[e[i].to]);
elseif (!col[e[i].to]) low[now] = min(low[now], dfn[e[i].to]);
if (dfn[now] == low[now]) {
sz[++tot]++; col[now] = tot;
while (sta[sta[0]] != now) {
sz[tot]++; col[sta[sta[0]]] = tot;
sta[0]--;
}
sta[0]--;
}
}
intmain(){
freopen("defeat.in", "r", stdin);
freopen("defeat.out", "w", stdout);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y); add(x, y); b[i] = make_pair(x, y);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= m; i++) {
int x = col[b[i].first], y = col[b[i].second];
if (x != y) G[x].push_back(y), ru[y]++, Gv[y].push_back(x);
}
for (int i = 1; i <= tot; i++) if (!ru[i]) q.push(i); int tmp = 0;
while (!q.empty()) {
int now = q.front(); q.pop(); id[now] = ++tmp; xl[tmp] = now;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i];
ru[x]--; if (!ru[x]) q.push(x);
}
}
for (int i = 1; i <= tot; i++) {
int to = tot + 1, x = xl[i];
for (int j = 0; j < G[x].size(); j++) {
int y = G[xl[i]][j];
to = min(to, id[y]);
}
pre[x + 1]++; pre[to]--;
to = 0;
for (int j = 0; j < Gv[x].size(); j++) {
int y = Gv[xl[i]][j];
to = max(to, id[y]);
}
suf[x - 1]++; suf[to]--;
}
for (int i = 1; i <= tot; i++) pre[i] = pre[i - 1] + pre[i];
for (int i = tot; i >= 1; i--) suf[i] = suf[i + 1] + suf[i];
int ans = 0;
for (int i = 1; i <= tot; i++)
if (!pre[id[i]] && !suf[id[i]]) ans += sz[i], yes[i] = 1;
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
if (yes[col[i]]) printf("%d ", i);
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现