結果

問題 No.1605 Matrix Shape
ユーザー 👑 H20H20
提出日時 2021-07-16 23:34:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,919 bytes
コンパイル時間 547 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 434,716 KB
最終ジャッジ日時 2023-09-20 16:06:38
合計ジャッジ時間 18,738 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 156 ms
107,276 KB
testcase_01 AC 157 ms
107,332 KB
testcase_02 AC 155 ms
107,184 KB
testcase_03 AC 152 ms
107,124 KB
testcase_04 AC 154 ms
107,572 KB
testcase_05 AC 153 ms
107,212 KB
testcase_06 AC 153 ms
107,136 KB
testcase_07 AC 156 ms
107,184 KB
testcase_08 AC 156 ms
107,328 KB
testcase_09 AC 156 ms
106,960 KB
testcase_10 AC 155 ms
107,080 KB
testcase_11 WA -
testcase_12 AC 155 ms
107,448 KB
testcase_13 AC 155 ms
107,264 KB
testcase_14 AC 154 ms
107,148 KB
testcase_15 WA -
testcase_16 AC 155 ms
107,136 KB
testcase_17 AC 154 ms
107,428 KB
testcase_18 WA -
testcase_19 AC 1,570 ms
434,716 KB
testcase_20 AC 1,129 ms
234,024 KB
testcase_21 AC 955 ms
254,780 KB
testcase_22 AC 954 ms
305,832 KB
testcase_23 AC 1,298 ms
401,128 KB
testcase_24 AC 1,386 ms
404,996 KB
testcase_25 AC 308 ms
118,080 KB
testcase_26 AC 308 ms
115,692 KB
testcase_27 AC 304 ms
116,816 KB
testcase_28 AC 306 ms
117,600 KB
testcase_29 AC 316 ms
117,548 KB
testcase_30 AC 489 ms
136,116 KB
testcase_31 AC 567 ms
153,960 KB
testcase_32 AC 621 ms
157,136 KB
testcase_33 AC 654 ms
162,340 KB
testcase_34 AC 308 ms
114,680 KB
testcase_35 AC 809 ms
278,232 KB
testcase_36 AC 710 ms
264,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import collections
sys.setrecursionlimit(10**6)
def scc(N,edges):
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    for e in edges:
        start[e[0]+1]+=1
    for i in range(1,N+1):
        start[i]+=start[i-1]
    counter=start[:]
    for e in edges:
        elist[counter[e[0]]]=e[1]
        counter[e[0]]+=1
    NG=[0,0]
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    def dfs(v):
        low[v]=NG[0]
        Ord[v]=NG[0]
        NG[0]+=1
        visited.append(v)
        for i in range(start[v],start[v+1]):
            to=elist[i]
            if Ord[to]==-1:
                dfs(to)
                low[v]=min(low[v],low[to])
            else:
                low[v]=min(low[v],Ord[to])
        if low[v]==Ord[v]:
            while(True):
                u=visited.pop()
                Ord[u]=N
                ids[u]=NG[1]
                if u==v:
                    break
            NG[1]+=1
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    group_num=NG[1]
    counts=[0]*group_num
    for x in ids:
        counts[x]+=1
    groups=[[] for i in range(group_num)]
    for i in range(N):
        groups[ids[i]].append(i)
    return groups


N = int(input())
H = []
W = []
edges=[]
for _ in range(N):
    h,w = map(int, input().split())
    H.append(h)
    W.append(w)
    edges.append((h,w))

CH = collections.Counter(H)
CW = collections.Counter(W)
if CH==CW:
    ans=scc(2*10**5+10,edges)
    le = len(CH)
    for a in ans:
        if len(a)==le:
            print(le)
            exit()
    print(0)
else:
    temp1 = CH - CW
    temp2 = CW - CH
    temp1 = temp1.most_common()[0][0]
    temp2 = temp2.most_common()[0][0]
    edges.append((temp2,temp1))
    ans=scc(2*10**5+10,edges)
    le = len(CH + CW)
    for a in ans:
        if len(a)==le:
            print(1)
            exit()
    print(0)
0