Advertisement
Alex_tz307

USACO 2017 December Contest, Silver Problem 3. The Bovine Shuffle

Apr 20th, 2021
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("shuffle.in");
  6. ofstream fout("shuffle.out");
  7.  
  8. const int NMAX = 1e5;
  9. int a[NMAX], cnt[NMAX];
  10.  
  11. void solve() {
  12.   int N;
  13.   fin >> N;
  14.   for (int i = 0; i < N; ++i) {
  15.     fin >> a[i];
  16.     --a[i];
  17.     ++cnt[a[i]];
  18.   }
  19.   queue<int> Q;
  20.   for (int i = 0; i < N; ++i)
  21.     if (!cnt[i])
  22.       Q.emplace(i);
  23.   int ans = N;
  24.   while (!Q.empty()) {
  25.     int curr = Q.front();
  26.     Q.pop();
  27.     --ans;
  28.     if (--cnt[a[curr]] == 0)
  29.       Q.emplace(a[curr]);
  30.   }
  31.   fout << ans << '\n';
  32. }
  33.  
  34. void close_files() {
  35.   fin.close();
  36.   fout.close();
  37. }
  38.  
  39. int main() {
  40.   solve();
  41.   close_files();
  42.   return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement