Codeforces Round #408 (Div. 2) B.Find The Bone(模拟)

来源:互联网 发布:itunes软件下载 编辑:程序博客网 时间:2024/06/06 02:05
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 43 4 61 22 55 77 1
output
1
input
5 1 221 22 4
output
2
Note

In the first sample, after the operations, the bone becomes at x = 2x = 5x = 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.

题目大意:有n个位置,m次操作,每次操作方式是把将两个位置的杯子互换,同时在k个位置上有一个洞,该位置上的杯子里面若有骨头,则骨头会从杯子中掉下来,初始骨头的位置在1,问m次操作后骨头会在哪个位置
解题思路:这道题其实坑蛮多的,首先是1本身可能是有洞的,一开始的操作不一定从1开始,有些操作是无用功,有时候虽然换了位置,但是会在后面可能再换回来,基于以上,我WA了很多发,本来想用搜索去做,发现不可以解决换了位置又换回来的问题,所以又换了一种方法,是个特别巧的办法
#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;int main(){bool flag, a[1000005];int n, m, k, i, x, ans, y;cin >> n >> m >> k;memset(a, false, sizeof(a));for (i = 1; i <= m; i++){cin >> x;a[x] = true;}ans = 1;flag = false;for (i = 1; i <= k; i++){cin >> x >> y;if (a[1] == true){ans = 1;flag = true;}if ((ans == x || ans == y) && flag != true){ans = x + y - ans;}if (a[ans] == true && flag != true){flag = true;}}cout << ans << endl;}


0 0
原创粉丝点击