Can someone provide the optimized way to solve this? I tried but got 8/16 and tle for rest.
this was my solution ans the question.
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,m,x,i,j,u,v;
cin>>n>>m>>x;
vector<vector<int> > graph(n);
for(i=0;i<m;i++){
cin>>u>>v;
graph[u-1].push_back(v-1);
}
vector<int> ans(n,0);
queue<int> node;
queue<int> h;
vector<int> vis(n,0);
node.push(x-1);
h.push(1);
while(!node.empty()){
int y=node.front();
node.pop();
int w=h.front();
h.pop();
//cout<<y<<" | "<<w<<endl;
if(w%2==0){
ans[y]++;
}
for(i=0;i<graph[y].size();i++){
node.push(graph[y][i]);
h.push(w+1);
}
}
//cout<<endl;
for(i=0;i<n;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
return 0;
}