# hihoCoder 1518-最大集合

hihoCoder 1518-最大集合

### 输出

```7
6 5 1 4 2 7 3```

`4`

```#include<iostream>
#include<vector>
#include<climits>
#include<algorithm>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<cmath>
using namespace std;
unordered_map<int, int> result;
void solve(vector<int>& A, int start) {
unordered_set<int> si;
si.insert(start);
int last = start;
while (si.find(A[last]) == si.end()) {
si.insert(A[last]);
last = A[last];
}
int ans = si.size();
unordered_set<int>::iterator it = si.begin();
while (it != si.end()) {
result[*it] = ans; // 循环内的所有S[K]都相等
++it;
}
}
int main() {
//freopen("input.txt", "r", stdin);
int n;
scanf("%d", &n);
vector<int> A(n + 1, 0);
for (int i = 1; i <= n; ++i)scanf("%d", &A[i]);
for (int i = 1; i <= n; ++i) {
if (result.find(A[i]) == result.end())solve(A, A[i]);
}
int ans = 0;
for (unordered_map<int, int>::iterator it = result.begin(); it != result.end(); ++it)ans = max(ans, it->second);
printf("%d\n", ans);
return 0;
}
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.