結果

問題 No.1266 7 Colors
ユーザー semisagisemisagi
提出日時 2020-10-23 22:24:55
言語 Swift
(6.0.3)
結果
AC  
実行時間 723 ms / 3,000 ms
コード長 2,856 bytes
コンパイル時間 13,151 ms
コンパイル使用メモリ 132,244 KB
実行使用メモリ 27,776 KB
最終ジャッジ日時 2024-07-21 11:06:57
合計ジャッジ時間 14,302 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        let value = peek()
        index += 1
        return value
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

struct UnionFind {
    var parent: [Int]
    var count: [Int]

    init(_ n: Int) {
        parent = Array(0 ..< n)
        count = [Int](repeating: 1, count: n)
    }

    mutating func find(_ v: Int) -> Int {
        if v == parent[v] {
            return v
        }
        parent[v] = find(parent[v])
        return parent[v]
    }

    mutating func union(_ u: Int, _ v: Int) {
        var u = find(u)
        var v = find(v)
        guard u != v else { return }
        if count[u] < count[v] {
            swap(&u, &v)
        }
        count[u] += count[v]
        parent[v] = u
    }

    mutating func count(_ u: Int) -> Int {
        count[find(u)]
    }
}

struct Array2D<Element> {
    let n1: Int
    let n2: Int
    private var elements: [Element]

    init(repeating: Element, _ n1: Int, _ n2: Int) {
        self.n1 = n1
        self.n2 = n2
        self.elements = [Element](repeating: repeating, count: n1 * n2)
    }

    subscript(i: Int, j: Int) -> Element {
        get {
            elements[i * n2 + j]
        }
        set {
            elements[i * n2 + j] = newValue
        }
    }
}

var scanner = Scanner()
let N = scanner.nextInt()
let M = scanner.nextInt()
let Q = scanner.nextInt()
var colors = Array2D(repeating: false, N, 7)
for i in 0 ..< N {
    let s = [Character](scanner.next())
    for j in 0 ..< 7 {
        colors[i, j] = s[j] == "1"
    }
}

var G = [[Int]](repeating: [Int](), count: N)
for _ in 0 ..< M {
    let u = scanner.nextInt() - 1
    let v = scanner.nextInt() - 1
    G[u].append(v)
    G[v].append(u)
}

var uf = UnionFind(N * 7)

func union(u: Int) {
    for i in 0 ..< 7 {
        let j = (i + 1) % 7
        if colors[u, i] && colors[u, j] {
            uf.union(u * 7 + i, u * 7 + j)
        }
    }
    for v in G[u] {
        for i in 0 ..< 7 {
            if colors[u, i] && colors[v, i] {
                uf.union(u * 7 + i, v * 7 + i)
            }
        }
    }
}

for u in 0 ..< N {
    union(u: u)
}

for _ in 0 ..< Q {
    switch scanner.nextInt() {
    case 1:
        let x = scanner.nextInt() - 1
        let y = scanner.nextInt() - 1
        colors[x, y] = true
        union(u: x)

    default:
        let x = scanner.nextInt() - 1
        let _ = scanner.nextInt()
        print(uf.count(x * 7))
    }
}

0