본문 바로가기
Problem Solving/Baekjoon

[백준] 31217 Y - Graph Theory / Java

by graycode 2024. 2. 5.

 문제 링크

 

31217번: Y

첫 번째 줄에 정점의 개수 $n$과 간선의 개수 $m$이 공백으로 분리되어 주어집니다. ($1 \le n \le 10^5$, $0 \le m \le \min(\frac{n(n-1)}{2},2\times 10^5)$) 두 번째 줄부터 $m$개의 줄에 $i$번째 간선이 연결하는 정

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), m = read();

        Node[] graph = new Node[n + 1];
        for (int i = 1; i <= n; i++) graph[i] = new Node(i);

        while (m-- > 0) {
            graph[read()].adj++;
            graph[read()].adj++;
        }

        int cnt = 0;
        for (int i = 1; i <= n; i++)
            if (graph[i].adj > 2) cnt = (cnt + graph[i].adj * (graph[i].adj - 1) * (graph[i].adj - 2) / 6) % 1000000007;

        bw.write(String.valueOf(cnt));
        bw.flush();
    }

    private static class Node {
        int src, adj;

        Node(int src) {
            this.src = src;
        }
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글