結果

問題 No.1605 Matrix Shape
ユーザー 👑 H20H20
提出日時 2021-07-16 23:11:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,968 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 437,020 KB
最終ジャッジ日時 2023-09-20 15:38:49
合計ジャッジ時間 15,890 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 156 ms
107,128 KB
testcase_02 AC 94 ms
71,492 KB
testcase_03 WA -
testcase_04 AC 160 ms
107,248 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 94 ms
71,516 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 96 ms
71,368 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 97 ms
71,520 KB
testcase_16 AC 96 ms
71,652 KB
testcase_17 AC 96 ms
71,424 KB
testcase_18 AC 94 ms
71,512 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 922 ms
306,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 302 ms
117,444 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 619 ms
153,704 KB
testcase_34 WA -
testcase_35 AC 311 ms
138,688 KB
testcase_36 AC 236 ms
115,076 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)
    if len(ans)+1==2*10**5+10:
        print(N)
    else:
        print(0)
else:
    temp1 = CH - CW
    temp2 = CW - CH
    if len(temp1) == len(temp2) == 1 and temp1.most_common()[0][1]==temp2.most_common()[0][1]==1:
        temp1 = temp1.most_common()[0][0]
        temp2 = temp2.most_common()[0][0]
        edges.append((temp2,temp1))
        ans=scc(2*10**5+10,edges)
        if len(ans)-2==2*10**5+10:
            print(1)
            exit()
    print(0)
0