fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool isAmbiguous(vector<int>& arr)
  6. {
  7. int i;
  8. vector<int> inverse(arr.size());
  9. for( i = 0; i < arr.size(); i++ )
  10. {
  11. inverse[arr[i]-1] = i+1;
  12. }
  13. for( i = 0; i < arr.size(); i++ )
  14. {
  15. if( arr[i] != inverse[i] )
  16. return false;
  17. }
  18. return true;
  19. }
  20.  
  21. int main()
  22. {
  23. int n;
  24. while(true)
  25. {
  26. cin >> n;
  27. if( n > 0 )
  28. {
  29. vector<int> arr(n);
  30. int i;
  31. for( i = 0; i < n; i++ )
  32. {
  33. cin >> arr[i];
  34. }
  35. if( isAmbiguous(arr) )
  36. {
  37. cout << "ambiguous" << endl;
  38. }
  39. else
  40. {
  41. cout << "not ambiguous" << endl;
  42. }
  43. }
  44. else
  45. break;
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 15240KB
stdin
4
1 4 3 2
5
2 3 4 5 1
1
1
0
stdout
ambiguous
not ambiguous
ambiguous