D - Find The Bone(模拟交换)

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, cupi 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 cupsk times, the i-th time of which involves the cups at the positionsx = ui andx = 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 atx = 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 n,m, and k (2 ≤ n ≤ 106,1 ≤ m ≤ n, 1 ≤ 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 integersui andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — the positions of the cups to be swapped.

Output

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

Example

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

Note

In the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.

In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground.

 

这题模拟交换听起来很厉害的样子,实际上就是个a,b交换的过程

代码如下

 

 

#include<stdio.h>
int main()
{
    int a[1000005]={0,1},b[1000005]={0};//a    bone     b     hole
    int n,m,k;//sun,,hole,,,time
    int i,j,temp;//temp->>b[m]
    scanf("%d%d%d",&n,&m,&k);
    for(i=0;i<m;i++)
    {
        scanf("%d",&temp);
        b[temp]=1;
    }
    int x,y;
    for(i=0;i<k;i++)
    {
        scanf("%d%d",&x,&y);

        if(!a[x]&&!a[y])//x,y  都没有骨头时 不用交换,交换也没影响
            continue;

        if((a[x]  &&  b[x] )  || ( a[y]  &&  b[y] ))//当x或y 有骨头并且骨头已经掉到坑中 不再交换
            break;//是可以直接跳出的,不影响
        temp=a[x];
        a[x]=a[y];//交换,没啥好说的
        a[y]=temp;
    }

    for(i=1;i<=n;i++)//这里要取等号
    {
        if(a[i])
        {
            printf("%d\n",i);
            break;
        }
    }

return 0;
}





 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值