796B.md (2761B)
1 Source: https://codeforces.com/problemset/problem/796/B 2 3 B. Find The Bone 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Zane the wizard is going to perform a magic show shuffling the cups. 22 23 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*. 24 25 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* = *u**i* and *x* = *v**i*. 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. 26 27 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. 28 29  30 31 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. 32 33 Input 34 35 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. 36 37 The second line contains *m* distinct integers *h*1, *h*2, ..., *h**m* (1 ≤ *h**i* ≤ *n*) — the positions along the *x*-axis where there is a hole on the table. 38 39 Each of the next *k* lines contains two integers *u**i* and *v**i* (1 ≤ *u**i*, *v**i* ≤ *n*, *u**i* ≠ *v**i*) — the positions of the cups to be swapped. 40 41 Output 42 43 Print one integer — the final position along the *x*-axis of the bone. 44 45 Examples 46 47 Input 48 49 ``` 50 7 3 4 51 3 4 6 52 1 2 53 2 5 54 5 7 55 7 1 56 ``` 57 58 Output 59 60 ``` 61 1 62 ``` 63 64 Input 65 66 ``` 67 5 1 2 68 2 69 1 2 70 2 4 71 ``` 72 73 Output 74 75 ``` 76 2 77 ``` 78 79 Note 80 81 In the first sample, after the operations, the bone becomes at *x* = 2, *x* = 5, *x* = 7, and *x* = 1, respectively. 82 83 In the second sample, after the first operation, the bone becomes at *x* = 2, and falls into the hole onto the ground.