結果

問題 No.1023 Cyclic Tour
ユーザー vwxyzvwxyz
提出日時 2024-07-17 04:24:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,726 ms / 2,000 ms
コード長 5,961 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 13,312 KB
実行使用メモリ 88,712 KB
最終ジャッジ日時 2024-07-17 04:26:04
合計ジャッジ時間 72,315 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,264 KB
testcase_01 AC 32 ms
11,392 KB
testcase_02 AC 32 ms
11,392 KB
testcase_03 AC 32 ms
11,392 KB
testcase_04 AC 942 ms
52,648 KB
testcase_05 AC 926 ms
52,712 KB
testcase_06 AC 1,049 ms
55,532 KB
testcase_07 AC 966 ms
53,960 KB
testcase_08 AC 968 ms
65,332 KB
testcase_09 AC 951 ms
63,876 KB
testcase_10 AC 913 ms
63,792 KB
testcase_11 AC 1,017 ms
66,916 KB
testcase_12 AC 961 ms
70,020 KB
testcase_13 AC 896 ms
66,548 KB
testcase_14 AC 900 ms
66,240 KB
testcase_15 AC 943 ms
67,748 KB
testcase_16 AC 1,524 ms
82,296 KB
testcase_17 AC 1,584 ms
83,224 KB
testcase_18 AC 1,580 ms
81,332 KB
testcase_19 AC 1,520 ms
79,584 KB
testcase_20 AC 1,524 ms
76,388 KB
testcase_21 AC 1,569 ms
77,396 KB
testcase_22 AC 1,620 ms
79,764 KB
testcase_23 AC 1,716 ms
81,804 KB
testcase_24 AC 1,673 ms
88,712 KB
testcase_25 AC 1,529 ms
76,324 KB
testcase_26 AC 1,539 ms
76,700 KB
testcase_27 AC 1,464 ms
74,536 KB
testcase_28 AC 1,627 ms
86,576 KB
testcase_29 AC 1,560 ms
87,604 KB
testcase_30 AC 1,726 ms
87,556 KB
testcase_31 AC 1,598 ms
82,684 KB
testcase_32 AC 1,585 ms
80,484 KB
testcase_33 AC 1,689 ms
85,508 KB
testcase_34 AC 1,555 ms
76,308 KB
testcase_35 AC 1,569 ms
77,224 KB
testcase_36 AC 1,643 ms
77,784 KB
testcase_37 AC 1,581 ms
79,384 KB
testcase_38 AC 1,534 ms
83,040 KB
testcase_39 AC 1,623 ms
81,528 KB
testcase_40 AC 1,609 ms
82,372 KB
testcase_41 AC 1,623 ms
82,108 KB
testcase_42 AC 1,568 ms
77,688 KB
testcase_43 AC 1,561 ms
84,724 KB
testcase_44 AC 908 ms
53,492 KB
testcase_45 AC 1,043 ms
72,672 KB
testcase_46 AC 844 ms
56,960 KB
testcase_47 AC 903 ms
54,572 KB
testcase_48 AC 1,594 ms
79,380 KB
testcase_49 AC 1,606 ms
78,232 KB
testcase_50 AC 1,551 ms
78,232 KB
testcase_51 AC 1,620 ms
79,512 KB
testcase_52 AC 1,583 ms
78,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=0
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)            
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

def SCC(N,edges):
    start = [0] * (N + 1)
    elist = [0] * len(edges)
    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
    N = N
    now_ord = group_num = 0
    visited = []
    low = [0] * N
    order = [-1] * N
    ids = [0] * N
    parent = [-1] * N
    stack = []
    for i in range(N):
        if order[i] == -1:
            stack.append(i)
            stack.append(i)
            while stack:
                v = stack.pop()
                if order[v] == -1:
                    low[v] = order[v] = now_ord
                    now_ord += 1
                    visited.append(v)
                    for i in range(start[v], start[v + 1]):
                        to = elist[i]
                        if order[to] == -1:
                            stack.append(to)
                            stack.append(to)
                            parent[to] = v
                        else:
                            low[v] = min(low[v], order[to])
                else:
                    if low[v] == order[v]:
                        while True:
                            u = visited.pop()
                            order[u] = N
                            ids[u] = group_num
                            if u == v:
                                break
                        group_num += 1
                    if parent[v] != -1:
                        low[parent[v]] = min(low[parent[v]], low[v])
    for i, x in enumerate(ids):
        ids[i] = group_num - 1 - x
    groups = [[] for _ in range(group_num)]
    for i, x in enumerate(ids):
        groups[x].append(i)
    return groups

N,M=map(int,input().split())
AB=[[],[]]
for m in range(M):
    a,b,c=map(int,input().split())
    a-=1;b-=1;c-=1
    AB[c].append((a,b))
UF=UnionFind(N)
ans="No"
for a,b in AB[0]:
    if UF.Same(a,b):
        ans="Yes"
    UF.Union(a,b)
edges=[]
for a,b in AB[1]:
    a=UF.Find(a)
    b=UF.Find(b)
    if a==b:
        ans="Yes"
    edges.append((a,b))
scc=SCC(N,edges=edges)
for lc in scc:
    if len(lc)>=2:
        ans="Yes"
print(ans)
0