반응형

https://www.acmicpc.net/problem/10159

 

10159번: 저울

첫 줄에는 물건의 개수 N 이 주어지고, 둘째 줄에는 미리 측정된 물건 쌍의 개수 M이 주어진다. 단, 5 ≤ N ≤ 100 이고, 0 ≤ M ≤ 2,000이다. 다음 M개의 줄에 미리 측정된 비교 결과가 한 줄에 하나씩

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. arr [ i ][ j ]에서 [ i ] > [ j ]라면 2, [ i ] < [ j ]라면 1로 저장합니다.

 

2. 1에서 N까지 for문을 돌면서 bfs를 통해 무게를 비교 가능하다면 cnt [ i ]에 값을 저장합니다.

 

3. N - cnt[ i ] - 1을 출력합니다.

 

[ 소스코드 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<queue>
 
using namespace std;
 
int N, M;
int arr[101][101];
int cnt[101];
 
int main()
{
    scanf("%d %d"&N, &M);
    
    for (int i = 0; i < M; i++) {
        int a, b;
        scanf("%d %d"&a, &b);
 
        arr[a][b] = 2;
        arr[b][a] = 1;
    }
 
    for (int i = 1; i <= N; i++) {
        int visited[101= { 0 };
 
        queue<pair<intint>> q;
        visited[i] = 1;
 
        for (int j = 1; j <= N; j++) {
            if (arr[i][j] != 0) {
                q.push({ j,arr[i][j] });
                cnt[i]++;
                visited[j] = 1;
            }
        }
 
        while (!q.empty()) {
            const int cur = q.front().first;
            const int ineq = q.front().second;
            q.pop();
 
            for (int j = 1; j <= N; j++) {
                if (visited[j] != 1 && arr[cur][j] == ineq) {
                    visited[j] = 1;
                    q.push({ j,ineq });
                    cnt[i]++;
                }
            }
        }
    }
 
    for (int i = 1; i <= N; i++) {
        printf("%d\n", N - cnt[i] - 1);
    }
}
cs
반응형

+ Recent posts