st-Spanning Tree
4 seconds
256 megabytes
standard input
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.
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 ≤ n, u ≠ 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 s, t, ds, dt (1 ≤ s, t ≤ n, s ≠ t, 1 ≤ ds, dt ≤ n - 1).
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.
3 3
1 2
2 3
3 1
1 2 1 1
Yes
3 2
1 3
7 8
7 4
1 3
5 4
5 7
3 2
2 4
6 1
1 2
6 4 1 4
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的更多相关文章
- 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 ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- STP(Spanning Tree Protocol)
STP生成树协议 问题 为了提高网络的可用性,需要进行冗余和备份.但是冗余路径会产生环路 环路会导致以下问题 广播风暴:由于交换机会对广播.多播.和未知目标MAC的单播包进行泛洪,在存在环路的情况 ...
随机推荐
- 3DTouch--2
苹果太贱了! 3D Touch 只能在真机上面试,模拟器没办法玩! ------------- 描述有点粗燥.....有6S 在手上玩得童鞋会更加清楚,只有玩过才更加体验到. 首先 有几个要知道的手势 ...
- 【Python@Thread】threading模块
theading模块的Thread类 属性: name 线程名 ident 线程标识符 daemon 布尔值,标示是否为守护线程 方法: __init__(target=None, name=Non ...
- 对于querystring取值时候发生+号变空格的问题
今天遇到这个问题,在网上找了几个答案,解决了问题,很高兴,所以贴出来给大家分享一下: URL如下 http://127.0.0.1/test/test.aspx?sql= and id='300+' ...
- freemarker之list和map
第一次使用freemarker很不习惯,之前都是用velocity的. @RequestMapping("/free.htm") public ModelAndView hello ...
- 浅谈SharePoint 2013 站点模板开发 转载自http://www.cnblogs.com/jianyus/p/3511550.html
一直以来所接触的SharePoint开发,都是Designer配合Visual Studio,前者设计页面,后者开发功能,相互合作,完成SharePoint网站开发.直到SharePoint 2013 ...
- EF Unit Of Work
BaseEntity, 所有的业务表都继承这个类,每个表的主键都是GUID, 主键名Id. public abstract class BaseEntity{ public BaseEntity() ...
- Javaweb 第4 天xml 课程
XML课程 今日大纲 ● XML概念 ● XML基本语法 ● XML约束 ● dom4j技术 ● xpath技术 ******************************************* ...
- OA系统出现窗口拦截的解决办法
我们使用oa时候,有时候会出现“你打开了窗口拦截功能”.如图 出现窗口被拦截主要有三种情况,分别是IE浏览器本身拦截功能.第三方插件(如百度工具栏.搜搜工具栏.谷歌工具栏等).第三方浏览器拦截功能(如 ...
- CSS3秘笈:第十章
CSS的transform.transition和animation属性 1.transform(倾斜):利用transform属性可以使导航栏稍微倾斜,或者使图片在访问者的鼠标经过它时放大两倍,甚至 ...
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)
Java1.5泛型指南中文版(Java1.5 Generic Tutorial) 英文版pdf下载链接:http://java.sun.com/j2se/1.5/pdf/generics-tutori ...