๋ฌธ์
ํ์ด
1๏ธโฃ bfs์ Marble ํด๋์ค ์ ์๋ฅผ ํตํ ํ์ด
๐ก ๋ ์ค๋ฅธ Idea
๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ ๋ฌธ์ ์ ๊ฑฐ์ ์ฃผ์ด์ง๋ค ์ถ์ด ๋์์์๋ค.
๊ตฌ์ฌ์ด ์ด 5๊ฐ์ผ๋๋ ํน์ ๊ตฌ์ฌ๋ณด๋ค ๊ฐ๋ฒผ์ด๊ฒ 3๊ฐ ์ด์์ธ ๊ฒฝ์ฐ ์ค๊ฐ์ด ๋ ์ ์๊ณ
๋ฌด๊ฑฐ์ด๊ฒ 3๊ฐ ์ด์์ธ ๊ฒฝ์ฐ๋ ์ค๊ฐ์ด ๋ ์ ์๋ค.
์ฒ์์๋ N์ด ์ง์๋ก๋ ์ฃผ์ด์ง๋ ์ถ์ด์, ์ง์์ธ ๊ฒฝ์ฐ์๋ ์ฝ๊ฐ ๊ธฐ์ค์ด ๋ฌ๋์ง๋ง ํ์๋ก๋ง ์ฃผ์ด์ง๋ค๊ณ ํ์ผ๋ฏ๋ก
ํน์ ๊ตฌ์ฌ์ด, ์๋์ ์ผ๋ก ๊ฐ๋ฒผ์ด ๊ตฌ์ฌ์ด (N+1)/2 ๊ฐ ์ด์์ด๊ฑฐ๋ ๋ฌด๊ฑฐ์ด ๊ตฌ์ฌ์ด (N+1)/2 ๊ฐ ์ด์์ด๋ผ๋ฉด ์ ๋ ์ค๊ฐ์ด ๋ ์ ์์์ ์ ์ ์๋ค.
๋ฐ๋ผ์, ๊ตฌ์ฌ๋ผ๋ฆฌ ์ฐ๊ฒฐ์ ๋งบ์ด์ฃผ๊ณ ํน์ ๊ตฌ์ฌ๋ณด๋ค ๋ฌด๊ฑฐ์ด ๊ฐ์ ํน์ ๊ฐ๋ฒผ์ด ๊ฐ์๋ฅผ bfs๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํ๋ฉด ๋๋ค.
public class Main {
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
List<Marble> marbles = new ArrayList<>();
// ๊ฐ ๊ตฌ์ฌ๋ง๋ค ๋ฌด๊ฑฐ์ด ํน์ ๊ฐ๋ฒผ์ด ๊ตฌ์ฌ์ ์ ์ฅํ ์ ์๋๋ก Marble ํด๋์ค๋ฅผ ๋ฃ์ด์ค๋ค.
for (int i = 0; i < N; i++) {
marbles.add(new Marble(i));
}
for (int i = 0; i < M; i++) {
input = br.readLine().split(" ");
Marble big = marbles.get(Integer.parseInt(input[0]) - 1);
Marble small = marbles.get(Integer.parseInt(input[1]) - 1);
// ํฐ ๊ตฌ์ฌ์ ์์ ๊ตฌ์ฌ ๋ชจ์์๋ ์์ ๊ฒ๋ค์ ์ถ๊ฐ
big.smallMarbles.add(small);
// ์์ ๊ตฌ์ฌ์ ํฐ ๊ตฌ์ฌ ๋ชจ์์๋ ํฐ ๊ฒ๋ค์ ์ถ๊ฐ
small.bigMarbles.add(big);
}
int ans = 0;
for (int i = 0; i < N; i++) {
// ๊ฐ ๊ตฌ์ฌ ๋ณ๋ก ๋ฌด๊ฑฐ์ด ๊ฐ์ ํน์ ๊ฐ๋ฒผ์ด ๊ฐ์๋ฅผ ์ฒดํฌํด์ค๋ค.
Marble marble = marbles.get(i);
if (getCntOfBigger(marble) >= (N + 1) / 2) {
ans++;
continue;
}
if (getCntOfSmaller(marble) >= ((N + 1) / 2)) {
ans++;
}
}
System.out.println(ans);
}
// bfs ๋ฅผ ์ด์ฉ
private static int getCntOfBigger(Marble node) {
int cnt = 0;
Queue<Marble> q = new LinkedList<>();
boolean[] checked = new boolean[N];
q.offer(node);
checked[node.idx] = true;
while (!q.isEmpty()) {
Marble poll = q.poll();
for (Marble bigMarble : poll.bigMarbles) {
if (!checked[bigMarble.idx]) {
q.offer(bigMarble);
checked[bigMarble.idx] = true;
cnt++;
}
}
}
return cnt;
}
private static int getCntOfSmaller(Marble node) {
int cnt = 0;
Queue<Marble> q = new LinkedList<>();
boolean[] checked = new boolean[N];
q.offer(node);
checked[node.idx] = true;
while (!q.isEmpty()) {
Marble poll = q.poll();
for (Marble smallMarble : poll.smallMarbles) {
if (!checked[smallMarble.idx]) {
q.offer(smallMarble);
checked[smallMarble.idx] = true;
cnt++;
}
}
}
return cnt;
}
static class Marble {
int idx;
List<Marble> bigMarbles = new ArrayList<>();
List<Marble> smallMarbles = new ArrayList<>();
public Marble(int idx) {
this.idx = idx;
}
}
}
๊ฒฐ๊ณผ
2๏ธโฃ ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ
๐ก ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด์ ํ ์๋ ์๋ค๊ณ ํ์ฌ ๋์ ํด๋ณด์๋ค.
๊ตฌ์ฌ ๊ฐ์๊ฐ ์ต๋ 100๊ฐ๋ก 3์ค for๋ฌธ์ ์ฌ์ฉํ๋ ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ์ถฉ๋ถํ ์ฌ์ฉํ ์ ์์์ ํ๋จํ์๋ค.
ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ ์ ์๋ ์ผ์ข ์ ํํธ๋ก์ ์ ์บ์นํด๋ด์ผ๊ฒ ๋ค.
ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ํตํด์ ๊ตฌ์ฌ์ด ํฐ ๊ด๊ณ ๋ผ๋ฆฌ๋ง ์ฐ๊ฒฐํ๋ ๊ทธ๋ํ์ ๊ตฌ์ฌ์ด ์์ ๊ฒฝ์ฐ ์ฐ๊ฒฐํ๋ ๊ทธ๋ํ 2๊ฐ๋ฅผ ์ค๋นํด์ ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฐ๊ฒฐํด์ค๋ค.
๊ฒฐ๊ณผ๋ก ๋์จ ๊ทธ๋ํ๊ฐ ์ด๊ธฐ๊ฐ์ด๋ MAX ๊ฐ์ด ์๋๋ผ๋ฉด ์ด์ฐ๋๋ ์ฐ๊ฒฐ์ด ๋์๋ค๋ ์๋ฏธ์ด๋ฏ๋ก
์นด์ดํ ์ํด์ค์ ๊ฐ์๊ฐ (N+1)/2 ์ด์์ธ์ง ์ฒดํฌํด์ฃผ๋ฉด ๋๋ค.
public class Main {
static int N;
static final int MAX = 100;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
int[][] smalls = new int[N][N];
int[][] bigs = new int[N][N];
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
if (r != c) {
smalls[r][c] = MAX;
bigs[r][c] = MAX;
}
}
}
for (int i = 0; i < M; i++) {
input = br.readLine().split(" ");
int big = Integer.parseInt(input[0]) - 1;
int small = Integer.parseInt(input[1]) - 1;
// ์์ ๊ด๊ณ ๊ทธ๋ํ
smalls[small][big] = 1;
// ํฐ ๊ด๊ณ ๊ทธ๋ํ
bigs[big][small] = 1;
}
//ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ
for (int m = 0; m < N; m++) {
for (int u = 0; u < N; u++) {
for (int v = 0; v < N; v++) {
smalls[u][v] = Math.min(smalls[u][v], smalls[u][m] + smalls[m][v]);
bigs[u][v] = Math.min(bigs[u][v], bigs[u][m] + bigs[m][v]);
}
}
}
int ans = 0;
for (int i = 0; i < N; i++) {
int cntOfSmall = getCount(smalls, i);
if (cntOfSmall >= (N + 1) / 2) {
ans++;
continue;
}
int cntOfBig = getCount(bigs, i);
if (cntOfBig >= (N + 1) / 2) {
ans++;
}
}
System.out.println(ans);
}
private static int getCount(int[][] graph, int i) {
int cnt = 0;
for (int c = 0; c < N; c++) {
if (graph[i][c] != MAX && graph[i][c] != 0) {
cnt++;
}
}
return cnt;
}
}
๊ฒฐ๊ณผ