• 문제 링크
3098번: 소셜네트워크
소셜 네트워크는 이제 우리 삶의 일부가 되어버렸다. 이러한 소셜 네트워크를 분석하는 김동규 석사과정은 흥미로운 현상을 발견했다. 바로 친구 관계의 수가 급속도로 증가한다는 것이다. 예
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Main {
static Node[] graph;
static Set<Pair> set = new HashSet<>();
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read(), total = m * 2, limit = n * (n - 1);
graph = new Node[n];
while (n-- > 0) graph[n] = new Node(n);
while (m-- > 0) {
int a = read() - 1, b = read() - 1;
graph[a].addEdge(b);
graph[b].addEdge(a);
}
StringBuilder sb = new StringBuilder("\n");
int cnt = 0;
while (total < limit) {
search();
total += set.size() * 2;
sb.append(set.size()).append("\n");
cnt++;
set.clear();
}
bw.write(cnt + sb.toString());
bw.flush();
}
private static void search() {
for (int i = 0; i < graph.length; i++) {
for (int j : graph[i].adj) {
for (int k : graph[j].adj)
if (i != k && !graph[i].adj.contains(k)) set.add(i < k ? new Pair(i, k) : new Pair(k, i));
}
}
for (Pair p : set) {
graph[p.a].addEdge(p.b);
graph[p.b].addEdge(p.a);
}
}
private static class Node {
int src;
Set<Integer> adj;
public Node(int src) {
this.src = src;
this.adj = new HashSet<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
private static class Pair {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair p = (Pair) o;
return this.a == p.a && this.b == p.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
}
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' 카테고리의 다른 글
[백준] 14397 해변 - Graph Theory / Java (0) | 2023.08.06 |
---|---|
[백준] 27737 버섯 농장 - Graph Theory / Java (0) | 2023.08.05 |
[백준] 9204 체스 - Graph Theory / Java (0) | 2023.08.03 |
[백준] 25416 빠른 숫자 탐색 - Graph Theory / Java (0) | 2023.08.02 |
[백준] 26111 Parentheses Tree - Data Structure / Java (0) | 2023.08.01 |
댓글