Codeforces Contest 1133 problem F2 Spanning Tree with One Fixed Degree ——在图中找出一颗节点1连了k条边的树

82 篇文章 1 订阅

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 degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.

Input
The first line contains three integers n, m and D (2≤n≤2⋅105, n−1≤m≤min(2⋅105,n(n−1)2),1≤D<n) — the number of vertices, the number of edges and required degree of the first vertex, 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
If there is no spanning tree satisfying the condition from the problem statement, print “NO” in the first line.

Otherwise print “YES” in the first line and then print n−1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. 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
inputCopy
4 5 1
1 2
1 3
1 4
2 3
3 4
outputCopy
YES
2 1
2 3
3 4
inputCopy
4 5 3
1 2
1 3
1 4
2 3
3 4
outputCopy
YES
1 2
1 3
4 1
inputCopy
4 4 3
1 2
1 4
2 3
3 4
outputCopy
NO

题意:

大致题意就是标题了,感觉这样写以后自己看博客会非常方便。
就是给你一张图,问你能否在其中找到一棵树,使得这棵树的根节点(节点1)连了d条边

题解:

大致想法就是,for一遍所有与1相连的点,从它开始dfs,如果这个点被访问过了,那么这条就是可有可无的边,如果这个点没被访问过,那么这个点就必须连边:举个例子:
1 2
2 3
1 3
1 4
很明显,1 2是必须的边,1 3 是可有可无的边,1 4是必须的边。之后我们在用并查集看看剩下的边需要连哪些即可。

#include<bits/stdc++.h>
using namespace std;
#define pa pair<int,int>
const int N=2e5+5;
int vis[N];
struct node
{
    int to,next;
}e[N*2];
int cnt,head[N];
void add(int x,int y)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt++;
}
int l[N],r[N];
int fa[N];
int finds(int x)
{
    return x==fa[x]?x:fa[x]=finds(fa[x]);
}
void dfs(int x)
{
    vis[x]=1;
    for(int i=head[x];~i;i=e[i].next)
    {
        if(vis[e[i].to])
            continue;
        dfs(e[i].to);
    }
}
vector<pa>ans;
int used[N];
int main()
{
    memset(head,-1,sizeof(head));
    int n,m,d,x,y;
    scanf("%d%d%d",&n,&m,&d);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=m;i++)
        scanf("%d%d",&x,&y),add(x,y),add(y,x),l[i]=x,r[i]=y;

    vis[1]=1;
    for(int i=head[1];~i;i=e[i].next)
    {
        if(vis[e[i].to])
            continue;
        d--;
        ans.push_back({1,e[i].to});
        fa[e[i].to]=1;
        used[e[i].to]=1;
        dfs(e[i].to);
    }

    for(int i=head[1];~i&&d;i=e[i].next)
    {
        if(used[e[i].to])
            continue;
        ans.push_back({1,e[i].to});
        d--;
        fa[finds(e[i].to)]=1;
    }
    if(d)
        return 0*printf("NO\n");
    printf("YES\n");
    for(int i=1;i<=m;i++)
    {
        if(l[i]==1||r[i]==1)
            continue;
        int ll=finds(l[i]),rr=finds(r[i]);
        if(ll==rr)
            continue;
        ans.push_back({l[i],r[i]});
        fa[ll]=fa[rr];
    }
    for(int i=0;i<ans.size();i++)
        printf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值