結果

問題 No.2563 色ごとのグループ
ユーザー kinoko_econkinoko_econ
提出日時 2023-12-02 15:27:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,351 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 269,308 KB
最終ジャッジ日時 2023-12-02 15:27:28
合計ジャッジ時間 8,305 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
60,264 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 34 ms
53,460 KB
testcase_05 AC 34 ms
53,460 KB
testcase_06 AC 33 ms
53,460 KB
testcase_07 AC 35 ms
53,460 KB
testcase_08 AC 36 ms
53,460 KB
testcase_09 AC 40 ms
59,588 KB
testcase_10 AC 39 ms
59,600 KB
testcase_11 AC 41 ms
59,588 KB
testcase_12 AC 40 ms
59,588 KB
testcase_13 AC 35 ms
53,460 KB
testcase_14 AC 87 ms
76,720 KB
testcase_15 AC 91 ms
76,712 KB
testcase_16 AC 95 ms
76,720 KB
testcase_17 AC 86 ms
76,592 KB
testcase_18 AC 73 ms
73,868 KB
testcase_19 AC 93 ms
76,672 KB
testcase_20 AC 1,765 ms
263,280 KB
testcase_21 AC 384 ms
79,232 KB
testcase_22 AC 175 ms
77,568 KB
testcase_23 AC 297 ms
78,592 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int,input().split())
C = list(map(int,input().split()))
G = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int,input().split())
    if C[u-1] == C[v-1]:
        G[u-1].append(v-1)
        G[v-1].append(u-1)

group = [[] for _ in range(N)]
for i in range(N):
    group[C[i]-1].append(i)
set_group = [set(group[i]) for i in range(N)]

def dfs(v, G, seen):
    # 頂点 v を探索済みにする
    seen[v] = True
    # G[v] に含まれる頂点 v2 について、
    for v2 in G[v]:
        # v2 がすでに探索済みならば、スキップする
        if seen[v2]: continue
        # v2 始点で深さ優先探索を行う (関数を再帰させる)
        dfs(v2, G, seen)
    return
Ans = 0
for i in range(N):
    if len(group[i]) <= 1:
        continue 
    seen = [False for _ in range(N)]    # seen[v]:頂点 v が探索済みなら true, そうでないなら false
    ans = 0 # 答えとなる変数
    # 全ての頂点について
    for v in range(N):
        # 頂点 v がすでに訪問済みであれば、スキップ
        if seen[v] or v not in set_group[i]: continue
        # そうでなければ、頂点 v を含む連結成分は未探索
        # 深さ優先探索で新たに訪問し、答えを 1 増やす
        dfs(v, G, seen)
        ans += 1
    Ans += ans-1
print(Ans)







0