Skip to content

Commit 8a9bb23

Browse files
authored
[20250714] BOJ / G4 / 저울 / 이종환
1 parent d851a42 commit 8a9bb23

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

0224LJH/202507/14 BOJ 저울.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
```java
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class Main {
9+
static StringBuilder sb = new StringBuilder();
10+
static int stuffCnt,pairCnt;
11+
static Stuff[] stuffs;
12+
static boolean[][] visited;
13+
14+
15+
static class Stuff implements Comparable<Stuff>{
16+
int num;
17+
int parentCnt = 0;
18+
List<Stuff> parents = new ArrayList<>();
19+
List<Stuff> children = new ArrayList<>();
20+
21+
public Stuff(int num) {
22+
this.num = num;
23+
}
24+
25+
@Override
26+
public int compareTo(Stuff o) {
27+
return o.parentCnt - this.parentCnt;
28+
}
29+
30+
//
31+
public void addToChildParentList(int num) {
32+
for (Stuff child : children) {
33+
if (visited[child.num][num]) continue;
34+
visited[child.num][num] = true;
35+
child.addToChildParentList(num);
36+
}
37+
}
38+
39+
public void addToParentChildList(int num) {
40+
for ( Stuff parent : parents) {
41+
if (visited[parent.num][num]) continue;
42+
visited[parent.num][num] = true;
43+
parent.addToParentChildList(num);
44+
}
45+
}
46+
}
47+
48+
49+
public static void main(String[] args) throws IOException {
50+
init();
51+
process();
52+
print();
53+
}
54+
55+
private static void init() throws IOException {
56+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
57+
stuffCnt = Integer.parseInt(br.readLine());
58+
pairCnt = Integer.parseInt(br.readLine());
59+
60+
stuffs = new Stuff[stuffCnt+1];
61+
visited = new boolean[stuffCnt+1][stuffCnt+1];
62+
63+
for (int i = 1; i <= stuffCnt; i++) {
64+
stuffs[i] = new Stuff(i);
65+
visited[i][i] = true;
66+
}
67+
68+
for (int i = 0; i < pairCnt; i++) {
69+
StringTokenizer st = new StringTokenizer(br.readLine());
70+
int parent = Integer.parseInt(st.nextToken());
71+
int child = Integer.parseInt(st.nextToken());
72+
73+
stuffs[parent].children.add(stuffs[child]);
74+
stuffs[child].parents.add(stuffs[parent]);
75+
stuffs[child].parentCnt++;
76+
visited[parent][child] = true;
77+
visited[child][parent] = true;
78+
}
79+
}
80+
81+
private static void process() {
82+
PriorityQueue<Stuff> pq = new PriorityQueue<>();
83+
for (int i = 1; i <= stuffCnt; i++) {
84+
pq.add(stuffs[i]);
85+
}
86+
87+
while (!pq.isEmpty()) {
88+
Stuff s = pq.poll();
89+
int num = s.num;
90+
91+
for (Stuff child : s.children) {
92+
child.addToChildParentList(num);
93+
}
94+
for (Stuff parent : s.parents) {
95+
parent.addToParentChildList(num);
96+
}
97+
}
98+
99+
for (int i = 1; i <= stuffCnt; i++) {
100+
int ans = stuffCnt;
101+
102+
for (int j = 1; j <= stuffCnt; j++) {
103+
if (visited[i][j]) ans--;
104+
}
105+
sb.append(ans).append("\n");
106+
}
107+
108+
109+
}
110+
111+
112+
113+
114+
private static void print() {
115+
System.out.println(sb.toString());
116+
}
117+
118+
119+
120+
}
121+
122+
```

0 commit comments

Comments
 (0)