結果

問題 No.1668 Grayscale
ユーザー mkawa2mkawa2
提出日時 2023-12-26 01:20:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,978 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 345,216 KB
最終ジャッジ日時 2023-12-26 01:20:42
合計ジャッジ時間 8,975 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 43 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 39 ms
53,460 KB
testcase_04 WA -
testcase_05 AC 38 ms
53,460 KB
testcase_06 WA -
testcase_07 AC 947 ms
342,372 KB
testcase_08 AC 774 ms
257,276 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 39 ms
53,460 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 39 ms
53,460 KB
testcase_23 AC 41 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, n):
        self.table = [-1]*n
        self.mem = [[u] for u in range(n)]
        self.cnt = n

    def root(self, u):
        stack = []
        while self.table[u] >= 0:
            stack.append(u)
            u = self.table[u]
        for v in stack: self.table[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u = self.root(u)
        v = self.root(v)
        if u == v: return False
        su = -self.table[u]
        sv = -self.table[v]
        if su < sv: u, v = v, u
        self.table[u] = -su-sv
        self.table[v] = u
        self.mem[u] += self.mem[v]
        self.mem[v].clear()
        self.cnt -= 1
        return True

    def member(self, u):
        return self.mem[self.root(u)]

    # グループの要素数
    def size(self, u):
        return -self.table[self.root(u)]

h, w, n = LI()
cc = LLI(h)

uf = UnionFind(h*w)
for i in range(h):
    for j in range(w):
        ij = i*w+j
        if i and cc[i][j] == cc[i-1][j]: uf.merge(ij, ij-w)
        if j and cc[i][j] == cc[i][j-1]: uf.merge(ij, ij-1)

m = 0
ij2u = [0]*h*w
for ijs in uf.mem:
    if not ijs: continue
    for ij in ijs: ij2u[ij] = m
    m += 1
# print(ij2u)

to = [[] for _ in range(m)]
indeg = [0]*m
for i in range(h):
    for j in range(w):
        ij = i*w+j
        u = ij2u[ij]
        if i:
            v = ij2u[ij-w]
            if cc[i][j] < cc[i-1][j]: to[u].append(v);indeg[v] += 1
            if cc[i][j] > cc[i-1][j]: to[v].append(u);indeg[u] += 1
        if j:
            v = ij2u[ij-1]
            if cc[i][j] < cc[i][j-1]: to[u].append(v);indeg[v] += 1
            if cc[i][j] > cc[i][j-1]: to[v].append(u);indeg[u] += 1

def topological_sort(to, indeg):
    stack = [u for u, d in enumerate(indeg) if d == 0]
    deg = indeg[:]
    uu = []
    while stack:
        u = stack.pop()
        uu.append(u)
        for v in to[u]:
            deg[v] -= 1
            if deg[v] == 0: stack.append(v)
    if len(uu) != len(deg): return []
    return uu

uu = topological_sort(to, indeg)
nc = [0]*m
for u in uu:
    for v in to[u]:
        nc[v] = max(nc[v], nc[u]+1)
print(max(nc)+1)
0