結果

問題 No.1605 Matrix Shape
ユーザー 👑 H20H20
提出日時 2021-07-16 23:23:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,856 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 86,832 KB
実行使用メモリ 433,964 KB
最終ジャッジ日時 2023-09-20 15:55:12
合計ジャッジ時間 18,146 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
106,892 KB
testcase_01 AC 147 ms
107,152 KB
testcase_02 AC 145 ms
107,040 KB
testcase_03 WA -
testcase_04 AC 145 ms
106,932 KB
testcase_05 AC 145 ms
106,948 KB
testcase_06 WA -
testcase_07 AC 144 ms
106,992 KB
testcase_08 WA -
testcase_09 AC 147 ms
106,848 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 149 ms
107,016 KB
testcase_14 WA -
testcase_15 AC 147 ms
106,836 KB
testcase_16 AC 147 ms
107,020 KB
testcase_17 AC 149 ms
107,120 KB
testcase_18 WA -
testcase_19 AC 1,597 ms
433,964 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 938 ms
306,992 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 298 ms
117,004 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 605 ms
153,236 KB
testcase_34 WA -
testcase_35 AC 722 ms
255,708 KB
testcase_36 AC 676 ms
255,100 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)==2*10**5+10-len(CH)+1:
        print(N)
    else:
        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)
    if len(ans)==2*10**5+10-len(CH):
        print(1)
        exit()
    print(0)
0