-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc07s3.cpp
46 lines (43 loc) · 975 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
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[10000];
int vis[10000], dist[10000];
int n;
bool cycle = false;
int ans = 0;
void dfs(int src){
if (ans > 0) return;
vis[src] = 1;
for (auto x: adj[src]){
if (vis[x] == 1){
ans = dist[x] - dist[src] - 1;
cycle = true;
return;
} else if (vis[x] == 0){
dist[x] = dist[src]+1;
dfs(x);
}
}
vis[src] = 2;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
}
int x = -1, y = -1;
while (true){
memset(vis, 0, sizeof(vis));
memset(dist, -1, sizeof(dist));
cycle = false;
cin >> x >> y;
if (x == 0 && y == 0) break;
dfs(x);
if (vis[y] != 0) cout <<"Yes " << dist[y] << "\n";
else cout << "No" << "\n";
}
}