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的单播包进行泛洪,在存在环路的情况 ...
随机推荐
- BZOJ3963: [WF2011]MachineWorks
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3963 CDQ分治加凸包.具体看这篇题解:http://www.acmerblog.com/ ...
- 4、Hibenrate中HQL的10中查询方式
二.具体事例如下: 2.0 编写如下sql语句 )); create sequence seq_teacher; insert into teacher values(seq_teacher.next ...
- Linux目录结构及快捷键
1.树形目录结构 2.最顶层:根目录 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动配置文件和脚本 /home 用户主目录的基点 /lib ...
- LeetCode OJ 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)
输出函数:这个函数别看它小,但浓缩的都是精华啊 作用:对于高精度的数组进行倒序输出 思路:首先从被传入的数组第一位开始,一直往前扫输出就可以了(i--) 注释:因为每个数组的第一位是用来存储这个数组的 ...
- 将数据库的数据导入solr索引库中
在solr与tomcat整合文章中,我用的索引库是mycore,现在就以这个为例. 首先要准备jar包:solr-dataimporthandler-4.8.1.jar.solr-dataimport ...
- UIButton的属性设置
1.背景颜色 btn.backgroundColor = [UIColor redColor]; 2.给按钮添加文字并添加显示状态 [btn setTitle@"播放" forS ...
- java中的Volatile 变量
Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...
- AJAx 刷新页面
<html><head> <meta http-equiv="Content-Type" content="text/html; chars ...
- NGUI等比缩放
/// <summary> /// UI 等比缩放 /// </summary> static private void AdaptiveUI() { ; ; UIRoot u ...