F - Find The Bone

F - 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 hasm 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?≤?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 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.

Example

Input

7 3 43 4 61 22 55 77 1

Output

1

Input

5 1 221 22 4

Output

2
题意 第一行输入整数n,m,k。表示有n个杯子,从1到n编号,沿x轴放置在一个有m个孔的桌子上;    第二行输入m个数,表示该位置有孔;    第三行k次操作,每次输入两个数x,y。交换x,y位置所在的杯子里的东西。骨头放在1号杯子中,如果骨头所在的位置有孔,    则骨头会掉进去(骨头就移动不了了)。    输出骨头的最终位置。

题解:1.用0表示该位置没孔,1表示该位置有孔,用ans标记骨头的位置,   2.每次操作,先判断骨头的位置有没有孔,有孔,则骨头的位置确定。    3.没有孔,就判断该两个位置上有没有骨头,有骨头,就更新骨头的位置。   4.再次判断骨头的位置有没有孔,有孔,则骨头的位置确定代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstdio>
using namespace std;
int a[1000005];
int main()
{
    int i,n,m,k,x,y,t,ans=1;
    scanf("%d %d %d",&n,&m,&k);

    for(i=1;i<=n;i++)
        a[i]=0;

    for(i=0;i<m;i++)
    {
        scanf("%d",&x);
        a[x]=1;
    }

    for(i=0;i<k;i++)
    {
        scanf("%d %d",&x,&y);
        if(a[ans]==1)
            break;
        if(x==ans)
            ans=y;
        else if(y==ans)
            ans=x;
        if(a[ans]==1)
            break;
    }
    cout<<ans<<endl;
}

				
时间: 2024-10-10 05:24:08

F - Find The Bone的相关文章

Bone Collector 0-1背包问题

题目描述: Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volume of V ,and a

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

Bone Collector

Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag with a volume of

HDU 1010 Tempter of the Bone(bfs)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 97219    Accepted Submission(s): 26383 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDOJ 2602 Bone Collector【01背包】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34251    Accepted Submission(s): 14101 Problem Description Many years ago , in

杭电 2639 Bone Collector II【01背包第k优解】

解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[v-c[i]]+w[i]中的最大值,但是现在要求第k大的值,我们就分别用两个数组保留f[v]的前k个值,f[v-c[i]]+w[i]的前k个值,再将这两个数组合并,取第k名. 即f的数组会增加一维. http://blog.csdn.net/lulipeng_cpp/article/details/

Bone Collector(ZeroOnebag)

Bone Collector Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag wi

杭电2602 Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65099    Accepted Submission(s): 27122 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bon