Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast")
- #include<unistd.h>
- inline char RC(){static char buf[65536],*p=buf,*q=buf;return p==q&&(q=(p=buf)+read(0,buf,65536))==buf?-1:*p++;}
- 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);}
- const int N = 3000000 + 225;
- int a[N], b[N];
- int occ[N], to[N];
- bool vis[N];
- int main(){
- int lena = 1, lenb = 1;
- char c; int eek = 0;
- while(true){
- c = RC();
- if(c == '\n'){
- a[lena] = eek;
- break;
- }
- if(c == ' '){
- a[lena++] = eek, eek = 0;
- }
- if(c >= '0'){
- eek = eek * 10 + c - '0';
- }
- }
- eek = 0;
- while(true){
- c = RC();
- if(c == '\n'){
- b[lenb] = eek;
- break;
- }
- if(c == ' '){
- b[lenb++] = eek, eek = 0;
- }
- if(c >= '0'){
- eek = eek * 10 + c - '0';
- }
- }
- if(lena != lenb){
- W(-1);
- return 0;
- }
- for(int i = 1; i <= lena; ++i){
- occ[a[i]] = i;
- }
- for(int i = 1; i <= lenb; ++i){
- to[i] = occ[b[i]];
- }
- int ans = lena;
- for(int i = 1; i <= lena; ++i){
- if(!vis[i]){
- --ans;
- int p = to[i]; vis[i] = true;
- while(p != i){
- vis[p] = true;
- p = to[p];
- }
- }
- }
- W(ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement