B. Spongebob and Joke
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai.

It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively.

The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n).

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

Output

Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am.

If there are multiple suitable sequences ai, print "Ambiguity".

If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible".

Sample test(s)
input
  1. 3 3
    3 2 1
    1 2 3
output
  1. Possible
    3 2 1
input
  1. 3 3
    1 1 1
    1 1 1
output
  1. Ambiguity
input
  1. 3 3
    1 2 1
    3 3 3
output
  1. Impossible
Note

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

In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.

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

题意:就是b[i]=f[a[i]],如果不可能的话输出Impossible,多种情况的话输出Ambiguity,刚好只用一种情况输出a。

思路:Impossible的话说明f里面没有b中的值,Ambiguity的话说明b中的值在f中有多个。用第一行中的值作为数组f下标,位置作为值。那就是a[i]=f[b[i]];

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int f[],num[],ans[];
  4. int main()
  5. {
  6. int i,n,m,a,b;
  7. int flag1=,flag2=;
  8. scanf("%d%d",&n,&m);
  9. memset(f,,sizeof(f));
  10. memset(num,,sizeof(num));
  11. for(i=; i<=n; i++)
  12. {
  13. scanf("%d",&a);
  14. f[a]=i;
  15. num[a]++;
  16. }
  17. for(i=; i<m; i++)
  18. {
  19. scanf("%d",&b);
  20. ans[i]=f[b];
  21. num[b]--;
  22. if(ans[i]==) flag1=;
  23. if(num[b]>) flag2=;
  24. }
  25. if(flag1==) cout<<"Impossible"<<endl;
  26. else if(flag2==) cout<<"Ambiguity"<<endl;
  27. else
  28. {
  29. cout<<"Possible"<<endl;
  30. for(i=; i<m; i++)
  31. cout<<ans[i]<<" ";
  32. cout<<endl;
  33. }
  34. return ;
  35. }

Codeforces 599B. Spongebob and Joke 模拟的更多相关文章

  1. CodeForces 599B Spongebob and Joke

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

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

    B. Spongebob and Joke     While Patrick was gone shopping, Spongebob decided to play a little trick ...

  3. codeforce 599B Spongebob and Joke

    一道水题WA那么多发,也是醉了.f看成函数的话,其实就是判断一下反函数存不存在. 坑点,只能在定义域内判断,也就是只判断b[i].没扫一遍前不能确定Impossible. #include<bi ...

  4. Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题

    B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...

  5. Codeforces Round #332 (Div. 2)_B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

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

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. CF-599B - Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

  9. Codeforces Round #332 (Div. 二) B. Spongebob and Joke

    Description While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. ...

随机推荐

  1. 抽象工厂模式( Abstract Factory )

    提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.解决多产品多等级结构.模式的类图如下: 抽象工厂模式的优点: 易于交换产品系列,由于具体工厂类在一个应用中只需要在初始化的时候出现一 ...

  2. LaunchFaster 启动器工具 - 类似 Rolan 和音速启动的图标式快捷启动软件

    LaunchFaster 启动器是本人近期编写的一款windows平台上快速启动应用的开源工具软件. LaunchFaster 启动器是一款类似于 Rolan 和 音速启动 和 Lily 的图标形式的 ...

  3. 数据库之SQL笛卡尔积

    1.笛卡尔积定义 笛卡尔积在SQL中的实现方式既是交叉连接(Cross Join).所有连接方式都会先生成临时笛卡尔积表,笛卡尔积是关系代数里的一个概念,表示两个表中的每一行数据任意组合,上图中两个表 ...

  4. 19.XPath选择器

    1.extract():提取数据 2./text()     :获取节点内容文本 3./@href   :获取节点href属性 4. @         :获取属性名称 需要注意问题: 用定义的规则那 ...

  5. Angular5 UI post 请求 输出 文件下载

    this.httpClient.post(url1, JSON.parse(data1) , {responseType: 'blob'}).subscribe(data => { const ...

  6. C# ADO.NET 封装的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. UI5-文档-4.11-Pages and Panels

    在完成了应用程序结构的所有工作之后,是时候改进我们的应用程序的外观了.在这一步中,您还将了解控件聚合. Preview A panel is now displaying the controls f ...

  8. XML学习记录1-复习SAX,DOM和JAXB

    对xml文档的解析常见的有JDK中的sax,dom,jaxb,stax和JAVA类库JDOM和DOM4J,下面先说说前三个. Java中解析XML的工具很多,像JDOM,DOM4J等,但Java标准库 ...

  9. Kotlin语言学习笔记(6)

    运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...

  10. @Controller 和 @RestController的区别

    @Controller 和 @RestController的区别 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配 ...