CodeForces 723F st-Spanning Tree
$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的更多相关文章
- 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 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- 【codeforces 723F】st-Spanning Tree
[题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过 ...
- codeforces 723F : st-Spanning Tree
Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...
- codeforces 609E. Minimum spanning tree for each edge 树链剖分
题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...
- Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树
好久没更新博客了,一直懒得动,这次更新一下. 题意大概是:给出一个图,求它的一个一号节点的度数恰好为D的生成树的方案. 一开始随便水了个乱搞贪心,不出意外并没有过. 仔细思考之后,对于这个问题我们可以 ...
- Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)
题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...
- CodeForces 618D Hamiltonian Spanning Tree
题意:要把所有的节点都访问一次,并且不能重复访问,有两种方式访问,一种是根据树上的路径 走和当前节点连接的下一个节点cost x, 或者可以不走树上边,直接跳到不与当前节点连接的节点,cost y 分 ...
- 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 ...
- 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 ...
随机推荐
- 51Nod 1031 骨牌覆盖 | Fibonacci
Input 输入N(N <= 1000) Output 输出数量 Mod 10^9 + 7 Input示例 3 Output示例 3 思路:对于第x块骨牌的情况,我们用a[x]表示其方法数:其比 ...
- 【设计模式】 模式PK:抽象工厂模式VS建造者模式
1.概述 抽象工厂模式实现对产品家族的创建,一个产品家族是这样的一系列产品:具有不同分类维度的产品组合,采用抽象工厂模式则是不需要关心构建过程,只关心什么产品由什么工厂生产即可.而建造者模式则是要求按 ...
- redis cluster以及master-slave在windows下环境搭建
一.redis cluster环境搭建: 1.了解Redis Cluster原理: 详细了解可参考:http://doc.redisfans.com/topic/cluster-tutorial.ht ...
- No cached version of ..... available for offline mode.
I had same error...Please Uncheck the offline work in Settings. File => Settings => Build, Exe ...
- WinRAR分割超大文件
在自己的硬盘上有一个比较大的文件,想把它从网上通过E-Mail发送给朋友时,却发现对方的收信服务器不能够支持那么大的文件……,这时即使用ZIP等压缩软件也无济于事,因为该文件本身已经被压缩过了.于是许 ...
- 设计模式之Builder
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) 介绍: Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象. 将一个复杂 ...
- vuejs怎么在服务器部署?
通过npm run build 把生成的dist文件夹(不要上传文件夹)里的内容上传到http服务器上就可以通过 http来访问了,开发机上正常,上传以后 程序出现错误不能运行的原因99.99%的可能 ...
- C++之复制控制
只有单个形参,而且该形参是对本类类型对象的引用(常用const修饰),这样的构造函数叫做复制构造函数(有时也称为拷贝构造函数),例如: class Person{ public: Person();/ ...
- Linux 内核通知链随笔【中】【转】
转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...
- 自己动手一步步安装Linux系统
自己动手一步步安装Linux系统 http://502245466.blog.51cto.com/7559397/1291910/