【codeforces 723F】st-Spanning Tree
【题目链接】:http://codeforces.com/contest/723/problem/F
【题意】
给你一张图;
让你选择n-1条边;
使得这张图成为一颗树(生成树);
同时s的度数不超过ds且t的度数不超过dt
【题解】
先把s和t隔离开;
考虑其他点形成的若干个联通块;
因为一开始的图是联通的;
所以,那些不同的联通块,肯定是通过s或通过t而联通的;
这样就只要考虑每个联通块和s、t的联通性了;
对于只能和s或t之中的一个相连的块;
直接连一条边到s或t就好,然后ds-=1或是dt-=1(取决与它在原图上只和哪一点相连);
最后处理能和s以及t同时相连的块;
这时s和t还是未联通的!
则一开始先找到一个块;
让s和t变成联通的
即s->连通块sp->t
然后ds-=1,dt-=1
之后,
根据ds和dt;
哪一个不为0就把那个联通块连到哪一个点;
(这种先把要讨论的点隔开的方式值得借鉴)
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+100;
int n, m,s,t,ds,dt,cnt,vis[N],com;
vector <int> G[N],S[N],T[N];
vector <pii> ans;
void dfs(int x)
{
vis[x] = 1;
for (int y : G[x])
{
if (y == s)
S[cnt].push_back(x);
else
if (y == t)
T[cnt].push_back(x);
else
if (!vis[y])
{
dfs(y);
ans.push_back(mp(x, y));
}
}
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0);//scanf,puts,printf not use
cin >> n >> m;
rep1(i, 1, m)
{
int x, y;
cin >> x >> y;
G[x].pb(y), G[y].pb(x);
}
cin >> s >> t >> ds >> dt;
rep1(i, 1, n)
if (!vis[i] && i != s && i != t)
{
cnt++;
dfs(i);
}
rep1(i, 1, cnt)
if (S[i].empty())
ans.push_back(mp(T[i][0], t)), dt--;
else
if (T[i].empty())
ans.push_back(mp(S[i][0], s)), ds--;
else
com++;
if (ds <= 0 || dt <= 0 || ds + dt < com + 1)
return cout << "No" << endl, 0;
if (!com)
ans.push_back(mp(s, t));
else
{
int lj = 0;
rep1(i,1,cnt)
if (!S[i].empty() && !T[i].empty())
{
if (!lj)
{
ds--, dt--;
lj = 1;
ans.push_back(mp(S[i][0], s));
ans.push_back(mp(T[i][0], t));
}
else
{
if (ds)
ans.push_back(mp(S[i][0], s)),ds--;
else
ans.push_back(mp(T[i][0], t)),dt--;
}
}
}
cout << "Yes" << endl;
rep1(i, 0, n - 2)
cout << ans[i].first << ' ' << ans[i].second << endl;
return 0;
}
【codeforces 723F】st-Spanning Tree的更多相关文章
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 【27.48%】【codeforces 699D】 Fix a Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 1086B】Minimum Diameter Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...
- 【Codeforces 161D】Distance in Tree
[链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离 ...
- 【codeforces 698B】 Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...
- 【CodeForces 699D】Fix a Tree
dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...
随机推荐
- 推送_即时推送_即时通讯_在线Demo
[伊尚]美容店(万达店)找创业合伙人(限女生) 点击查看Demo 线上预览 运行Demo截图如下: 线上预览
- Vultr好server不敢独享
Vultr是一家美国2014年成立的新公司.瞬间红遍世界,他是干什么的?他是serverVPS(Virtual Private Server)提供商,这个价格真实惊人的廉价5美金/月.折合人民币30元 ...
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...
- 【CSS】CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框
在网页中,常常会用到各种Icon,假设老是麻烦设计狮画出来不免有些不好意思,所以有时候我们也能够用CSS写出各种简单的形状.一来能够减轻他们的负担,二来也能够使用CSS替代图片.提高载入速度. 在网页 ...
- java中String的21种使用方法
(构造函数必须new出来) * public String (char[] vaue) 将一个字符数组变成字符串(构造函数) * public Stri ...
- c18---数组和指针
// // main.c // day09 #include <stdio.h> int main(int argc, const char * argv[]) { ; int *numP ...
- python时间戳
time.strftime("%Y-%m-%dT%H:%M:%S.000", time.localtime())
- k8s Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry
环境介绍: 说明 节点 ip 系统 Gitlab Server git.ds.com 10.0.1.179 CentOS 7.5.1804 Gitlab Runner 10.0.1.178 Cen ...
- 框架,表格,表单元素,css基础以及基本标签的结合
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- python print 显示不同的字体
显示格式: print('\033[显示方式;字体颜色;背景色m.....\033[0m') ------------------------------- 显示方式 | 效果 ----------- ...