• 문제 링크
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 8073 Road Net - Graph Theory / Java (0) | 2024.02.07 |
---|---|
[백준] 17848 Flight Turbulence - Graph Theory / Java (0) | 2024.02.06 |
[백준] 18422 Emacs - Graph Theory / Java (0) | 2024.02.04 |
[백준] 4351 Hay Points - Data Structure / Java (0) | 2024.02.03 |
[백준] 7587 Anagrams - Data Structure / Java (0) | 2024.02.02 |
댓글