-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCC '18 J5 - Choose your own path
60 lines (49 loc) · 1.4 KB
/
CCC '18 J5 - Choose your own path
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
#include <iostream>
#include <queue>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int n, m, x; //n = number of pages, m = # pages connected to ith page, x = page connected to ith page
int i, j;
int stps;
int cur;
scanf(" %d", &n);
int chk[n];
vector <int> pg[n];
queue <int> q;
stps = n+1; //Since there is at least one ending page, and the furthest distance would be the number of pages
for(i=0; i<n; i++){
scanf(" %d", &m);
chk[i] = 0;
for(j=0; j<m; j++){
scanf(" %d", &x);
x--; //Subtract 1 from x since array is 0 indexed
pg[i].push_back(x);
}
}
chk[0] = 1; //Since the first page counts as 1 distance
q.push(0);
while(!q.empty()){
cur = q.front();
q.pop();
if(pg[cur].empty() && chk[cur] < stps){ //*Second condition for making sure the minimum distance doesn't get reset
stps = chk[cur];
}
for(i=0; i<pg[cur].size(); i++){
if(chk[pg[cur][i]] == 0 || chk[cur]+1 < chk[pg[cur][i]]){
chk[pg[cur][i]] = chk[cur]+1;
q.push(pg[cur][i]);
}
}
}
for(i=0; i<n; i++){ //Checking if all the pages have been reached
if(chk[i]==0){
printf("N\n%d", stps);
return 0;
}
}
printf("Y\n%d", stps);
return 0;
}