Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector <int> gr, g, d;
- int n, cycle, sizecyc, sizeleaf;
- pair <int, int> rek(int v) {
- if (d[v] == 0) {
- d[v] = 1;
- pair <int, int> u = rek(gr[v]);
- if (u.first == v) {
- cycle++;
- sizecyc += u.second + 1;
- }
- return { u.first, u.second + 1};
- }
- else {
- return { v, 0 };
- }
- }
- int main()
- {
- iostream::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- cin >> n;
- n++;
- gr.resize(n); g.resize(n, 0); d.resize(n, 0);
- for (int i = 1; i < n; i++) {
- int y;
- cin >> y;
- g[y] = 1;
- gr[i] = y;
- }
- for (int i = 1; i < n; i++) {
- if (g[i] == 0) {
- rek(i);
- sizeleaf++;
- }
- }
- for (int i = 1; i < n; i++) {
- if (d[i] == 0) {
- rek(i);
- sizeleaf++;
- }
- }
- cout << sizeleaf << " " << n - 1 - sizecyc + cycle;
- }
Advertisement
Add Comment
Please, Sign In to add comment