Codeforces Round #332 (Div. 2) B. Spongebob and Joke simulation

Source: Internet
Author: User

B. Spongebob and Joke

While Patrick is gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick ' s personal stuff and found a sequencea1,a2,...,a m  of length  m , consisting of integers From 1 to  n , not necessarily distinct. Then he picked some Sequence  F 1, F 2, ..., f n  of length  n  and for each number  a i  got number  b I = F a i . To finish the prank he erased the initial sequence  a i .

It's hard-to-express how sad Patrick is when he returned home from shopping! We'll just say that Spongebob immediately got really sorry on what he had done and he's now trying to restore the OR Iginal sequence. Help him does this or determine, that's impossible.

Input

The first line of the input contains the integers n and m (1≤ n, m ≤100 -the lengths of sequences Fi and bi respectively.

The second line contains n integers, determining sequence F1, F2, ..., f n (1≤ fin).

The last line contains m integers, determining sequence b1, b2, ... , bm (1≤ bin).

Output

Print "possible" if there is exactly one sequence  A i , such that  b i = f a i  for all  i  from 1 to   m . Then Print  m  integers  a 1, a 2, ..., a m .

If There is multiple suitable sequences ai, print "ambiguity".

If Spongebob have made a mistake in his calculations and no suitable sequence ai exists, print " C9>impossible ".

Sample Test (s) input
3 3
3 2 1
1 2 3
Output
Possible
Input
3 3
1 1 1
1 1 1
Output
Ambiguity
input
3 3
1 2 1
3 3 3
Output
Impossible
Note

In the first sample 3 are replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.

In the second sample all numbers be replaced by 1, so it's impossible to unambiguously restore the original sequence .

In the third sample fI ≠3 for all i, so no sequence ai transforms to such bi and we can say for sure that Spongebob have made a mistake.

Test instructions

Give you b array, f array, construct an array of a to satisfy: bi = fai. There are multiple sets of conditions output ambiguity, a set of conditions output it, no output impossblie

Exercises

We mark the F-array, count to find the only one.

//1085422276#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_backinline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){        if(ch=='-') f=-1; ch=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************Const intn=100000+ -;#defineMAXN 100000+5intH[n],ans[n],c[n],b[n],f[n];intMain () {intN=read (), m=read ();  for(intI=1; i<=n;i++) {scanf ("%d",&F[i]); H[f[i]]++; C[f[i]]=i; }     for(intI=1; i<=m;i++) {scanf ("%d",&B[i]); }intf=0, siz=0, cool=0;  for(intI=1; i<=m;i++) {        if(h[b[i]]>1) f=1; if(h[b[i]]==1&AMP;&AMP;!F) {ans[++siz]=C[b[i]];} if(h[b[i]]==0) cool=1; }    if(cool) {cout<<"Impossible"<<Endl; }    Else if(f) {cout<<"Ambiguity"<<Endl; }    Else{cout<<"Possible"<<Endl;  for(intI=1; i<=siz;i+=1) {cout<<ans[i]<<" "; }    }    return 0;}
Code

Codeforces Round #332 (Div. 2) B. Spongebob and Joke simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.