๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] Swift 20361 ์ผ์šฐ๋Š” ์•ผ๋ฐ”์œ„๊พผ implementation

by Jouureee 2022. 4. 12.

๋ฌธ์ œ :

https://www.acmicpc.net/problem/20361

 

20361๋ฒˆ: ์ผ์šฐ๋Š” ์•ผ๋ฐ”์œ„๊พผ

์ฒซ์งธ ์ค„์— N (3 ≤ N ≤ 200,000), X (1 ≤ X ≤ N), K (1 ≤ K ≤ 100,000)๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ K๊ฐœ์˜ ์ค„์—๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋ฐ”๊พผ ๋‘ ์ปต์˜ ์œ„์น˜ Ai, Bi (1 ≤ Ai, Bi ≤ N, Ai ≠ Bi)๊ฐ€ ๊ณต๋ฐฑ

www.acmicpc.net

 

๋ถ„์„ :

๊ตฌ์Šฌ์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ true๋กœ ํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊นŒ ํ•˜๋‹ค๊ฐ€ ์œ„์น˜๋ฅผ ๋ณ€์ˆ˜๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค„์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์•„์„œ 

๋ฐฐ์—ด์„ ์•ˆ์“ฐ๊ณ  ํ’€์—ˆ๋‹ค !

 

 

Swift ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/04/12.
//

import Foundation
var line = readLine()!.split(separator: " ").map { Int(String($0))! }
var n = line[0]
var x = line[1]
var k = line[2]
var answer = x

for _ in 0..<k {
    var line = readLine()!.split(separator: " ").map { Int(String($0))! }
    var l1 = line[0]
    var l2 = line[1]
    if(l1 == answer){
        answer = l2
    }else if(l2 == answer) {
        answer = l1
    }
}

print(answer)

๋Œ“๊ธ€