티스토리 뷰

Algorithm

[백준 15587] Cow at Large (Gold)

devbelly 2021. 2. 8. 13:40

문제

www.acmicpc.net/problem/15587

 

알고리즘

BFS

 

풀이

Bessie가 트리 형태의 목장에서 탈출하려 합니다. 이때 이 탈출을 제지해야하는 최소의 농부 수를 구하는 문제입니다. 탈출구는 리프노드입니다.

 

만일 한 농부가 $u$노드까지의 거리가 베시보다 가까우면서 다른 농부들보다도 가깝다면 다른 농부들을 카운트할 필요는 없습니다. 이를 위해 다양한 source로부터(여기서는 리프노드, 농부들의 시작점) bfs를 수행할 것입니다. $dist$의 상태에는 3가지가 있습니다. 아직 방문하지 않은 상태인 -1, 농부가 방문한 상태인 0, bessie가 도착한 int_max의 상태입니다. 

 

아래 코드에서 가장 눈여겨 볼 점은 bessie에 해당하는 노드를 queue에 넣을 때, 농부들보다 뒤에 넣는다는 점입니다. 이렇게 하면 $u$노드에 동시에 도착하는 것은 물론 터널 사이에서 만나는 것 또한 고려할 수 있습니다. 시간복잡도는 $O(N)$입니다. 

 

코드

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
using namespace std;

const int MAXN = 1e5 + 5;

int N, K, ans;
int dist[MAXN];
queue<int> q;
vector<vector<int>> adj;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    cin.tie(NULL);
    cout.tie(NULL);
    ios::sync_with_stdio(false);

    cin >> N >> K;
    adj.resize(N);
    memset(dist, -1, sizeof(dist));
    --K;
    rep(i, N - 1) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }

    rep(i, N) {
        if (adj[i].size() == 1) {
            q.emplace(i);
            dist[i] = 0;
        }
    }

    q.emplace(K);
    dist[K] = INT_MAX;
    while (!q.empty()) {
        int here = q.front();
        q.pop();

        for (auto there : adj[here]) {
            if (dist[there] == -1) {
                if (dist[here] == INT_MAX) {
                    dist[there] = INT_MAX;
                } else {
                    dist[there] = 0;
                }
                q.emplace(there);
            } else if (dist[here] == INT_MAX && dist[there] != INT_MAX) {
                ans += 1;
            }
        }
    }
    cout << ans;
    return 0;
}

'Algorithm' 카테고리의 다른 글

[백준 8875] 장난감 정리 로봇  (0) 2021.02.10
[백준 1126] 같은 탑  (0) 2021.02.08
[백준 15589] Lifeguards (Silver)  (0) 2021.02.05
[백준 12877] 먹이 사슬  (0) 2021.01.28
[백준 15590] Rental Service  (0) 2021.01.26
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함