๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 10159๋ฒˆ - ์ €์šธ

by dkswnkk 2021. 10. 16.
 

10159๋ฒˆ: ์ €์šธ

์ฒซ ์ค„์—๋Š” ๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜ N ์ด ์ฃผ์–ด์ง€๊ณ , ๋‘˜์งธ ์ค„์—๋Š” ๋ฏธ๋ฆฌ ์ธก์ •๋œ ๋ฌผ๊ฑด ์Œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹จ, 5 ≤ N ≤ 100 ์ด๊ณ , 0 ≤ M ≤ 2,000์ด๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์— ๋ฏธ๋ฆฌ ์ธก์ •๋œ ๋น„๊ต ๊ฒฐ๊ณผ๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ

www.acmicpc.net

 

//  Copyright © 2021 ์•ˆ์ฃผํ˜•. All rights reserved.
//  ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
//  https://www.acmicpc.net/problem/10159
//  BOJ10159 ์ €์šธ

#include <iostream>
#define INF 1e9    //๋ฌดํ•œ๋Œ€๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์ง€์ •
using namespace std;
int N, M; //N=๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜, M=๋ฌผ๊ฑด ์Œ์˜ ๊ฐœ์ˆ˜
int graph[101][101];
int ans[101] = { 0, };    //๋น„๊ต๊ฒฐ๊ณผ๋ฅผ ์•Œ์ˆ˜์—†๋Š” ๋ฌผ๊ฑด ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ…Œ์ด๋ธ”

int main() {
    cin >> N >> M;

    for (int i = 0; i < 101; i++) {
        fill(graph[i], graph[i] + 101, INF);
    }

    for (int a = 1; a<= N; a++) {        //์ž๊ธฐ ์ž์‹ ๊ณผ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ๋Š” 0์œผ๋กœ ์„ค์ •
        for (int b = 1; b <= N; b++) {
            if (a == b) graph[a][b] = 0;
        }
    }
    for (int i = 1; i <= M; i++) {    //๋น„๊ต ๊ฒฐ๊ณผ ์ž…๋ ฅ
        int a, b; cin >> a >> b;
        graph[a][b] = 1;
    }

    for (int k = 1; k <= N; k++) {    //ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰
        for (int a = 1; a <= N; a++) {
            for (int b = 1; b <= N; b++) {
                graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]);
            }
        }
    }
    for (int a = 1; a <= N; a++) {    //์ถœ๋ ฅ
        for (int b = 1; b <= N; b++) {
            if (graph[a][b] == INF&&graph[b][a]==INF) ans[a]++;    //N๋ฒˆ์งธ ๋ฌผ๊ฑด์ด ๋‹ค๋ฅธ๋ฒˆํ˜ธ์™€ ์„œ๋กœ ๋น„๊ต๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ• ๋•Œ ์นด์šดํ„ฐ๋ฅผ ์„ธ์ค€๋‹ค.
        }
        cout << ans[a] << "\n";
    }

}

๋Œ“๊ธ€