1006E - Military Problem - CodeForces Solution


dfs and similar graphs trees *1600

Please click on ads to support us..

C++ Code:

// AUTHOR : PRIYESH ANAND
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pp pop_back
#define dsort(a,n)  sort(a,a+n,greater<int>())
#define min_heap priority_queue<int, vector<int>, greater<int>> pq;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
 
/* BE A BOSS */
 
using namespace std;

void dfs(int i, vector<int> adj[], vector<int>& DFS) {
	
	DFS.pb(i);
	int cnt=0;
	for(auto it:adj[i]) {
	    dfs(it,adj,DFS);
	}
}

int cntsize(int i, vector<int> adj[], vector<int>& cnt) {
	
	cnt[i]=adj[i].size();
	for(auto it:adj[i]) {
		cnt[i]+=cntsize(it,adj,cnt);
	}
	return cnt[i];	
}

void wrong_answer() {
    
    int n,q,a;
    cin>>n>>q;
    vector<int> adj[n+1];
    for(int i=2; i<=n; ++i) {
    	cin>>a;
    	adj[a].pb(i);
	}
	vector<int> DFS;
	dfs(1,adj,DFS);
	vector<int> cnt(n+1,0);
	cntsize(1,adj,cnt);
	for(int i=1; i<=n; ++i) {
		cnt[i]+=1;
	}
	map<int,int> mp;
	for(int i=0; i<n; ++i) {
		mp[DFS[i]]=i;
	}
	int u,k;
	while(q--) {
		cin>>u>>k;
		int x=mp[u];
		int y=x+(k-1);
		if(y-x+1>cnt[u] or y>=n)cout<<-1<<endl;
		else cout<<DFS[y]<<endl;
	}
    
}
 
int main ()
{
	fast;
//	int t;
//	cin>>t;
//	while(t--)
	wrong_answer();
  return 0;
}
 
// --------------------------------Binary Exponentiation-----------------------------------//
// ll binpow(ll a, ll b) {
//     ll res = 1;
//     while (b > 0) {
//         if (b & 1)
//             res = res * a;
//         a = a * a;
//         b >>= 1;
//     }
//     return res;
// }
 
//-------------------------------Seive of Erathonesthenes-----------------------------------//
// void SieveOfEratosthenes(ll n)
// { 
//     bool prime[n + 1];
//     memset(prime, true, sizeof(prime));
 
//     for (ll p = 2; p * p <= n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (ll i = p * p; i <= n; i += p)
//                 prime[i] = false;
//              //v.push_back(i);
//         }
//     }  
// }
 
//-----------------------------SPF(Smallest Prime Factor) array------------------------------//
// ll spf[n];
// for(int i=0;i<=n;i++){
//       spf[i] = i;
//  }
//  for(int i=2;i*i<=n;i++){
//     if(spf[i]==i){
//         for(int j=i*i;j<=n;j+=i){
//             if(spf[j]==j){
//                 spf[j]=i;
//             }
//         }
//      }
//  }
 
//--------------------------------------NO. of digit---------------------------------------//
// ll ndig(ll n){
//  ll tmp= n,ctr=0;
//  while(tmp>0){
//      tmp/=10;
//      ctr++;
//  }
//  return ctr;
//  // take care: if n==0 this fn returns 0.
// }
 
//------------------------------------------------------------------------------------------//
//sort(a,a+n,greater<ll>());  //---> to sort in descending order.
//
//  Custom Comparator:
//
// bool cmpr(pair<ll,ll> a,pair<ll,ll> b){
//      return(a.second<b.second);
//}
//sort(v.begin(),v.end(),cmpr);
//#define min_heap priority_queue<int, vector<int>, greater<int>> pq;


Comments

Submit
0 Comments
More Questions

1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing