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 ...
随机推荐
- 详解ListView加载网络图片的优化
我们来了解一些ListView在加载大量网络图片的时候存在的常见问题: 1.性能问题,ListView的滑动有卡顿,不流畅,造成非常糟糕的用户体验. 2.图片的错位问题. 3.图片太大,加载Bitma ...
- java有关Time类型数据的接收和转换
一:前言 有关Time的时间其实很少有用到.但是用到就很纠结了,转换和保存,都是烦人的事情,我自己就在这上面吃过一个亏,所以就加载下来吧! 二:内容 (1):被坑的地方 实体类 import java ...
- Spring 学习笔记 整合 Struts2
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spri ...
- python模拟android屏幕高频点击工具
一.环境 windows 10 + python3.6 二.需求 1.模拟android设备高频点击事件: 2.模拟规定次数的点击事件或模拟规定时间内的点击事件: 三.code 1.模拟规定时间内的 ...
- .net core Fundamentals
• Application Startup 應用程序啟動 • Middleware 中間件 • Working with Static Files 靜態文件 • Routing 路由 • URL Re ...
- free命令buff和cache的区别
[root@iz8vb8kca7twx6177bqg0fz ~]# free -h total used free shared b ...
- bzoj 2200: [Usaco2011 Jan]道路和航线——拓扑+dijkstra
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- 【BZOJ】1229 [USACO2008 Nov]toy 玩具
[算法]三分+贪心 [题解] 数据范围小的版本:餐巾计划 这题不是使用最小费用流的下凸函数,因为这题是要满足最大流,那么这题到底在三分什么? 三分的这个函数,其实是总费用随卖出玩具量变化而变化的函数, ...
- MYSQL查找总结
a.条件判断where select * from 表 where id > 1 and name != 'alex' and num = 12; select * from 表 where ...
- Spring Cloud的基本认识和使用Spring Cloud的基本教程(山东数漫江湖)
认识Spring Cloud Spring Cloud简单来说就是一个微服务相关的框架,至于什么是微服务,简单来说就是一个整体项目由多个单独运行的小项目构成,每个小项目负责一个或多个功能,每个小项目有 ...