Il codice passa tutti i testcase tranne il 6,7,18 e 19 per Execution killed (could be triggered by violating memory limits). Non capisco proprio dove sia l’errore, aiuto plz
#include <iostream>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
vector<int> split(string t){
vector<int> v;
string temp="";
for(int i=0; i<t.size(); i++){
if(t[i]==' '){
v.pb(stoi(temp));
temp = "";
}
else temp += t[i];
}
v.pb(stoi(temp));
return v;
}
int main(){
int N,K;
cin >> N >> K;
cin.ignore();
string t;
getline(cin, t);
vector<int> V = split(t);
int tent = 0;
int j=0;
while(true){
if(tent > N){
cout << -1;
break;
}
if(j+1==K){
cout << tent;
break;
}
else{
tent++;
j=V[j]-1;
}
}
return 0;
}