#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-15
const int N = 1e5+12,INF=1e9+7;
int q[N];
bool vis[N];
main(){
int n,i,j,m;
cin>>n;
for (i=1;i<=n;++i){
cin>>q[i];
}
cin>>m;
while (m--){
int t;
cin>>t;
if (t==1){
int x;
cin>>x;
q[x] = 0;
}
else {
int x,y;
cin>>x>>y;
int ans=0;
priority_queue <int> t;
while (q[x] != 0 && x != y){
if (vis[x])break;
vis[x]=1;
t.push(x);
x = q[x];
ans ++;
}
while (!t.empty()){
vis[t.top()]=0;
t.pop();
}
if (x != y )ans =-1;
cout <<ans<<'\n';
}
}
}
Compilation message
specialg.cpp:23:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
23 | main(){
| ^
specialg.cpp: In function 'int main()':
specialg.cpp:24:13: warning: unused variable 'j' [-Wunused-variable]
24 | int n,i,j,m;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
364 KB |
Output is correct |
2 |
Correct |
12 ms |
364 KB |
Output is correct |
3 |
Correct |
15 ms |
364 KB |
Output is correct |
4 |
Correct |
38 ms |
364 KB |
Output is correct |
5 |
Correct |
16 ms |
364 KB |
Output is correct |
6 |
Correct |
404 ms |
620 KB |
Output is correct |
7 |
Correct |
432 ms |
364 KB |
Output is correct |
8 |
Correct |
461 ms |
448 KB |
Output is correct |
9 |
Correct |
367 ms |
364 KB |
Output is correct |
10 |
Correct |
475 ms |
364 KB |
Output is correct |
11 |
Execution timed out |
1077 ms |
1824 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |