-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.cpp
48 lines (44 loc) · 988 Bytes
/
factory.cpp
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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
bool connected[101][101];
vector<int> connections[101];
void dfs(int initial, int current) {
if (connected[initial][current]) {
return;
}
connected[initial][current] = true;
for (auto neighbor: connections[current]) {
dfs(initial, neighbor);
}
}
int main() {
freopen("factory.in", "r", stdin);
freopen("factory.out", "w", stdout);
int N; cin >> N;
for (int i=0; i<N-1; i++) {
int a, b; cin >> a >> b;
connections[a].push_back(b);
}
for (int i=1; i<=N; i++) {
dfs(i, i);
}
for (int i=1; i<=N; i++) {
bool good = true;
for (int j=1; j<=N; j++) {
if (not connected[j][i]) {
good = false;
}
}
if (good) {
cout << i;
return 0;
}
}
cout << -1;
return 0;
}