Problem p02678 (python)

Fixed submission:
from collections import defaultdict

class que(object):
    def __init__(self):
        self.item = []

    def push(self,x):
        self.item.append(x)
    
    def front(self):
        return self.item[0]
    
    def pop(self):
        self.item.pop(0)
    
    def empty(self):
        return len(self.item) == 0

n,m = map(int , input().split())
path = defaultdict(list)

ans = [-1 for i in range(n)]

for i in range(m):
    a,b = map(int, input().split())
    path[a-1].append(b-1)
    path[b-1].append(a-1)

q = que()

q.push(0)

ans[0] = 0

while not q.empty():
    now = q.front()
    q.pop()


    for tmp in path[now]:
        # print(tmp, now)
        if tmp == now:
            continue
        
        if ans[tmp] != -1:
            continue
            
        else:
            ans[tmp] = now
            q.push(tmp)
    # print(now)

# print(ans)

for tmp in ans:
    if tmp == -1:
        print("No")
        exit()

print("Yes")

for tmp in ans[1:]:
    
    print(tmp+1)

Buggy submission (verdict: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

//マクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 //10^12
#define MOD 10000007 //10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main(int argc, char const *argv[]) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> path(n);
    int ans[n] = {-1}; // 距離を格納するリスト
    REP(i,n)
        ans[i] = -1;
    
    queue<int> que;
    
    REP(i,m) 
    {
        int a,b;
        cin >> a >> b;
        path[a-1].PB(b-1);
        path[b-1].PB(a-1);
    }
    que.push(0);

    // cout << endl;
    // REP(i,SIZE(path))
    // {   
    //     cout << i << " ";
    //     REP(j,SIZE(path[i]))
    //     {
    //         cout << path[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    ans[0] = 0;
    while (!que.empty())
    {
        int now = que.front(); //現在の場所
        que.pop();
        // cout << "here is top of roop" << endl;
        // cout << "que size" << que.size() << endl; 
        // cout << "now" << now  << endl;
        

        // if (que.size() > 100)
        //     break;

        REP(i,SIZE(path[now]))
        {
            int tmp = path[now][i];
            if (tmp == now) continue;
            if (ans[tmp] != -1)
                continue;
            else
            {
                ans[tmp] = now;
                que.push(tmp);
            }
            // 個々提出前に米
            // cout << "This is test " << now << " " << tmp << endl;
            // REP(i,n)
            //     cout << ans[i] << " ";
            // cout << endl;
        }
    }

    REP(i,n)
    {
        if (ans[i] == -1)
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    
    FOR(i,1,n-1)
        cout << ans[i]+1 << endl;

    return 0;
}