2458번: 키 순서
1번부터 N번까지 번호가 붙여져 있는 학생들에 대하여 두 학생끼리 키를 비교한 결과의 일부가 주어져 있다. 단, N명의 학생들의 키는 모두 다르다고 가정한다. 예를 들어, 6명의 학생들에 대하여
www.acmicpc.net
- 풀이
풀이 방식은 플로이드와샬을 사용했습니다.
자신보다 우선순위가 큰 값은 (i,j)를 1로 작은 값은 -1로 초기화 시켰습니다.
플로이드와샬 방식을 통해 배열을 돌며 자신의 큰 값보다 더 큰 값 또한 (i,j)를 1로 작은 값보다 더 작은 값은 -1로 초기화 시켜주었습니다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
static int N, M;
static int[][] array;
static int answer;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SetData();
System.out.println(answer);
}
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
M = in.nextInt();
answer = 0;
array = new int[N+1][N+1];
for(int i = 0 ; i < M; i++) {
int a = in.nextInt();
int b = in.nextInt();
array[a][b] = 1;
array[b][a] = -1;
}
for(int k = 1; k <= N; k++) {
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
if(array[i][k] + array[k][k] == 2)
array[i][j] = 1;
else if(array[i][k] + array[k][j] == -2)
array[i][j] = -1;
}
}
}
for(int i = 1; i <= N; i++) {
int count = 0;
for(int j = 1; j <= N; j++) {
if(array[i][j] != 0 || array[j][i] != 0)
count++;
}
if(count == N-1) answer++;
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 13023번 : ABCDE (0) | 2021.10.21 |
---|---|
[BOJ/JAVA] 백준 1261번 : 알고스팟 (0) | 2021.10.20 |
[BOJ/JAVA] 백준 11403번 : 경로 찾기 (0) | 2021.10.19 |
[BOJ/JAVA] 백준 1707번 : 이분 그래프 (0) | 2021.10.14 |
[프로그래머스/JAVA] 위클리 챌린지 10주차 (교점에 별 만들기) (0) | 2021.10.13 |