#include <iostream> #include <vector> #include <stack> #include <queue> #include <set> using namespace std; int main() { int N; cin >> N; vector<int> books[N+1]; bool visited[N+1]; int minPath = N+1; int dist[N+1]; visited[0] = true; dist[0] = minPath; for(int i = 1; i < N+1; i++){ int pn = 0; cin >> pn; if(pn == 0){ books[i].push_back(pn); } for(int j = 0; j < pn; j++){ int n = 0; cin >> n; books[i].push_back(n); } visited[i] = false; dist[i] = minPath; } vector<int> mystack; mystack.push_back(1); visited[1] = true; dist[1] = 1; while(!mystack.empty()){ int n = mystack.back(); mystack.pop_back(); int p = dist[n]; for(int i = 0; i < books[n].size(); i++){ if(books[n][0] == 0){ if(minPath > p) minPath = p; } else{ int x = books[n][i]; if(p+1 < dist[x]){ dist[x] = p+1; visited[x] = false; } if(visited[x] == 0){ mystack.push_back(x); visited[x] = 1; } } } } bool allvisited = true; for(int i = 1; i < N+1; i++){ if( visited[i] == false){ allvisited = false; break; } } if(allvisited) cout<<"Y"<<endl; else cout<<"N"<<endl; cout <<minPath<<endl; return 0; }
top of page
Recent Posts
See All#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c,...
s1 = input() s2 = input() silly = '' silly_o = '' quiete = '-' i = 0 j = 0 while i < len(s1) and j < len(s2): if s1[i] != s2[j]: if...
#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int,...
bottom of page
Comments