top of page

2018 CCC J5 c++

#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; }

Recent Posts

See All

Comments


bottom of page