Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树
好久没更新博客了,一直懒得动,这次更新一下。
题意大概是:给出一个图,求它的一个一号节点的度数恰好为D的生成树的方案。
一开始随便水了个乱搞贪心,不出意外并没有过。
仔细思考之后,对于这个问题我们可以先不管有一端是1的边,然后在这样的子图中,观察节点联通块的情况。

如上图,我们可以认为{2,5,6},{3,7},{4}分别是一个联通块。
为了保证最后生成树的连通性,显然每个联通块最少要连接一条对节点1的边。
这样一来我们就可以自由地连接剩下的边,当然,要符合生成树的性质,用并查集来判断是否能连接这条边。
于是我们用剩下的对1连接的边来使得1的度数符合要求。
然后接下来再用剩下的其他边补全生成树即可。
#include<bits/stdc++.h>
using namespace std;
int i,i0,n,m,D,pre[200005],pre0[200005];
bool vis[200005];
vector<int>v[200005];
vector<pair<int,int>>v0,ans;
int fin(int x){return (pre[x]==x)?x:pre[x]=fin(pre[x]);}
void uni(int x,int y){if(fin(x)!=fin(y))pre[fin(y)]=fin(x);}
int fin0(int x){return (pre0[x]==x)?x:pre0[x]=fin0(pre0[x]);}
void uni0(int x,int y){if(fin0(x)!=fin0(y))pre0[fin0(y)]=fin0(x);}
int main()
{
scanf("%d %d %d",&n,&m,&D);
for(i=1;i<=n;i++)pre[i]=pre0[i]=i;
for(i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
if(x>y)swap(x,y);
if(x!=1)uni(x,y),v0.push_back({x,y});
else vis[y]=1;
}
for(i=1;i<=n;i++)if(vis[i])v[fin(i)].push_back(i);
for(i=2;i<=n;i++)
{
if(fin(i)==i)
{
D--;
uni0(1,v[i][v[i].size()-1]);
ans.push_back({1,v[i][v[i].size()-1]});
v[i].pop_back();
}
}
for(i=2;i<=n;i++)
{
if(fin(i)==i)
{
while(D&&v[i].size())
{
D--;
uni0(1,v[i][v[i].size()-1]);
ans.push_back({1,v[i][v[i].size()-1]});
v[i].pop_back();
}
}
}
for(auto i:v0)if(fin0(i.first)!=fin0(i.second))uni0(i.first,i.second),ans.push_back({i.first,i.second});;
bool f=1;
for(i=2;i<=n;i++)if(fin0(i)!=fin0(1))f=0;
if(f&&!D)
{
printf("YES\n");
for(auto i:ans)printf("%d %d\n",i.first,i.second);
}
else printf("NO\n");
return 0;
}
Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树的更多相关文章
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- 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 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
C. Not Equal on a Segment 题目连接: http://www.codeforces.com/contest/622/problem/C Description You are ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- codeforces 811E Vladik and Entertaining Flags(线段树+并查集)
codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...
随机推荐
- HDFS要点剖析
谈到大数据,不得不提的一个名词是"HDFS".它是一种分布式文件存储系统,其系统架构图如下图所示: 从图中可以了解到的几个关键概念 元数据(MetaData) 机架(Rock) 块 ...
- 三角形-->九九乘法表
使用嵌套循环打印九行*组成的三角形: * ** *** ...... *********(9个) public class Triangle { /** * 使用嵌套循环打印九行*组成的三角形 */ ...
- python数据类型之字典
字典定义 字典是一种 key-value 的数据类型,这点很重要,是区别使用列表和字典的依据. 语法格式: info = { 'stu1101': "Aaron", 'stu110 ...
- SpringMVC+MyBatis+MySQL 8小时链接断开
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...
- BZOJ1856: [Scoi2010]字符串(组合数)
题意 题目链接 Sol \(30 \%\)dp: \(f[i][j]\)表示放了\(i\)个\(1\)和\(j\)个\(0\)的不合法方案 f[0][0] = 1; cin >> N &g ...
- CSS中vertical-align的默认值baseline的理解
写在前面的话 在学校业余学习了一个月的CSS,人生中第一篇博客就献给CSS了,欢迎大家指正文章中不正确的地方,在此感谢.在尝试开始写博客的时候查了好多资料,原本以为写自己熟悉的知识的博客很容易,没想到 ...
- http常见状态码有哪些?
ajax常见面试题 1:什么是ajax?ajax作用是什么? 异步的javascript和xml AJAX 是一种用于创建快速动态网页的技术. ajax用来与后台交互 2:原生js ajax请求有几个 ...
- opencv3.2.0形态学滤波之形态学梯度、顶帽、黑帽
/*一.形态学梯度 (1)含义:是膨胀图和腐蚀图之差 (2)数学表达式:dst=morph-grad(src,element) =dilate(src,element) - erode(src,ele ...
- 风云2号云图Mosaic Dataset处理
# ---------------------------------------------------------------------------# -*- coding: utf-8 -*- ...
- sql 中的分隔符
1.通过分隔符可以将其中的内容作为一个整体的字符串进行处理. 假设数据库中有一个表,名为user info,注意这个名字,user 和 info 中间存在一个空格. 如果直接写如下查询,会报错,可能会 ...