문제 링크: 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, 0, sizeof(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
|
'USACO > Bronze' 카테고리의 다른 글
백준 18269 Where Am I? (USACO December 2019 Bronze 2번) (0) | 2020.03.20 |
---|---|
백준 18268 Cow Gymnastics (USACO December 2019 Bronze 1번) (0) | 2020.03.18 |
백준 17198 Bucket Brigade (USACO US Open 2019 Bronze 1번) (0) | 2020.03.17 |
백준 17041 Measuring Traffic (USACO February 2019 Bronze 3번) (0) | 2020.03.17 |
백준 17040 The Great Revegetation (USACO February 2019 Bronze 2번) (0) | 2020.03.14 |