$dfs$,构造。

类似于$k$度限制生成树的想法,可以将$s$和$t$先从图中删去,将剩下的部分求连通块,每个连通块内部很容易构造生成树,每个连通块缩成一个点来处理。

连通块分三种:

$1$.只与$s$有边

$2$.只与$t$有边

$3$.与$s$和$t$都有边

前两种没办法,只能和$s$和$t$相连。如果没有第三种,那么$s$和$t$之前需要连一条边。如果有第三种,在第三种里面选出一个来和$s$、$t$连,其余的当做第一种和第二种处理。

连边的过程中判断$s$和$t$的度是否满足条件即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} struct Edge
{
int a,b,nx;
}e[];
int h[];
int n,m,sz,s,t,ds,dt;
int belong[],block;
vector<int>ansx,ansy; struct X
{
int e1,e2;
}w[]; set<int>SS,TT; void add(int a,int b)
{
e[sz].a=a; e[sz].b=b; e[sz].nx=h[a]; h[a]=sz++;
} void dfs(int x)
{
belong[x]=block;
for(int i=h[x];i!=-;i=e[i].nx)
{
int to=e[i].b;
if(belong[to]!=) continue;
if(to==s) continue;
if(to==t) continue; ansx.push_back(x);
ansy.push_back(to); dfs(to);
}
} int main()
{
cin>>n>>m; memset(h,-,sizeof h);
for(int i=;i<=m;i++)
{
int a,b; cin>>a>>b;
add(a,b); add(b,a);
}
cin>>s>>t>>ds>>dt; for(int i=;i<=n;i++)
{
if(i==s) continue;
if(i==t) continue;
if(belong[i]!=) continue;
block++; dfs(i);
} for(int i=;i<=block;i++) w[i].e1=w[i].e2=-; for(int i=;i<sz;i=i+)
{
if(e[i].a==s&&(e[i].b!=s&&e[i].b!=t))
{
SS.insert(belong[e[i].b]);
if(w[belong[e[i].b]].e1==-) w[belong[e[i].b]].e1=i;
}
if(e[i].b==s&&(e[i].a!=s&&e[i].a!=t))
{
SS.insert(belong[e[i].a]);
if(w[belong[e[i].a]].e1==-) w[belong[e[i].a]].e1=i;
}
if(e[i].a==t&&(e[i].b!=s&&e[i].b!=t))
{
TT.insert(belong[e[i].b]);
if(w[belong[e[i].b]].e2==-) w[belong[e[i].b]].e2=i;
}
if(e[i].b==t&&(e[i].a!=s&&e[i].a!=t))
{
TT.insert(belong[e[i].a]);
if(w[belong[e[i].a]].e2==-) w[belong[e[i].a]].e2=i;
}
} int sum=; vector<int>tmp;
for(int i=;i<=block;i++)
{
if(SS.count(i)&&TT.count(i)) { tmp.push_back(i); continue; }
if(SS.count(i))
{
ansx.push_back(e[w[i].e1].a);
ansy.push_back(e[w[i].e1].b);
ds--;
}
else
{
ansx.push_back(e[w[i].e2].a);
ansy.push_back(e[w[i].e2].b);
dt--;
}
} if(tmp.size()==)
{
ansx.push_back(s);
ansy.push_back(t);
ds--; dt--;
} else
{
ansx.push_back(e[w[tmp[]].e1].a);
ansy.push_back(e[w[tmp[]].e1].b);
ansx.push_back(e[w[tmp[]].e2].a);
ansy.push_back(e[w[tmp[]].e2].b); ds--; dt--; for(int i=;i<tmp.size();i++)
{
if(ds>)
{
ansx.push_back(e[w[tmp[i]].e1].a);
ansy.push_back(e[w[tmp[i]].e1].b);
ds--;
}
else if(dt>)
{
ansx.push_back(e[w[tmp[i]].e2].a);
ansy.push_back(e[w[tmp[i]].e2].b);
dt--;
}
} } if(ds<||dt<||ansx.size()!=n-) printf("No\n");
else
{
printf("Yes\n");
for(int i=;i<ansx.size();i++)
printf("%d %d\n",ansx[i],ansy[i]);
} return ;
}

CodeForces 723F st-Spanning Tree的更多相关文章

  1. codeforces 609E Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  2. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  3. 【codeforces 723F】st-Spanning Tree

    [题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过 ...

  4. codeforces 723F : st-Spanning Tree

    Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...

  5. codeforces 609E. Minimum spanning tree for each edge 树链剖分

    题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...

  6. Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树

    好久没更新博客了,一直懒得动,这次更新一下. 题意大概是:给出一个图,求它的一个一号节点的度数恰好为D的生成树的方案. 一开始随便水了个乱搞贪心,不出意外并没有过. 仔细思考之后,对于这个问题我们可以 ...

  7. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  8. CodeForces 618D Hamiltonian Spanning Tree

    题意:要把所有的节点都访问一次,并且不能重复访问,有两种方式访问,一种是根据树上的路径 走和当前节点连接的下一个节点cost x, 或者可以不走树上边,直接跳到不与当前节点连接的节点,cost y 分 ...

  9. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  10. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

随机推荐

  1. ZooKeeper入门(四)

    入门:使用ZooKeeper的协调分布式应用 这个文档使你对ZooKeeper快速入门,它主要针对想尝试它的开发者.并且包含简单的单机的ZooKeeper服务的安装说明,一些验证是否运行的命令,和一个 ...

  2. Strand Sort

    Strand sort是思路是这样的,它首先需要一个空的数组用来存放最终的输出结果,给它取个名字叫"有序数组" 然后每次遍历待排数组,得到一个"子有序数组",然 ...

  3. jenkins slave agent 当作服务运行

    1. 接上边编辑好文件 2. 双击以上的jnlp文件 3. 点击弹出的窗口File->save as service, 此时如果报错的话很可能是由于没有安装.net(.net2 以上) 4. 保 ...

  4. python学习笔记(三)之变量和字符串

    在其他语言中,变量就是有名字的存储区,可以将值存储在变量中,也即内存中.在Python中略有不同,python并不是将值存储在变量中,更像是把名字贴在值上边.所以,有些python程序员会说pytho ...

  5. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  6. javascript中=、==与===的区别

    1.等号 =赋值运算符,给变量赋值 var a="1"; 2.相等和不相等操作符 相等操作符由==表示,若两个操作数相等,则返回true:不相等操作符由!=表示,若两个操作数不相等 ...

  7. flask_返回字节流错误

    # flask_返回字节流错误 def export_data(filename, fields, data, names=None, sheet='Sheet1'): # fields 为list ...

  8. POJ1014(多重背包)

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65044   Accepted: 16884 Descri ...

  9. python基础===monkeytype可以自动添加注释的模块!

    monkeytype 一个可以自动添加注释的模块! 先要下载: pip install monkeytype 以官网的sample code为例 #moudle.py def add(a, b): r ...

  10. mount/umount命令【转】

    转自:http://www.cnblogs.com/qq78292959/archive/2012/03/06/2382334.html 如果想在运行的Linux下访问其它文件系统中的资源的话,就要用 ...