cf Codeforces Round #544

原題:

F1. Spanning Tree with Maximum Degree
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an undirected unweighted connected graph consisting of n vertices and m 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 n and m (2≤n≤2⋅10^ 5, n−1≤m≤min(2⋅10^5,n(n−1)/2)) — the number of vertices and edges, respectively.

The following m lines denote edges: edge i is represented by a pair of integers vi, ui (1≤vi,ui≤n, ui≠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,ui) there are no other pairs (vi,ui) or (ui,vi) in the list of edges, and for each pair (vi,ui) the condition vi≠ui is satisfied.

Output

Print n−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) is considered the same as the edge (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

中文:

給你一個無向圖,權值全部爲1,現在讓你找一個最小生成樹,這個生成樹的度數最大(生成樹的最大度就是生成樹中度最大的節點)

代碼:

    #include<bits/stdc++.h>
    using namespace std;
     
    typedef long long ll;
     
    typedef pair<int,int> pii;
     
    const int maxn=200001;
     
    int father[maxn];
    int n,m;
    void init()
    {
        for(int i=1;i<200001;i++)
        {
            father[i]=i;
        }
    }
    int Find(int x)
    {
        if(x!=father[x])
        father[x]=Find(father[x]);
        return father[x];
    }
     
     
    int mark[maxn];
    bool vis[maxn];
    pii vp[maxn];
     
    int main()
    {
        ios::sync_with_stdio(false);
        while(cin>>n>>m)
        {
            int a,b;
            init();
            memset(mark,0,sizeof(mark));
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=m;i++)
            {
                cin>>a>>b;
                mark[a]++;
                mark[b]++;
                vp[i]=make_pair(a,b);
            }
            int ind=1;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]>mark[ind])
                    ind=i;
            }
            for(int i=1;i<=m;i++)
            {
                if(vp[i].first==ind||vp[i].second==ind)
                {
                    if(vp[i].first!=ind)
                        swap(vp[i].first,vp[i].second);
                }
            }
            for(int i=1;i<=m;i++)
            {
                if(vp[i].first==ind)
                {
                    father[Find(vp[i].first)]=Find(vp[i].second);
                    vis[i]=1;
                }
            }
            for(int i=1;i<=m;i++)
            {
                if(Find(vp[i].first)!=Find(vp[i].second))
                {
                    father[Find(vp[i].first)]=Find(vp[i].second);
                    vis[i]=1;
                }
            }
            for(int i=1;i<=m;i++)
            {
                if(vis[i])
                    cout<<vp[i].first<<" "<<vp[i].second<<endl;
            }
     
        }
        return 0;
    }

思路:

很好想到,沒權值的最小生成樹,先找到原始圖中度最大的那個節點,直接先把它扔進最小生成樹中,再把剩下的節點連上就行了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章