-
Notifications
You must be signed in to change notification settings - Fork 1
/
2013 S4.cpp
106 lines (65 loc) · 2.16 KB
/
2013 S4.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
2013 S4 - Who is Taller?
Difficulty: Easy/Medium
This problem is a very easy S4, simple graph traversal
You can view this problem as a directed graph with the taller person pointing to the shorter person
To check if A is taller than B
Start at A and keep going down the chain of shorter people
If you find B, it means B is shorter than A
If you can't find B it either means unknown or B is taller
Repeat the same process with B
If you still cannot find A from B, it's unknown otherwise, A is shorter
*/
#include <iostream>
#include <vector>
#include <queue>
int main(){
int N, M;
std::cin >> N >> M;
std::vector<std::vector<int>> shorter (N + 1);
//Construct our graph
for (int i = 0; i < M; i++){
int taller, shorterPerson;
std::cin >> taller >> shorterPerson;
shorter[taller].push_back(shorterPerson);
}
int personA, personB;
std::cin >> personA >> personB;
std::vector<bool> visited (N + 1, false);
std::queue<int> q;
q.push(personA);
visited[personA] = true;
char personAisTaller = 'u';
//Try finding person B starting from A
while (!q.empty()){
int currentPerson = q.front();
q.pop();
for (auto shorterPerson: shorter[currentPerson]){
if (shorterPerson == personB) personAisTaller = 'y';
if (!visited[shorterPerson]){
q.push(shorterPerson);
visited[shorterPerson] = true;
}
}
}
visited.clear();
visited.resize(N + 1, false);
q.push(personB);
visited[personB] = true;
//Try the other way around, start from person B and go to person A
while (!q.empty()){
int currentPerson = q.front();
q.pop();
for (auto shorterPerson: shorter[currentPerson]){
if (shorterPerson == personA) personAisTaller = 'n';
if (!visited[shorterPerson]){
q.push(shorterPerson);
visited[shorterPerson] = true;
}
}
}
if (personAisTaller == 'y') std::cout << "yes";
else if (personAisTaller == 'n') std::cout << "no";
else std::cout << "unknown";
return 0;
}