fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sp " "
  5. #define el '\n'
  6. #define ll long
  7. int n;
  8. vector<int> p;
  9. vector<int> d;
  10. vector<int>vis;
  11. map<int,int>mp;
  12. ll ans = 0;
  13. set<int>s;
  14. void dfs(int i){
  15. if(!vis[i]){
  16. vis[i] = 1;
  17. int k = p[i];
  18. p[i] = mp[k];
  19. s.erase(k);
  20. s.insert(p[i]);
  21. if(s.size() == n) {
  22. ans++;
  23. return;
  24. }
  25. else{
  26. ans++;
  27. dfs(mp[p[i]]);
  28. }
  29. }
  30. }
  31. void Solution()
  32. {
  33. cin >> n;
  34. p = vector<int> (n+1);
  35. d = vector<int> (n+1);
  36. vis = vector<int> (n+1,0);
  37. for(int i = 1; i <= n; i++){
  38. cin >> p[i];
  39. s.insert(p[i]);
  40. mp[p[i]] = i;
  41. }
  42. for(int i = 1; i <= n; i++){
  43. cin >> d[i];
  44. }
  45. for(int i = 1; i <= n; i++){
  46. if(!vis[d[i]]) dfs(d[i]);
  47. cout << ans << sp;
  48. }
  49. cout << el;
  50. ans = 0;
  51. }
  52. int main()
  53. {
  54. ios::sync_with_stdio(0);
  55. cin.tie(0); cout.tie(0);
  56. int t;
  57. cin >> t;
  58. for(int tc = 1; tc <= t; tc++)
  59. Solution();
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout