-
Notifications
You must be signed in to change notification settings - Fork 3
/
CCC07S3.cpp
56 lines (47 loc) · 1006 Bytes
/
CCC07S3.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
// Problem: CCC '07 S3 - Friends
// Contest: CCC
// URL: https://dmoj.ca/problem/ccc07s3
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// Ryan Liu
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
#define pb push_back
vector<int> adj[10000];
queue<int> q;
int n, t1, t2;
void bfs(int a, int b) {
bool used[10000];
int dist[10000];
memset(used, false, sizeof(used));
memset(dist, 0, sizeof(dist));
q.push(a);
used[a] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : adj[v]) {
if (!used[u]) {
used[u] = true;
q.push(u);
dist[u] = dist[v] + 1;
}
}
}
cout << ((used[b]) ? ("Yes " + to_string(dist[b] - 1)) : "No") << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t1 >> t2;
adj[t1].pb(t2);
}
while ((cin >> t1 >> t2) && ((t1 != 0) && (t2 != 0))) {
bfs(t1, t2);
}
}