Find The Bone (Codeforces-796B)

题目链接:

http://codeforces.com/problemset/problem/796/B

B. Find The Bone
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Zane the wizard is going to perform a magic show shuffling the cups.

There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.

The problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = ui and x = vi. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.

Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation.

Zane’s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.

Input

The first line contains three integers nm, and k (2 ≤ n ≤ 1061 ≤ m ≤ n1 ≤ k ≤ 3·105) — the number of cups, the number of holes on the table, and the number of swapping operations, respectively.

The second line contains m distinct integers h1, h2, ..., hm (1 ≤ hi ≤ n) — the positions along the x-axis where there is a hole on the table.

Each of the next k lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the positions of the cups to be swapped.

Output

Print one integer — the final position along the x-axis of the bone.

Examples
input
7 3 4
3 4 6
1 2
2 5
5 7
7 1
output
1
input
5 1 2
2
1 2
2 4
output
2

题目大意:

有n个碗,分别放在桌子上的 编号为1-n的位置, 骨头 最初在第一个碗下面,但是其中有m个碗下面的桌子有洞,也就是说如果 带骨头 的碗换到这个位置,碗下面的骨头将掉下去。题目要求计算出经过 k 次交换(任意两个碗进行交换)后,骨头最终停留在几号位置?第一行输入n m k,接下输入m个整数,表示有洞的位置,接下来k行表示进行k次交换。

解题思路:

这道题我用c++写的,但是用GNU G++ 5.1.0提交时间超限,而用Microsoft Visual C++ 2010提交正确。这一点我不明白为什么。说一下我的思路吧:用ans储存骨头的位置(显然最初ans=1),用map标记有洞的位置,用结构体储存每一组交换的两个位置。然后从结构体数组的开头开始遍历,如果a[i].x==ans,那么ans=a[i].y;如果a[i].y==ans,那么ans=a[i].x;同时还得考虑有洞的情况。

代码:

#include<iostream>
#include<map>
using namespace std;
struct qq
{
    long long int x;
    long long int y;
}a[300020];
int main()
{
    long long int n,m,k;
    while(cin>>n>>m>>k)
    {
        long long int s;
        map<int,int>M;
        for(int i=0;i<=m-1;i++)
        {
            cin>>s;
            M[s]++;   //有洞的位置标记为1
        }
        long long int ans=1;
        for(int i=0;i<=k-1;i++)
            cin>>a[i].x>>a[i].y;
        for(int i=0;i<=k-1;i++)
        {
            if(M[ans]==1)   //骨头以掉到编号为ans的洞里,直接输出ans即可,不需要再循环了
                break;
            if(a[i].x==ans)   //交换
                ans=a[i].y;
            else if(a[i].y==ans)   //交换
                ans=a[i].x;
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值