?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
207884247 |
Practice: visheshgautam.official |
1775D - 20 | C++14 (GCC 6-32) | Accepted | 795 ms | 38584 KB | 2023-05-30 19:54:55 | 2023-05-30 19:54:55 |
#include<bits/stdc++.h> using namespace std; const int maxval{300000}; int main(){ int n; cin>>n; vector<int>inp(n+1); for (int i = 1; i <= n; i++) { cin>>inp[i]; } int a,b; cin>>a>>b; vector<vector<int>>v(n+maxval+1); for (int i = 1; i <= n; i++) { int x=inp[i];; for (int j = 2; j*j<= x; j++) { if(x%j)continue; while((x%j)==0)x/=j; v[i].push_back(n+j), v[n+j].push_back(i); } if(x>1){ v[i].push_back(x+n); v[x+n].push_back(i); } } bool found=false; vector<int>par(n+maxval+1,-1); vector<int>vis(n+maxval+1); queue<int>q; q.push(a); while (!q.empty()) { int node=q.front(); q.pop(); if(node==b){found=true;break;} vis[node]=true; for(auto x:v[node]){ if(par[x]!=-1)continue; if(vis[x]==false){ par[x]=node; q.push(x); } } } if(found){ vector<int>ans; for (int i = b; i !=a&&par[i]!=-1; i=par[par[i]]) { ans.push_back(i); } cout<<ans.size()+1<<"\n"; reverse(ans.begin(),ans.end()); cout<<a<<" "; for (int i = 0; i < ans.size(); i++) { cout<<ans[i]<<" "; } cout<<"\n"; } else{ cout<<-1<<"\n"; } return 0; }
?
?
?
?