Created
July 22, 2019 12:41
-
-
Save surinoel/a0f19e8d46737c9bd687b86ba1ea46a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int a[101]; | |
int d[101]; | |
int p[101]; | |
bool b[101]; | |
int ans = 0; | |
int dfs(int idx, int cnt, int parent) { | |
if (d[idx] != 0) { | |
if (p[idx] == parent) { | |
ans += cnt - d[idx]; | |
return d[idx]; | |
} | |
else { | |
return -1; | |
} | |
} | |
d[idx] = cnt; | |
p[idx] = parent; | |
int ret = dfs(a[idx], cnt + 1, parent); | |
if (ret == -1 || cnt < ret) { | |
b[idx] = false; | |
} | |
else if(cnt >= ret) { | |
b[idx] = true; | |
} | |
return ret; | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
for (int i = 1; i <= n; i++) { | |
int x; cin >> x; | |
a[i] = x; | |
} | |
for (int i = 1; i <= n; i++) { | |
if (d[i] != 0) continue; | |
dfs(i, 1, i); | |
} | |
cout << ans << '\n'; | |
for (int i = 1; i <= n; i++) { | |
if (b[i] == 1) { | |
cout << i << '\n'; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment