当前位置:网站首页>(2022杭电多校六)1010-Planar graph(最小生成树)
(2022杭电多校六)1010-Planar graph(最小生成树)
2022-08-05 05:42:00 【AC__dream】
题目:


题意:给n个点和m条边,m条边将一个图分成若干个连通区域,然后我们可以删除一些边,问至少删除多少条边才能使得所有的区域是连通的,在所有的删边情况中选择字典序最小的一种情况进行输出。
先来思考一下,什么情况下才能使得所有区域是连通的?其实画个图观察不难发现,只有当图中没有环时整个区域才是连通的,现在问题就转化为我们至少删除多少条边才能使得图中没有环,由于树是一个没有环的最大连通子图,所以显然是将图删边使其形成一棵树,这样的话删除的边数一定是最小的,由于题目中要求删除的边的字典序最小,那么我们就可以考虑从编号大的边开始添加边,只要加进去该边不成环就把该边加入图中,这样最后没有加入图中的边就是我们要删除的边,那么我们直接按照字典序进行从小到大输出即可,这个过程就是利用类似kruscal的过程进行实现,只是我们不是按照边权进行排序,而是按照编号进行排序。
下面是代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<cmath>
using namespace std;
const int N=2e6+10;
int fu[N],u[N],v[N];
int find(int x)
{
if(x!=fu[x]) fu[x]=find(fu[x]);
return fu[x];
}
stack<int>st;
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) fu[i]=i;
for(int i=1;i<=m;i++)
scanf("%d%d",&u[i],&v[i]);
for(int i=m;i>=1;i--)
{
int fx=find(u[i]),fy=find(v[i]);
if(fx==fy)
st.push(i);
else
fu[fx]=fy;
}
printf("%d\n",st.size());
while(!st.empty())
{
printf("%d ",st.top());
st.pop();
}
puts("");
}
return 0;
}
边栏推荐
- 单片机原理与应用复习
- D39_Eulerian Angles and Quaternions
- NACOS Configuration Center Settings Profile
- LeetCode practice and self-comprehension record (1)
- el-progress implements different colors of the progress bar
- JS控制只能输入数字并且最多允许小数点两位
- Passing parameters in multiple threads
- H5开发调试-Fiddler手机抓包
- js 使用雪花id生成随机id
- Tips for formatting code indentation
猜你喜欢
随机推荐
docker部署完mysql无法连接
js 使用雪花id生成随机id
H5开发调试-Fiddler手机抓包
记录vue-页面缓存问题
js判断文字是否超过区域
亚马逊美国站:马术头盔CPC认证标准要求
【2022 DSCTF决赛wp】
ES2020新特性
Configuration of routers and static routes
【MyCat简单介绍】
【FAQ】What is Canon CCAPI
Come, come, let you understand how Cocos Creator reads and writes JSON files
## 简讲protobuf-从原理到使用
Q 2020, the latest senior interview Laya soul, do you know?
微信小程序仿input组件、虚拟键盘
Network Troubleshooting Basics - Study Notes
多行文本省略
HelloWorld
Vim tutorial: vimtutor
【5】Docker中部署MySQL







