Advertisement
FHVirus

Untitled

Nov 11th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #include<unistd.h>
  3. inline char RC(){static char buf[65536],*p=buf,*q=buf;return p==q&&(q=(p=buf)+read(0,buf,65536))==buf?-1:*p++;}
  4. inline void W(int n){char OB[12],OP=0,buf[12],p;if(n<0)n=-n,OB[OP++]='-';if(n==0)OB[OP++]='0';p=0;while(n)buf[p++]='0'+(n%10),n/=10;for(--p;p>=0;--p)OB[OP++]=buf[p];write(1,OB,OP);}
  5.  
  6. const int N = 3000000 + 225;
  7. int a[N], b[N];
  8. int occ[N], to[N];
  9. bool vis[N];
  10.  
  11. int main(){
  12.     int lena = 1, lenb = 1;
  13.     char c; int eek = 0;
  14.     while(true){
  15.         c = RC();
  16.         if(c == '\n'){
  17.             a[lena] = eek;
  18.             break;
  19.         }
  20.         if(c == ' '){
  21.             a[lena++] = eek, eek = 0;
  22.         }
  23.         if(c >= '0'){
  24.             eek = eek * 10 + c - '0';
  25.         }
  26.     }
  27.     eek = 0;
  28.     while(true){
  29.         c = RC();
  30.         if(c == '\n'){
  31.             b[lenb] = eek;
  32.             break;
  33.         }
  34.         if(c == ' '){
  35.             b[lenb++] = eek, eek = 0;
  36.         }
  37.         if(c >= '0'){
  38.             eek = eek * 10 + c - '0';
  39.         }
  40.     }
  41.     if(lena != lenb){
  42.         W(-1);
  43.         return 0;
  44.     }
  45.  
  46.     for(int i = 1; i <= lena; ++i){
  47.         occ[a[i]] = i;
  48.     }
  49.     for(int i = 1; i <= lenb; ++i){
  50.         to[i] = occ[b[i]];
  51.     }
  52.  
  53.     int ans = lena;
  54.     for(int i = 1; i <= lena; ++i){
  55.         if(!vis[i]){
  56.             --ans;
  57.             int p = to[i]; vis[i] = true;
  58.             while(p != i){
  59.                 vis[p] = true;
  60.                 p = to[p];
  61.             }
  62.         }
  63.     }
  64.     W(ans);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement