CF div2 332 B
2015-11-21 23:39
260 查看
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
output
input
output
input
output
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]是否有值。值大于1则是多解情况,等于0则是无解。
View Code
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.
另开一个数组做一个映射,然后判断该数组里面b[i]是否有值。值大于1则是多解情况,等于0则是无解。
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <queue> #include <map> #include <algorithm> #include <vector> #include <set> using namespace std; const int maxn = 1000005; typedef long long LL; vector<int>G[maxn]; int f[maxn],a[maxn]; int b[maxn],bb[maxn]; int cnt[maxn]; int ans[maxn]; int main() { int n,m; scanf("%d%d",&n,&m); bool ambiguity=false; int i; for(i=0;i<n;i++){ scanf("%d",&f[i]); a[f[i]]=i+1; cnt[f[i]]++; } for(i=0;i<m;i++){ scanf("%d",&b[i]); if(cnt[b[i]]==0){ printf("Impossible"); return 0; } else if(cnt[b[i]]>1){ ambiguity=true; } else if(cnt[b[i]]==1){ ans[i]=a[b[i]]; } } if(ambiguity==true){ printf("Ambiguity"); return 0; } printf("Possible\n"); for(i=0;i<m;i++){ printf("%d ",ans[i]); } return 0; }
View Code
相关文章推荐
- 异常处理机制
- Android 中的adapter和作用以及常见的adapter
- opencv3中FLANN结合SURF进行关键点的描述和匹配
- 「喵神成长史」『四大元首』Activity(活动)
- ECMAScript——引用数据类型之操作语句
- Spring+Log4j+ActiveMQ实现远程记录日志——实战+分析
- 010--VS2013 C++ 平面地图贴图
- Android Activity转场动画
- 正则表达式在iOS中的运用
- 练习--LINUX进程间通信之信号SIGNAL
- java switch语句详解
- cmake脚本编写
- ckfinder2.5 破解
- 【离散数学实验】Dijkstra算法求指定两点间的距离
- 程序的性能优化之代码上的细节优化
- 判断java虚拟机内存里的对象已死两种方式
- ECMAScript——引用数据类型之function关于call和apply
- Longest Substring Without Repeat Characters
- Office 365 Service Communications API reference
- java switch