zoukankan      html  css  js  c++  java
  • CodeForces

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

    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.

    题解:模拟来回交换的过程即可

    代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    int vis[2000005];
    int main() {
    	int n,m,k;
    	cin>>n>>m>>k;
    	int s;
    	for(int t=0; t<m; t++) {
    		scanf("%d",&s);
    		vis[s]=1;
    	}
    	int a,b,temp=1;
    	int cnt,flag=0;
    
    	for(int t=0; t<k; t++) {
    		scanf("%d%d",&a,&b);
    
    		if(a==temp&&vis[temp]==0) {
    			temp=b;
    		} else if(b==temp&&vis[temp]==0) {
    			temp=a;
    		}
    
    	}
    
    	cout<<temp<<endl;
    
    	return 0;
    }

  • 相关阅读:
    用小程序&#183;云开发两天搭建mini论坛丨实战
    巧用小程序&#183;云开发实现邮件发送功能丨实战
    借助云开发,10行代码实现小程序支付功能!丨实战
    你的心事我全知晓——心情日记小程序丨实战
    只需20小时,让0基础的你掌握小程序云开发!这个暑假,约否?
    诗词歌赋,样样精通!诗词古语小程序带你领略魅力古风丨实战
    在线教育项目-day02【后台讲师管理模块】
    在线教育项目-day02【搭建项目结构】
    在线教育项目-day01【wrapper条件构造器】
    在线教育项目-day01【性能分析插件】
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781900.html
Copyright © 2011-2022 走看看