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 ;
}
最新文章
- php读取大文件
- MVP 个人理解2
- Client默认用户及登录密码(转)
- HTTP响应状态码记录
- jmeter 构建一个LDAP测试计划
- Datediff函数 助你实现不同进制时间之间的运算
- #DP# ----- OpenJudge数字组合
- QT Creator 快速入门教程 读书笔记(二)
- springMVC+commons-fileupload上传文件大小限制异常
- JavaScript中创建对象的几种模式
- 文献笔记:Genome-wide associations for birth weight and correlations with adult disease
- jQuery使用(五):DOM操作之插入和删除元素
- js中的new Option默认选中
- chardet查看字符串的编码(非常好用)
- A - The Water Bowls POJ - 3185 (bfs||高斯消元)
- Linux成长之路
- redis和mongodb的比较
- sql group by max
- [转]VS2015+OpenCV3.3 GPU模块和opencv_contrib模块的编译以及采用CMake编译opencv_contrib时提示“No extra modules found in folder”问题的解决方案
- Objective-C 语法之 Debug 表达式
热门文章
- Spark开发指南
- 导入excel表格的数据--->;到mysql中
- 如何自定义JSR-303标准的validator
- Linux Root密码忘记怎么办?用Vultr vps教程演示重置root密码
- xampp 搭建 web mac上
- Openjudge-计算概论(A)-短信计费
- 动态封杀与解封IP
- 转 shell awk 使用详解
- 使用 NSUserDefaults 读取和写入自定义对象
- The server instance Witness rejected configure request; read its error log file for more information. The reason 1427, and state 31, can be of use for