关闭

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

标签: codeforces哈希
482人阅读 评论(0) 收藏 举报
分类:
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
3 3
3 2 1
1 2 3
output
Possible
3 2 1 
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 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.

用哈希过的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int max1 = 101000;

struct node {
	int x, y;
}a[max1];

int b[max1];
int f[max1];
int a1[max1];

int main() {
	memset(f, 0, sizeof(f));
	memset(a, 0, sizeof(a));
	memset(a1, 0, sizeof(a1));
	memset(b, 0, sizeof(b));
	for (int i = 0; i<max1; i++)
		a[i].x = 0;
	int n, m;
	cin >> n>> m;
	for (int i = 1; i<=n; i++) {
		cin >> f[i];
		a[f[i]].x ++ ;
		a[f[i]].y = i;
	}
	for (int i = 1; i<=m; i++) 
		cin >> b[i];
	int flag1 = 0;
	int flag = 0;
	int num = 1;
	for (int i = 1; i<=m; i++) {
		if (a[b[i]].x == 1) {
			a1[num++] = a[b[i]].y;
		}
		else if (a[b[i]].x >= 2) {
			a1[num++] = a[b[i]].y;
			flag = 1;
		}
		else if (a[b[i]].x == 0) {
			flag = 2;
			break;
		}
	}
	if (flag == 2)
		cout << "Impossible" << endl;
	else if (flag == 1)
		cout << "Ambiguity" << endl;
	else {	
		cout << "Possible" << endl;
		for (int i = 1; i<num-1; i++)
			cout << a1[i] << " " ;
		cout << a1[num-1] << endl;

	}
	return 0;
	
}


0
0
查看评论

Codeforces Round #378 (Div. 2) E. Sleep in Class

链接:http://codeforces.com/contest/733/problem/E 题意:给一个只含'U'和'D'的字符串,U表示向右走,D表示向左走。每个格子踩一次之后翻转(U变D,D变U),求从每个格子出发走多少步才能走到位置小于1或大于n。 分析:...
  • Fsss_7
  • Fsss_7
  • 2016-11-15 13:48
  • 359

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include #include #include #include #include #pragma commen...
  • rowanhaoa
  • rowanhaoa
  • 2014-07-25 14:39
  • 1481

Codeforces Round #332 (Div. 2) B. Spongebob an Joke (水)

B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Pa...
  • helloiamclh
  • helloiamclh
  • 2016-01-01 19:28
  • 239

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 standard input output standard o...
  • ZZ_AC
  • ZZ_AC
  • 2015-11-21 14:12
  • 263

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

题意:若第三行的数在第二行的数中出现过,并且全部为出现1次的,则输出possible,并输出其对应的在第二行中的位置。若出现过两次,则输出Ambiguity,否则,输出Impossible.#include #include #include using namespace std; const i...
  • zyx520ytt
  • zyx520ytt
  • 2015-11-21 10:58
  • 193

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

题目链接 题意:给出一个f序列,和b序列,问是否存在a序列,使得f[a[i]]==b[i] 成立。只有一组解就输出possible,和a序列,多组解,输出Ambiguity 否则输出impossible 解答:对于impossibe我们直接看b序列的数是不是都能在f序列找到 ,对于多解,只...
  • u013167299
  • u013167299
  • 2015-11-21 10:21
  • 195

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-02-25 17:55
  • 413

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 standard input output standard output ...
  • just_sort
  • just_sort
  • 2016-09-07 22:30
  • 164

Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜,还是停留在AB题的水平,有时候CD其实很简单,但就是想不到,别人一眼看出而我就是想不到,有时候想到了点子上但就是突破不了 题目链接:  Codeforces Round #395 (Div. 2) A题: &...
  • qq_34731703
  • qq_34731703
  • 2017-02-03 12:50
  • 500

Codeforces Round #315 (Div. 1)

Codeforces Round #315 (Div. 1)
  • u013023344
  • u013023344
  • 2015-08-16 21:31
  • 1067
    个人资料
    • 访问:74912次
    • 积分:2333
    • 等级:
    • 排名:第18924名
    • 原创:162篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    博客专栏