Josif_tepe

Untitled

May 10th, 2026
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int n;
  8.     cin>>n;
  9.     vector<int>node(n+1);
  10.     for(int i=1;i<=n;i++){
  11.         cin>>node[i];
  12.     }
  13.     int Q;
  14.     cin>>Q;
  15.     while(Q--){
  16.         int t,x;
  17.         cin>>t>>x;
  18.         if(t==2){
  19.             node[x]=0;
  20.         }else{
  21.             vector<int>visited(n+1,0);
  22.             queue<int>q;
  23.             q.push(x);
  24.             bool pronajden=false;
  25.             while(!q.empty()){
  26.                 int c=q.front();
  27.                 q.pop();
  28.                 if(visited[c]){
  29.                     cout<<"CIKLUS"<<endl;
  30.                     pronajden=true;
  31.                     break;
  32.                 }
  33.                 visited[c]=1;
  34.                 if(node[c]==0){
  35.                     cout<<c<<endl;
  36.                     pronajden=true;
  37.                     break;
  38.                 }
  39.                 q.push(node[c]);
  40.             }
  41.  
  42.         }
  43.     }
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment