結果

問題 No.1147 土偶Ⅱ
ユーザー lam6er
提出日時 2025-03-20 20:25:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 85 ms / 500 ms
コード長 1,182 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,904 KB
実行使用メモリ 77,292 KB
最終ジャッジ日時 2025-03-20 20:26:27
合計ジャッジ時間 2,144 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
max_node = n
friends = [[False] * (max_node + 1) for _ in range(max_node + 1)]

for _ in range(m):
    a, b = map(int, input().split())
    friends[a][b] = True
    friends[b][a] = True

# Initialize friends_of_friends (fof) matrix
fof = [[False] * (max_node + 1) for _ in range(max_node + 1)]
for i in range(1, n+1):
    for j in range(i+1, n+1):
        if friends[i][j]:
            fof[i][j] = False  # Friends cannot be friends-of-friends
        else:
            has_common = False
            for k in range(1, n+1):
                if friends[i][k] and friends[j][k]:
                    has_common = True
                    break
            fof[i][j] = has_common
            fof[j][i] = has_common  # Mirror for lower half, though not necessary if handled in loop correctly

count = 0
# Check all possible triplets
for i in range(1, n+1):
    for j in range(i+1, n+1):
        if fof[i][j]:
            continue  # Skip if i and j are friends-of-friends
        for k in range(j+1, n+1):
            if fof[i][k] or fof[j][k]:
                continue  # Skip if any other pair is friends-of-friends
            count += 1

print(count)
0