結果

問題 No.583 鉄道同好会
ユーザー 👑 KazunKazun
提出日時 2021-03-02 19:05:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 2,705 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 82,588 KB
実行使用メモリ 77,492 KB
最終ジャッジ日時 2024-04-14 04:48:12
合計ジャッジ時間 3,100 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,012 KB
testcase_01 AC 45 ms
53,504 KB
testcase_02 AC 42 ms
52,828 KB
testcase_03 AC 42 ms
52,620 KB
testcase_04 AC 46 ms
54,320 KB
testcase_05 AC 43 ms
52,860 KB
testcase_06 AC 43 ms
53,244 KB
testcase_07 AC 42 ms
52,820 KB
testcase_08 AC 42 ms
53,472 KB
testcase_09 AC 43 ms
52,712 KB
testcase_10 AC 48 ms
55,828 KB
testcase_11 AC 120 ms
77,480 KB
testcase_12 AC 125 ms
76,616 KB
testcase_13 AC 126 ms
76,584 KB
testcase_14 AC 135 ms
76,484 KB
testcase_15 AC 151 ms
76,388 KB
testcase_16 AC 179 ms
76,756 KB
testcase_17 AC 193 ms
76,636 KB
testcase_18 AC 205 ms
77,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Generalized_Union_Find():
    def __init__(self):
        """初期化する.

        N:要素数
        f:2変数関数の合成
        e:最初の値
        """
        self.parent={}
        self.SIZE={}
        self.rank={}

    def vertex_exist(self,x):
        return x in self.parent

    def vertex_add(self,x):
        if x in self.parent:
            return
        self.parent[x]=x
        self.SIZE[x]=1
        self.rank[x]=1
        return

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """

        self.vertex_add(x)

        V=[]
        while self.parent[x]!=x:
            V.append(x)
            x=self.parent[x]

        self.parent[x]=x
        for v in V:
            self.parent[x]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.rank[x]<self.rank[y]:
            x,y=y,x

        self.SIZE[x]+=self.SIZE[y]
        self.parent[y]=x

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return self.SIZE[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        x:要素
        """
        root = self.find(x)
        return [v for v in self.parent if self.find(v)==root]

    def roots(self):
        """族の名前のリスト
        """
        return [v for v in self.parent if self.find(v)==v]

    def group_count(self):
        """族の個数
        """
        x=0
        for v in self.parent:
            if self.find(v)==v:
                x+=1
        return x

    def all_group_members(self):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in self.parent:
            X[self.find(k)].append(k)
        return X

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()
#================================================
N,M=map(int,input().split())
E=[0]*N
U=Generalized_Union_Find()

for _ in range(M):
    a,b=map(int,input().split())
    E[a]+=1
    E[b]+=1
    U.union(a,b)

count=0
for x in E:
    count+=x%2

print("YES" if count<=2 and U.group_count()==1 else "NO")
0