st-Spanning Tree
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an undirected connected graph consisting of n vertices and m edges. There are no loops and no multiple edges in the graph.

You are also given two distinct vertices s and t, and two values ds and dt. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex s doesn't exceed ds, and the degree of the vertex t doesn't exceed dt, or determine, that there is no such spanning tree.

The spanning tree of the graph G is a subgraph which is a tree and contains all vertices of the graph G. In other words, it is a connected graph which contains n - 1 edges and can be obtained by removing some of the edges from G.

The degree of a vertex is the number of edges incident to this vertex.

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ min(400 000, n·(n - 1) / 2)) — the number of vertices and the number of edges in the graph.

The next m lines contain the descriptions of the graph's edges. Each of the lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v) — the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.

The last line contains four integers stdsdt (1 ≤ s, t ≤ ns ≠ t, 1 ≤ ds, dt ≤ n - 1).

Output

If the answer doesn't exist print "No" (without quotes) in the only line of the output.

Otherwise, in the first line print "Yes" (without quotes). In the each of the next (n - 1) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.

You can output edges in any order. You can output the ends of each edge in any order.

If there are several solutions, print any of them.

Examples
input
3 3
1 2
2 3
3 1
1 2 1 1
output
Yes
3 2
1 3
input
7 8
7 4
1 3
5 4
5 7
3 2
2 4
6 1
1 2
6 4 1 4
output
Yes
1 3
5 7
3 2
7 4
2 4
6 1
分析:根据贪心思想,先把不含s,t联通的联通块连上;
   然后把和s相连却不和t相连的联通块加入s,把和t相连却不和s相连的联通块加入t;
   然后对于和s和t都相连的联通块依次判断加入即可,最后看s和t是否联通及s和t是否直连即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=4e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,s,ds,dt,vis[maxn],pos1[maxn],pos2[maxn],cnt,ans1,ans2;
vi e[maxn];
bool f1[maxn],f2[maxn],ok,flag,ca;
vector<pii>ans;
void dfs(int now)
{
vis[now]=cnt;
for(int x:e[now])
{
if(x!=s&&x!=t&&!vis[x])
{
ans.pb(mp(now,x));
dfs(x);
}
else if(x==s)
{
f1[cnt]=true;
pos1[cnt]=now;
}
else if(x==t)
{
f2[cnt]=true;
pos2[cnt]=now;
}
}
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&j,&k);
e[j].pb(k),e[k].pb(j);
}
scanf("%d%d%d%d",&s,&t,&ds,&dt);
rep(i,,n)
{
if(i!=s&&i!=t&&!vis[i])cnt++,dfs(i);
}
rep(i,,cnt)
{
if(f1[i]&&!f2[i])ans.pb(mp(s,pos1[i])),ans1++;
else if(!f1[i]&&f2[i])ans.pb(mp(t,pos2[i])),ans2++;
}
if(ans1>=ds||ans2>=dt)puts("No");
else
{
ok=true;
rep(i,,cnt)
{
if(f1[i]&&f2[i])
{
flag=true;
if(flag&&ok)
{
ans.pb(mp(s,pos1[i])),ans1++;
ans.pb(mp(t,pos2[i])),ans2++;
ok=false;
}
else
{
if(ans1<ds)ans.pb(mp(s,pos1[i])),ans1++;
else if(ans2<dt)ans.pb(mp(t,pos2[i])),ans2++;
else
{
flag=false;
break;
}
}
}
}
for(int x:e[s])if(x==t){ca=true;break;}
if(!flag&&ca&&ans1<ds&&ans2<dt)flag=true,ans.pb(mp(s,t));
if(!flag)puts("No");
else
{
puts("Yes");
for(pii x:ans)printf("%d %d\n",x.fi,x.se);
}
}
//system("Pause");
return ;
}

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. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  3. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  4. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  5. Codeforces Edu3 E. Minimum spanning tree for each edge

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. CF# Educational Codeforces Round 3 E. 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 ...

  7. 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 ...

  8. MST(Kruskal’s Minimum Spanning Tree Algorithm)

    You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...

  9. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. STP(Spanning Tree Protocol)

    STP生成树协议   问题 为了提高网络的可用性,需要进行冗余和备份.但是冗余路径会产生环路 环路会导致以下问题 广播风暴:由于交换机会对广播.多播.和未知目标MAC的单播包进行泛洪,在存在环路的情况 ...

随机推荐

  1. 遮盖层实现(jQuery+css+html)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. createElement创建

    定义和用法 createElement() 方法可创建元素节点. 此方法可返回一个 Element 对象. <script type="text/javascript"> ...

  3. Java 多态 父类和子类方法的访问控制权限

    Java 多态 父类和子类方法的访问控制权限 @author ixenos 父类和子类方法的访问控制权限 继承是为了扩展类的功能,而这种扩展显然就是对一个原始类的扩展,目的还是向上转型来调用,所以这就 ...

  4. mysql导出数据表结构,必须退出mysql命令.重新使用msyqldump命令

    只导出数据库中所有表结构(-d 减去数据) 导出所有表结构和数据 mysqldump -uroot --default-character-set=utf8 -p123-d必须空格good>H: ...

  5. Objetive-C 中的相等比较

    1.== 用于比较两个对象的地址是否相同 1)需要注意的是相同的短字符串,一定大小整数(nsnumber),Objetive-C 底层会做cache,两个对象,指向同一个地址. 例如: NSStrin ...

  6. Docker技术学习

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog.51cto.com/353572/1598249 貌似Doc ...

  7. 监控自定义信息 —— ESFramework 4.0 快速上手(10)

    在ESFramework 4.0 进阶(02)-- 核心:消息处理的骨架流程一文中,我们介绍了通过挂接IMessageSpy到骨架流程,我们就可以监控到所有收发的消息.由于Rapid引擎已经为我们组装 ...

  8. #include 和 #pragma comment 的相对路径起点

    #include 是以当前文件所在路径为当前目录 #pragma comment 是以当前工程所在路径为当前目录 #include "../../../../ThirdParty/Inclu ...

  9. MFC HTTP

    CInternetSession m_winet(NULL,,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,); CHttpConnection *pConnection; ...

  10. velocity的宏

    velocity中的宏macro的使用当中,由于velocity会将宏加载到tomcat中去,但是如果修改之后再加载的话velocity发现有了相同的宏名称,则不会加载 所以这时候的问题就是,在页面上 ...