F - Spanning Tree with Maximum Degree CodeForces - 1133F1 (bfs)

You are given an undirected unweighted connected graph consisting of nn vertices and mm edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.

Your task is to find any spanning tree of this graph such that the maximum degree over all vertices is maximum possible. Recall that the degree of a vertex is the number of edges incident to it.

Input

The first line contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤min(2⋅105,n(n−1)2)n−1≤m≤min(2⋅105,n(n−1)2)) — the number of vertices and edges, respectively.

The following mm lines denote edges: edge ii is represented by a pair of integers vivi, uiui (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠vi), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (vi,uivi,ui) there are no other pairs (vi,uivi,ui) or (ui,viui,vi) in the list of edges, and for each pair (vi,ui)(vi,ui) the condition vi≠uivi≠ui is satisfied.

Output

Print n−1n−1 lines describing the edges of a spanning tree such that the maximum degree over all vertices is maximum possible. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v,u)(v,u) is considered the same as the edge (u,v)(u,v)).

If there are multiple possible answers, print any of them.

Examples

Input

5 5
1 2
2 3
3 5
4 3
1 5

Output

3 5
2 1
3 2
3 4

Input

4 6
1 2
1 3
1 4
2 3
2 4
3 4

Output

4 1
1 2
1 3

Input

8 9
1 2
2 3
2 5
1 6
3 4
6 5
4 5
2 7
5 8

Output

3 2
2 5
8 5
6 1
2 7
1 2
3 4

Note

Picture corresponding to the first example: 

In this example the number of edges of spanning tree incident to the vertex 33 is 33. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer.

Picture corresponding to the second example: 

In this example the number of edges of spanning tree incident to the vertex 11 is 33. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer.

Picture corresponding to the third example: 

In this example the number of edges of spanning tree incident to the vertex 22 is 44. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer. But because this example is symmetric, we can choose almost the same spanning tree but with vertex 55 instead of 22.


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/    //博客园
//https://blog.csdn.net/qq_44134712     //csdn

using namespace std;

typedef long long ll;
const ll N=2e5+10;
const int mod=1000000007;

vector<int>n[N];
int flag[N];
int a,b;

void bfs(int t){
    queue<int>q;
    memset(flag,0,sizeof(flag));
    q.push(t);
    flag[t]=1;
    while(!q.empty()){
        int g=q.front();
        q.pop();
        for(int i=0;i<n[g].size();i++){
            if(flag[n[g][i]]==0){
                cout << g << " " << n[g][i] << endl;
                flag[n[g][i]]=1;
                q.push(n[g][i]);
            }
        }
    }
}

int main()
{
    int x,y;
    cin >> a >> b;
    for(int i=1;i<=b;i++){
        cin >> x >> y;
        n[x].push_back(y);
        n[y].push_back(x);
    }
    int ma=0;
    int t=0;
    for(int i=1;i<=a;i++){
        if(ma<n[i].size()){
            ma=n[i].size();
            t=i;
        }
    }
    bfs(t);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值