• 796B Find The Bone


    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 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 integers ui and vi (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.

    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
    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.

     题意:给你n个杯子标号为(1~n),有m个杯子有洞,有k次操作,

              每次操作交换编号为u和编号为v的杯子,如果这个杯子 有洞,骨头就会掉下去,

              初始骨头在位置1,现在问你k此操作以后骨头在哪里

     解析:标记洞编号,每次交换前判断骨头是否掉入洞中,

             如果交换的两个杯子中没有骨头,骨头位置没有变,不用处理

             如果交换的两个杯子中有骨头,在 ui 中,并且骨头未掉入洞中, 骨头位置变化到 vi 中

                                                    在 vi 中,并且骨头未掉入洞中, 骨头位置变换到 ui 中

            重复 K 次操作,最后找到 骨头的位置 

    另:用cin cout 可能会超时,较大数据用scanf(); printf();

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int M = 1e6 +10;
     5 int holes[M];
     6 int main(){
     7     int n, m, k;
     8     scanf("%d%d%d",&n,&m,&k);
     9     //cin >> n >> m >> k;
    10     int stone = 1;
    11     for(int i = 0; i < m; i++){
    12         int hol;
    13         //cin >. hol;
    14         scanf("%d",&hol);
    15         holes[hol] ++;
    16     }
    17     for(int i = 0; i < k; i++){
    18         int ui, vi;
    19         //cin >> ui >> vi;
    20         scanf("%d%d",&ui,&vi);
    21         if(ui == stone && holes[ui] != 1){
    22             stone = vi;
    23         }
    24         else if(vi == stone && holes[vi] != 1){
    25             stone = ui;
    26         }
    27     }
    28     //cout << stone << endl;
    29     printf("%d",stone);
    30 }

                    

  • 相关阅读:
    10 个超棒的 JavaScript 简写技巧
    不掌握这些坑,你敢用BigDecimal吗?
    Java 8之Lambda表达式的写法套路
    设计好接口的36个锦囊
    喜提JDK的BUG一枚!多线程的情况下请谨慎使用这个类的stream遍历。
    Java 设计模式
    浅谈 ThreadLocal 的实际运用
    JavaScript设计模式es6(23种)
    JAVA中简单的for循环竟有这么多坑,但愿你没踩过
    2.24 Go之信息管理系统
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6696782.html
Copyright © 2020-2023  润新知