传送门

分析:构造题。可以这么想:先把s,t两个点去掉,把剩下的点先并查集合并。这样会出现N+2个集合:s, t, N个剩余集合。那么N个集合中先把只能与s或t中一个相连的连起来,如果这样已经超出了要求,那么就不能构造。剩余的既能和s又能和t相连的集合就按照不超过ds,dt这两个要求相连,可以则Yes,否则为No。这样有一个特殊情况:就是s或者t可能只有一条边连向t和s,不知道有没有说清楚,就是对于s只有s−t或者对于t只有s−t。这个需要在最后进行特判。

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
int par[maxn];
int s, t, ds, dt;
bool inq[maxn];
int cons[maxn], cont[maxn];
std::vector<pii> vis;
std::vector<int> conn;
std::vector<int> G[maxn];
int N, M; void init() {
for (int i = 0; i <= N; i ++)
par[i] = i;
}
int findpar(int x) {
return par[x] = (par[x] == x ? x : findpar(par[x]));
}
void unite(int x, int y) {
int px = findpar(x), py = findpar(y);
if (px == py) return;
// cout<<x<<" "<<y<<endl;
vis.pb(mk(x, y));
par[px] = py;
}
bool work() {
init();
for (int u = 1; u <= N; u ++) {
if (u == s || u == t) continue;
for (unsigned i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (v == s || v == t) continue;
unite(u, v);
}
}
mem(inq, false);
for (int u = 1; u <= N; u ++) {
if (u == s || u == t) continue;
int p = findpar(u);
if (!inq[p]) {
conn.pb(p);
inq[p] = true;
}
}
mem(cons, -1); mem(cont, -1);
bool st = false;
for (unsigned i = 0; i < G[s].size(); i ++) {
int v = G[s][i];
if (v == t) {st = true; continue;}
int p = findpar(v);
cons[p] = v;
}
for (unsigned i = 0; i < G[t].size(); i ++) {
int v = G[t][i];
if (v == s) {st = true; continue;}
int p = findpar(v);
cont[p] = v;
}
int anss = 0, anst = 0;
for (unsigned i = 0; i < conn.size(); i ++) {
int p = conn[i];
if (cons[p] != -1 && cont[p] != -1) continue;
if (cons[p] == -1 && cont[p] == -1) return false;
if (cons[p] != -1 && cont[p] == -1) {
unite(s, cons[p]);
anss ++;
}
if (cons[p] == -1 && cont[p] != -1) {
unite(t, cont[p]);
anst ++;
}
}
if (anss >= ds || anst >= dt) return false;
ds -= anss, dt -= anst;
bool flag = true;
for (unsigned i = 0; i < conn.size(); i ++) {
int p = conn[i];
if (!(cons[p] != -1 && cont[p] != -1)) continue;
if (flag) {
ds --, dt --;
unite(s, cons[p]);
unite(t, cont[p]);
flag = false;
}
else {
if (ds) {
ds --;
unite(s, cons[p]);
}
else if (dt) {
dt --;
unite(t, cont[p]);
}
else return false;
}
}
int ps = findpar(s), pt = findpar(t);
if (ps != pt) {
if (ds && dt && st) unite(s, t);
else return false;
}
return true;
} int main(int argc, char const *argv[]) {
cin>>N>>M;
for (int i = 0; i < M; i ++) {
int u, v; cin>>u>>v;
G[u].pb(v); G[v].pb(u);
}
cin>>s>>t>>ds>>dt;
if (work()) {
puts("Yes");
for (unsigned i = 0; i < vis.size(); i ++)
printf("%d %d\n", vis[i].fi, vis[i].se);
}
else puts("No");
return 0;
}

Codeforces Round #375 (Div. 2) F. st-Spanning Tree的更多相关文章

  1. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  7. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  8. Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)

    https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...

  9. Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula

    F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...

随机推荐

  1. Sql Server 中一个非常强大的日期格式化函数

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0)-- 05 16 2006 10:57AMSelect CONV ...

  2. mysql 修改密码

    安装完成后,默认本地登录超级管理员root,密码为空连接服务后(先停止服务,在wamp中运行mysql控制台enterpass:直接回车),执行set password for root@localh ...

  3. Codeforces Round #342 (Div. 2)-A. Guest From the Past

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. TTradmin v1.1 - 免端口映射穿透任何内网、基于radmin核心的即时远程协助

        TTradmin 是一款免端口映射可直接穿透任何内网,基于radmin核心的即时远程协助软件.在使用的时候只需要保证“协助端”和“被协助端”使用同一个验证码即可实现安全便捷的远程控制,不需要进 ...

  5. mysql主从配置(基于mysql5.5.x)

    安装mysql数据库主从复制,步骤如下:1.主从服务器分别作以下准备:    1.1.数据库版本完全一致    1.2.修改root的密码, 不改也可以 2.修改主服务器master #vi /etc ...

  6. MYSQL limit,offset 区别

    SELECT keyword FROM keyword_rank WHERE advertiserid' order by keyword LIMIT OFFSET ; 比如这个SQL ,limit后 ...

  7. 如何获得APP内部资源

    安装一个iTools(百度一下就有) 用USB连接设备,打开iTools

  8. 封装的ajax请求

    在做登录注册这类提交表单数据时,我们经常需要局部刷新网页来验证用户输入的信息,这就需要用到ajax请求,我们通常需要获取表单中的数据,发起ajax请求,通过服务程序,与数据库的数据进行比对,判断信息的 ...

  9. Windows下的Memcache安装

    Windows下的Memcache安装: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在终端(也即cmd命令界面)下输入 'c:\memca ...

  10. 第三方的图片加载( Android-Universal-Image-Loader)

    Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示. (1).使用多线程加载图片(2).灵活配置Im ...