[CLOSED] Atcoder ABC168 D Mistake in code?

Problem Link
I am trying to solve it using BFS from node 1 and storing the parent for each node > 1.
My inputs are matching for sample_01.txt and sample_02.txt manually but giving WA when submitted.

// Author: Amey Bhavsar
// IDE: Geany/Ubuntu
// Platform: ABC168

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i,a,b) for(auto i=a;i<b;i++)
#define repD(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
// traversal function to avoid long template definition
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
//find version works for all containers. This is present in std namespace.
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define MOD 1000000007
//make sure to change MAXN according to constraints
#define MAXN 200005
int n,m,u,v;
int main() {
	cin >> n >> m;
	vector<int> adj[n+1];
	vector<int> vst(n+1, -1);
	rep(i,0,m) {
		cin >> u >> v;
	queue<int> q;
	while(!q.empty()) {
		int f = q.front();
		for (auto& nb : adj[f]) {
			if (vst[nb] == -1) {
				vst[nb] = f;
	rep(i,2,n+1) {
		if (vst[i] == -1) {
			return 0;
	rep(i,2,n+1) {
		cout << vst[i] << endl;
	return 0;

You are mixing C style IO with C++ style IO, after desyncing cin from stdio.

That worked, thank you.
But, it never caused any problems on Codechef or Codeforces. Should I not use ios_base::sync_with_stdio(false);cin.tie(NULL); for faster IO?

I hope you know what it does. It makes cin and cout faster by desyncing from stdio. If you want to use both, then don’t desync them.

cin.tie(NULL), desyncs cin from cout, which means output won’t be flushed everytime you take an input.

When you desync streams, the IO is faster.

Can somebody please tell the mistake in my code? for same question.

Please help.Someone