본문 바로가기

USACO/Bronze

백준 17199 Milk Factory (USACO US Open 2019 Bronze 2번)

문제 링크: https://www.acmicpc.net/problem/17199

 

17199번: Milk Factory

The milk business is booming! Farmer John's milk processing factory consists of $N$ processing stations, conveniently numbered $1 \ldots N$ ($1 \leq N \leq 100$), and $N-1$ walkways, each connecting some pair of stations. (Walkways are expensive, so Farmer

www.acmicpc.net

a, b를 거꾸로 받아서, i = 1 ~ N번 순서대로 station i에서 다른 모든 station으로 갈수 있는지를 dfs로 체크하고, 가능하면 i를 출력하고, 가능한 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
#include <bits/stdc++.h>
using namespace std;
 
int N, V[101];
vector<int> adj[101];
 
void dfs(int cur) {
    V[cur] = 1;
    for(int i = 0; i < adj[cur].size(); i++) {
        int nxt = adj[cur][i];
        if(!V[nxt]) dfs(nxt);
    }
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N;
    for(int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        adj[b].push_back(a);
    }
 
    for(int i = 1; i <= N; i++) {
        memset(V, 0sizeof(V));
        dfs(i);
        bool ok = true;
        for(int j = 1; j <= N; j++)
            if(V[j] == 0) ok = false;
        if(ok) {
            cout << i << '\n';
            return 0;
        }
    }
 
    cout << -1 << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter