Advertisement
pb_jiang

ARC097D

Nov 28th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. // Problem: D - Equals
  2. // Contest: AtCoder - AtCoder Regular Contest 097
  3. // URL: https://atcoder.jp/contests/arc097/tasks/arc097_b
  4. // Memory Limit: 1024 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. #include <atcoder/dsu>
  12. using namespace std;
  13. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  14. template <typename... Args> void logger(string vars, Args &&... values)
  15. {
  16.     cerr << vars << " = ";
  17.     string delim = "";
  18.     (..., (cerr << delim << values, delim = ", "));
  19.     cerr << endl;
  20. }
  21.  
  22. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  23. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  24. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  25. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  26.  
  27. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  28. using vi = vector<int>;
  29. using ll = long long;
  30. using pii = pair<int, int>;
  31. using atcoder::dsu;
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     int n, m;
  36.     cin >> n >> m;
  37.     vi p(n);
  38.     for (int i = 0; i < n; ++i)
  39.         cin >> p[i];
  40.     dsu d(n);
  41.     int x, y;
  42.     for (int i = 0; i < m; ++i) {
  43.         cin >> x >> y;
  44.         d.merge(x - 1, y - 1);
  45.     }
  46.     int ans = 0;
  47.     for (const auto &g : d.groups()) {
  48.         set<int> s(g.begin(), g.end());
  49.         for (auto x : g) {
  50.             if (s.count(p[x] - 1))
  51.                 ans++;
  52.         }
  53.     }
  54.     cout << ans << endl;
  55.     return 0;
  56. };
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement