寒假集训第二场(A - Abandoned Animal )

Your little sister has been a big help today: she went into town to do all the groceries! During this grand voyage, she was accompanied by her fluffy friend, Mr. Fluffynose the Stuffed Animal. However, after her return, it seems that she has left him somewhere along the route! This is devastating news for your little sister, and as she won’t stop crying about it, you decide to retrace her steps through town.

You know that your sister will hold on to her beloved Fluffynose whenever possible, so the only time she could’ve lost it is when she grabbed an item on her shopping list. So, all you have to do is figure out at what store she bought what, and then you’ll reunite her with her counterpart in no time! However, you soon find out that this isn’t quite as easy as you thought: she went to a lot of stores, and although she knows the names of the stores she went to and the order in which she visited them, she does not recall what she bought at each store (it could have been nothing!). It would take a lot of time to blindly search all the stores for all these items. As you have better things to do today, like solving programming problems, you want to spend as little time on this retrieval as possible. Therefore, you want to know exactly which items your sister bought at each store before you start your search.

For this you have two pieces of information: firstly you know the inventory of all stores your sister went to. Secondly, you know exactly in what order she purchased the groceries, as she has very carefully stacked all items into her bag. You decide to number the stores your sister visited according to the order in which she visited them. Given this information, you want to decide whether you know for sure where she bought every item so you can retrace her steps as efficiently as possible.

Input

The input starts with a line with a single integer 1≤N≤100,0001≤N≤100,000, the number of supermarkets in town. Then follows a line with an integer N≤K≤100,000N≤K≤100,000, after which KK lines follow with a space-separated integer ii (between 00 and N−1N−1) and a string SS (consisting of only lowercase letters, at most 1010), denoting that item SS is available at the ithith store that your sister visited. It is guaranteed that every store has at least one item, every item is available at at least one store, and that every item occurs at most once at every store.

The second part of the input contains the list of items your sister bought, in order of purchase. It starts with a line with an integer M≤KM≤K, the number of items your sister has bought. Then follow MM lines, each with string TT, denoting the name of the item your sister bought. The items are given in the order she purchased them in. All items that your sister has bought are unique.

Output

Output “impossible" if there is no path through the stores that matches your sister’s description. Output “unique" if there is exactly one path through the stores that matches. Output “ambiguous" if there are multiple possible paths.

Sample Input 1 Sample Output 1
3
3
0 chocolate
1 icecream
2 cookies
3
chocolate
cookies
icecream
impossible
Sample Input 2 Sample Output 2
3
4
0 chocolate
1 icecream
2 cookies
2 chocolate
3
chocolate
icecream
cookies
unique
Sample Input 3 Sample Output 3
3
10
0 tomatoes
0 cucumber
1 tomatoes
2 tomatoes
2 cucumber
1 mustard
0 salt
2 salad
2 salt
2 mustard
5
tomatoes
cucumber
salad
mustard
salt
ambiguous

题意:妹妹去很多商店买东西,结果把自己最喜欢的玩具丢了,给你她买的商品的顺序和每个商店中存在的玩具,根据这个顺序求出她到商品的路径,如果不存在输出impossible,如果唯一路径输出就输出unique,否则输出ambiguous。

思路:先用map将每个超市中的商品存起来,之后就开始遍历给你的路径如果在递增方向上存在就继续判断是否路径唯一,否则输出impossible;判断路径是否唯一关键是判断路径有没有交叉路径;即在前一个超市上的商品在下一个超市上能买到(不能跨层跳跃);如果存在的话路径不唯一 

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn =1e5+10;
map<string,int>mp1[maxn];
int vis[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int w;
    scanf("%d",&w);
    int x;
    string s;
    while(w--)
    {
        cin>>x>>s;
        mp1[x][s]=1;
    }
    string a[maxn];
    int m;
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        cin>>a[i];
    }
    int k=0;
    int i=0;
    int tot=0;
    for(i=0;i<m;i++)
    {
        int flag=0;
        if(mp1[k][a[i]]&&k<n)
        {
            flag=1;
            vis[tot++]=k;
            continue;
        }
        if(!flag)
        {
            k++;
            i--;
        }
        if(k==n)
        {
            break;
        }
    }
//    cout<<tot<<endl;
    if(tot<m) cout<<"impossible"<<endl;
    else
    {
       int flag=0;
       vis[tot]=n;
       for(int i=0;i<m;i++)
       {
           for(int j=vis[i]+1;j<=vis[i+1];j++)
           {
//               cout<<mp1[j][a[i]]<<" "<<j<<" "<<i<<endl;
               if(mp1[j][a[i]]) flag=1;
           }
//           cout<<flag<<endl;
           if(flag) break;
       }
//       for(int i=0;i<tot;i++)
//       {
//           cout<<vis[i]<<" ";
//       }
//       cout<<endl;
       if(flag)cout<<"ambiguous"<<endl;
       else cout<<"unique"<<endl;
    }
    return 0;
}
发布了12 篇原创文章 · 获赞 0 · 访问量 343

猜你喜欢

转载自blog.csdn.net/Ramzes_666_fan/article/details/103963939