CodeForces#375 - F-st-Spanning Tree 构造

构造题

给一个n点m边的图

要求构造一个生成树,满足s点和t点的度不超过 ds,dt


1把除去s和t之外的点 缩点,得到一些联通块

要么只和s连或只和t连,要么和两者都连

首先把只和s或t相连的联通块都分别和s,t相连

对于剩下的,s还是t的度数不超过的前提下随便连。

最后要记得判断一下s和t是否已经连起来了

整个图的联通


注意不要连不存在的边,要记录一下某个联通块于s或t的借口


 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
typedef long long  ll;
const int N=400000+50;
const int M=400000+50;
vector<int>mp[N];
struct node
{
    int x,y;
    node() {}
    node(int a,int b)
    {
        x=a,y=b;
    }
};
node tt[M];
int fa[N];
int find(int x)
{
    if (x==fa[x])return x;
    return fa[x]=find(fa[x]);
}
int newmap[N][2];
int con_pos[N][2];
int flag=0; // st connect with ed
int du[2];
int vis[N];
vector<node>ans;
int has_edge=0;
int main()
{
    int n,m;
    int u,v;
    cin>>n>>m;
    for (int i=1; i<=m; i++)
    {
        scanf("%d%d",&u,&v);
        tt[i]=node(u,v);

    }
    int st,ed,dst,ded;
    scanf("%d%d%d%d",&st,&ed,&dst,&ded);

    for (int i=1; i<=n; i++) fa[i]=i;
    for (int i=1; i<=m; i++)
    {
        int x=tt[i].x,y=tt[i].y;
        if (x==st||x==ed)continue;
        if (y==st||y==ed)continue;
        int fx=find(x);
        int fy=find(y);
        if (fx!=fy)
        {
            fa[fx]=fy;
            ans.push_back(node(x,y));       //注意不要连不存在的边
        }
    }
    for (int i=1; i<=m; i++)
    {
        int x=tt[i].x,y=tt[i].y;
        if (x==st&&y==ed)
        {
            has_edge=1;
            continue;
        }
        if (y==st&&x==ed)
        {
            has_edge=1;
            continue;
        }
        int fy=find(y);
        int fx=find(x);
        if (x==st)  newmap[fy][0]=1,con_pos[fy][0]=y;       //记录连接点的位置
        if (y==st)  newmap[fx][0]=1,con_pos[fx][0]=x;
        if (x==ed)  newmap[fy][1]=1,con_pos[fy][1]=y;
        if (y==ed)  newmap[fx][1]=1,con_pos[fx][1]=x;
    }
    int unconnect=0;
    for (int i=1; i<=n; i++)
    {
        int x=i;
        int fx=find(x);
        if (fx!=x) continue;
        if (x==st||x==ed)continue;
        if (newmap[fx][0]&&!newmap[fx][1])
        {
            du[0]++;
            vis[fx]=1;
            ans.push_back(node(st,con_pos[fx][0]));
            continue;
        }
        else if (newmap[fx][1]&&!newmap[fx][0])
        {
            du[1]++;
            vis[fx]=1;
            ans.push_back(node(ed,con_pos[fx][1]));
            continue;
        }
        else if (newmap[fx][1]&& newmap[fx][0]);
        else unconnect=1;       //不和s又不和t连的联通块,无法生成树

    }
    if (unconnect)//不和s又不和t连的联通块,无法生成树
    {
        printf("No\n");
        return 0;
    }
    int ok=1;
    for (int i=1; i<=n; i++)
    {
        int x=i;
        int fx=find(x);
        if (fx!=x) continue;
        if (x==st||x==ed)continue;
        if (vis[fx])continue;
        if (!flag)          //s与t还未连起来
        {
            flag=1;
            du[0]++,du[1]++;
            vis[fx]=1;
            ans.push_back(node(ed,con_pos[fx][1]));
            ans.push_back(node(st,con_pos[fx][0]));
        }
        else if (du[0]<dst)
        {
            du[0]++;
            vis[fx]=1;
            ans.push_back(node(st,con_pos[fx][0]));
        }
        else if (du[1]<ded)
        {
            du[1]++;
            vis[fx]=1;
            ans.push_back(node(ed,con_pos[fx][1]));
        }
        else                //联通块无法与s或t连起来,度数不满足
            ok=0;
    }
    if (!flag)  //s与t还未连起来
    {
        if (has_edge)  //s与t有边
        {
            du[0]++,du[1]++;
            flag=1;
            ans.push_back(node(st,ed));
        }
        else
        {
            printf("No\n");
            return 0;
        }
    }
    if (du[0]<=dst&&du[1]<=ded&&flag&&ok)
    {
        printf("Yes\n");
        for (int i=0; i<ans.size(); i++)
            printf("%d %d\n",ans[i].x,ans[i].y);
    }
    else
        printf("No\n");



    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值