結果

問題 No.1955 Not Prime
ユーザー tktk_snsntktk_snsn
提出日時 2022-05-23 17:27:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,545 ms / 2,000 ms
コード長 4,801 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 81,904 KB
実行使用メモリ 296,712 KB
最終ジャッジ日時 2023-10-20 17:37:50
合計ジャッジ時間 6,763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
85,824 KB
testcase_01 AC 82 ms
85,824 KB
testcase_02 AC 79 ms
85,820 KB
testcase_03 AC 81 ms
85,820 KB
testcase_04 AC 79 ms
85,824 KB
testcase_05 AC 76 ms
85,820 KB
testcase_06 AC 102 ms
86,684 KB
testcase_07 AC 189 ms
94,260 KB
testcase_08 AC 171 ms
92,116 KB
testcase_09 AC 225 ms
92,612 KB
testcase_10 AC 169 ms
86,404 KB
testcase_11 AC 1,545 ms
296,712 KB
testcase_12 AC 235 ms
87,176 KB
testcase_13 AC 262 ms
94,832 KB
testcase_14 AC 130 ms
87,208 KB
testcase_15 AC 134 ms
86,752 KB
testcase_16 AC 166 ms
93,084 KB
testcase_17 AC 219 ms
106,372 KB
testcase_18 AC 226 ms
95,852 KB
testcase_19 AC 242 ms
100,808 KB
testcase_20 AC 83 ms
85,844 KB
testcase_21 AC 144 ms
87,588 KB
testcase_22 AC 84 ms
85,768 KB
testcase_23 AC 84 ms
85,772 KB
testcase_24 AC 83 ms
85,768 KB
testcase_25 AC 82 ms
85,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
U = 10 ** 6 + 100


class SCC_graph(object):
    def __init__(self, n):
        """n:ノード数"""
        self.n = n
        self.edges = []

    def add_edge(self, frm, to):
        """frm -> toへ有効辺を張る"""
        self.edges.append((frm, to))

    def __csr(self):
        self.start = [0] * (self.n + 1)
        self.elist = [0] * len(self.edges)
        for frm, to in self.edges:
            self.start[frm + 1] += 1
        for i in range(1, self.n + 1):
            self.start[i] += self.start[i - 1]
        cnt = self.start[:]
        for frm, to in self.edges:
            self.elist[cnt[frm]] = to
            cnt[frm] += 1

    def __dfs(self, v):
        self.low[v] = self.now_ord
        self.order[v] = self.now_ord
        self.now_ord += 1
        self.visited.append(v)
        for i in range(self.start[v], self.start[v + 1]):
            to = self.elist[i]
            if self.order[to] == -1:
                self.__dfs(to)
                self.low[v] = min(self.low[v], self.low[to])
            else:
                self.low[v] = min(self.low[v], self.order[to])
        if self.low[v] == self.order[v]:
            while self.visited:
                u = self.visited.pop()
                self.order[u] = self.n
                self.ids[u] = self.group_num
                if u == v:
                    break
            self.group_num += 1

    def _make_scc_ids(self):
        self.__csr()
        self.now_ord = 0
        self.group_num = 0
        self.visited = []
        self.low = [0] * self.n
        self.ids = [0] * self.n
        self.order = [-1] * self.n
        for i in range(self.n):
            if self.order[i] == -1:
                self.__dfs(i)
        for i in range(self.n):
            self.ids[i] = self.group_num - 1 - self.ids[i]

    def scc(self):
        """
        強連結成分分解O(N+M), groupsを返す
        self.ids[i] -> 頂点iがトポロジカル順で何番目の成分に属するか
        groups[j] -> トポロジカル順でj番目の強連結成分に属する頂点集合
        """
        self._make_scc_ids()
        groups = [[] for _ in range(self.group_num)]
        for i in range(self.n):
            groups[self.ids[i]].append(i)
        return groups

    def make_condensation_graph(self):
        """強連結成分間の隣接リスト、入次数/出次数のリストを返す"""
        n = self.n
        G = [[] for _ in range(self.group_num)]
        indeg = [0] * self.group_num
        outdeg = [0] * self.group_num
        used = set()
        for s, t in self.edges:
            s = self.ids[s]
            t = self.ids[t]
            if s == t:
                continue
            if s * n + t in used:
                continue
            # G[s].append(t)
            G[t].append(s)
            indeg[t] += 1
            outdeg[s] += 1
            used.add(s * n + t)
        return G, indeg, outdeg


class TwoSAT(SCC_graph):
    def __init__(self, n):
        """ n: ノード数"""
        self._n = n
        super().__init__(2 * n)

    def add_clause(self, i, f, j, g):
        """ (xi == f) or (xj == g)というクローズを追加 """
        x = 2 * i + (0 if f else 1)
        y = 2 * j + (1 if g else 0)
        self.add_edge(x, y)
        x = 2 * j + (0 if g else 1)
        y = 2 * i + (1 if f else 0)
        self.add_edge(x, y)

    def satisfiable(self):
        """ 条件を満たす割り当てが存在するか判定する """
        self._make_scc_ids()
        self._answer = [False] * self._n
        for i in range(self._n):
            if self.ids[2 * i] == self.ids[2 * i + 1]:
                return False
            self._answer[i] = (self.ids[2 * i] < self.ids[2 * i + 1])
        return True

    def answer(self):
        """ 最後に読んだsatisfiableのクローズを満たす割り当てを返す """
        return self._answer


def merge(a, b):
    x = b
    while x:
        a *= 10
        x //= 10
    return a + b


def main(N, AB):
    sieve = [0] * (U + 1)
    prime = []
    for i in range(2, U + 1):
        if sieve[i] == 0:
            sieve[i] = i
            prime.append(i)
        for p in prime:
            if p > sieve[i] or i * p > U:
                break
            sieve[i * p] = p

    ts = TwoSAT(N)
    for i in range(N):
        for j in range(N):
            for s in range(2):
                for t in range(2):
                    val = merge(AB[i][s], AB[j][t])
                    if sieve[val] == val:
                        ts.add_clause(i, s, j, t ^ 1)
    return ts.satisfiable()


N = int(input())
AB = tuple(tuple(map(int, input().split())) for _ in range(N))
if main(N, AB):
    print("Yes")
else:
    print("No")
0