voiddfs(int u, int id){ vis[u] = id; if (vis[a[u]]) { x[id] = vis[a[u]] == id ? u : -1; } else { dfs(a[u], id); } }
intmain(){ int n, u = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (a[i] == i) { u = i; } } int num = 0; for (int i = 1; i <= n; ++i) { if (!vis[i]) { dfs(i, ++num); } } if (u == 0) { for (int i = 1; i <= num; ++i) { if (x[i] != -1) { u = x[i]; break; } } } int ans = 0; for (int i = 1; i <= num; ++i) { if (x[i] != -1 && a[x[i]] != u) { a[x[i]] = u; ++ans; } } printf("%d\n", ans); for (int i = 1; i <= n; ++i) { printf("%d ", a[i]); } puts(""); return0; }