• 문제 링크
9255번: The Friend of My Enemy is My Enemy
For each data set, output “Data Set x:” on a line by itself, where x is its number. Then, output on one line all the direct friends of person s in the network, sorted in increasing order, separated by a space. (No person is friends with himself/herself
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int k = read();
for (int t = 1; t <= k; t++) {
int n = read(), m = read();
Node[] graph = new Node[++n];
while (n-- > 1) graph[n] = new Node(n);
while (m-- > 0) {
int a = read(), b = read();
graph[a].adj.add(b);
graph[b].adj.add(a);
}
sb.append("Data Set ").append(t).append(":\n");
for (int i : new TreeSet<>(graph[read()].adj)) sb.append(i).append(" ");
sb.append("\n\n");
}
bw.write(sb.toString());
bw.flush();
}
private static class Node {
int src;
List<Integer> adj;
Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
}
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' 카테고리의 다른 글
[백준] 6018 Tea Time - Graph Theory / Java (0) | 2024.01.08 |
---|---|
[백준] 6188 Clear Cold Water - Graph Theory / Java (0) | 2024.01.07 |
[백준] 6601 Knight Moves - Graph Theory / Java (0) | 2024.01.05 |
[백준] 8975 PJESMA - Data Structure / Java (0) | 2024.01.04 |
[백준] 9843 LVM - Data Structure / Java (0) | 2024.01.03 |
댓글