【题目链接】: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的更多相关文章

  1. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  4. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  5. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【Codeforces 1086B】Minimum Diameter Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...

  7. 【Codeforces 161D】Distance in Tree

    [链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离 ...

  8. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  9. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

随机推荐

  1. oc6--类方法

    // // main.m // 第一个OC类-类方法 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : N ...

  2. css3 背景background

    Css3背景<background> Css3包含多个新的背景属性,它们提供了对背景更强大的控制.可以自定义背景图的大小,可以规定背景图片的定位区域,css3还允许我们为元素使用多个背景图 ...

  3. HUdson2092整数解

    2019-05-17 16:04:37 加油,坚持就是胜利,fightting m / i的情况,i可能等于0 #include <bits/stdc++.h> using namespa ...

  4. Pop3协议详解

      POP3全称为Post Office Protocol version3,即邮局协议第3版.它被用户代理用来邮件服务器取得邮件.POP3采用的也是C/S通信 模型 用户从邮件服务器上接收邮件的典型 ...

  5. Session版购物车+MVC局部刷新

     效果图: 大致代码: <script type="text/javascript"> $(function () { LoadOrderDetailList(); } ...

  6. 织梦dedecms红黑配图片模板源码v2.0

    dedecms红黑配风格美女图片站是采用dedecms程序搭建的图片网站源码,网站感觉很大气,简约但是不简单,适合做图片网站.网站模板是收集其他网站的模板,感谢原网站提供者.在安装过程中出现问题,现已 ...

  7. java exception 异常错误记录

    //异常:Could not obtain transaction-synchronized Session for current thread 做定时器的时候用ApplicationContext ...

  8. [原创]C++带空格字符串的输入问题

    字符串一直是一个重点加难点,很多笔试面试都会涉及,带空格的字符串更是十分常见,现在对字符串的输入问题进行一下总结. C++用cin输入的时候会忽略空格以后的字符,比如 char a[100]; cin ...

  9. Dictionary 小知识

    Dictionary<string, string>是一个泛型 他本身有集合的功能有时候可以把它看成数组 他的结构是这样的:Dictionary<[key], [value]> ...

  10. 获取 Windows Phone 的 User-Agent 字符串

    这个是简单介绍一下如何在wp开发中获取 Windows Phone 的 User-Agent 字符串,更多wp移动开发就去那个wp教程网吧.http://wp.662p.com 使用方法 public ...