====== Monochromatic Triangles ====== ===== 풀이 ===== * [[ps:tutorial:부분그래프 세기#모두 연결되었거나, 모두 연결되어있지 않은 세 노드 세기]] 참고. * {{myicon>p4}} [[ps:problems:boj:8907]], {{myicon>p4}} [[ps:problems:boj:17275]] 도 동일한 문제이다. * 시간복잡도는 O(|E|). ===== 코드 ===== """Solution code for "BOJ 8096. Monochromatic Triangles". - Problem link: https://www.acmicpc.net/problem/8096 - Solution link: http://www.teferi.net/ps/problems/boj/8096 Tags: [math] """ import math import sys from teflib import graph as tgraph def count_3_cycle_or_3_independent_set(graph): n = len(graph) degs = [len(neighbors) for neighbors in graph] invalid_set_count = sum(d * (n - 1 - d) for d in degs) // 2 return math.comb(n, 3) - invalid_set_count def main(): n = int(sys.stdin.readline()) m = int(sys.stdin.readline()) graph = tgraph.create_graph_from_input(n, m) print(count_3_cycle_or_3_independent_set(graph)) if __name__ == '__main__': main() {{tag>BOJ ps:problems:boj:플래티넘_4}}