链接:https://codeforces.com/contest/765/problem/D
题意;给n个数,分别是f(i),让你找n个g(i)和m(任意)个h(i),
其中,g(h(x))=x,h(g(x))=f(x);
题解:推一下这个式子,可得到h(x)=f(h(x)),g(x)=g(f(x));
从而推出,f(x)=f(f(x));判定h函数是否存在,h函数即为f函数的去重;
code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+10; 4 int a[maxn],b[maxn],c[maxn]; 5 int n,m; 6 map<int,int> mp; 7 int main() 8 { 9 scanf("%d",&n); 10 for(int i=1; i<=n; i++) cin>>a[i]; 11 for(int i=1; i<=n; i++) 12 { 13 if(a[a[i]]!=a[i]) 14 { 15 cout<<"-1\n"; 16 return 0; 17 } 18 } 19 for(int i=1; i<=n; i++) 20 { 21 if(mp[a[i]]==0) 22 { 23 m++; 24 mp[a[i]]=m; 25 c[m]=a[i]; 26 } 27 b[i]=mp[a[i]]; 28 } 29 cout<<m<<endl; 30 for(int i=1; i<=n; i++) 31 cout<<b[i]<<" "; 32 cout<<endl; 33 for(int i=1; i<=m; i++) 34 cout<<c[i]<<" "; 35 cout<<endl; 36 //system("pause"); 37 return 0; 38 }