【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths
题目来源:Codeforce 1005F +传送门+
◆ 简单题意
给定一个n个点、m条边的无向图。保证图是连通的,且m≥n-1。
以首都(1节点)为根节点生成最小树。树的值定义为每个节点的深度和(根节点深度0)。举个例子:
而我们知道,可能有多种情况使树的权值最小,题目给出了一个整数k,如果最小树的生成方案数为ans,当 ans≤k 时,将 ans 种方案全部输出;当 ans>k 时,任意输出 k 种不同生成方案即可。输出方案格式为一个01串,第i个字符如果为0,表示不选第i条边(按照输入顺序),1为选择第i条边。
◆ 解析
其实点 i 的深度 dep[i] 就是根节点1到 i 的路径,而我们知道 1 到 i 没有任何一条路径短于它们的最短路径,所以生成树的权值最小时,根节点到每个点的距离就是原图中根节点到每个节点的最短路径。也就是说,我们生成的最小树就是一个最短路径树。然而显然有时候存在多条最短路径,这也就造成了我们生成的最小树有多种解。于是我们假装生成一棵树,实际上只是生成一个图。
由于这道题的边权都是等价的(不如就把边权看成1吧),我们可以用BFS直接求得最短路,所以说其实这也是一个BFS序图。为了考虑每种情况,我们把所有最小的BFS序边连上。下面再举一个生成BFS序图的例子(希望入门reader可以理解):
这样我们就生成了一个BFS序有根图,由于我们要生成树,而树的每一个节点的父节点少于一个。在上图中,4的父节点有两个,因此需要断开一条边——两条边是等价的,断掉任意一条即可。
我们可以把 u→v 的边存入v的边集 min_edg[v] ,那么最小权值树则是对于每一个除根节点之外的 v,选择 min_edg[v] 中的任意一条边,所以方案总数为 (除去根节点 i:2~n)min_edg[v]的边数之积。最后再DFS递归求方案即可(具体见代码)。
◆ 源代码
- /*Lucky_Glass*/
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<vector>
- #include<queue>
- using namespace std;
- const int MAXN=int(2e5);
- const int INF=int(1e9);
- int n_pnt,n_edg,k;
- int dis[MAXN+];
- vector<pair<int,int> > lnk[MAXN+];
- vector<pair<int,int> > min_edg[MAXN+];
- void BFS(int start)
- {
- fill(dis,dis+MAXN+,INF);
- dis[start]=;
- queue<int> que;
- que.push(start);
- while(!que.empty())
- {
- int u=que.front();que.pop();
- for(int i=;i<lnk[u].size();i++)
- {
- int v=lnk[u][i].first,id=lnk[u][i].second,Stp=dis[u]+;
- if(Stp>dis[v]) continue;
- min_edg[v].push_back(make_pair(u,id));
- if(Stp!=dis[v])
- dis[v]=Stp,que.push(v);
- }
- }
- }
- bool chose[MAXN+];int cnt;
- void DFS(int v)
- {
- if(v==n_pnt+)
- {
- cnt++;
- for(int i=;i<=n_edg;i++)
- printf("%d",chose[i]);
- printf("\n");
- return;
- }
- for(int i=;i<min_edg[v].size();i++)
- {
- chose[min_edg[v][i].second]=true;
- DFS(v+);
- chose[min_edg[v][i].second]=false;
- if(cnt==k) return;
- }
- }
- int main()
- {
- scanf("%d%d%d",&n_pnt,&n_edg,&k);
- for(int i=,u,v;i<n_edg;i++)
- scanf("%d%d",&u,&v),
- lnk[u].push_back(make_pair(v,i+)),
- lnk[v].push_back(make_pair(u,i+));
- BFS();
- long long ans=;
- for(int i=;i<=n_pnt;i++)
- {
- ans*=min_edg[i].size();
- if(ans>k) break;
- }
- printf("%lld\n",min(k*1ll,ans));
- DFS();
- return ;
- }
The End
Thanks for reading!
- Lucky_Glass
(Tab:如果我有没讲清楚的地方可以直接在邮箱lucky_glass@foxmail.com email我,在周末我会尽量解答并完善博客~)
【例题收藏】◇例题·II◇ Berland and the Shortest Paths的更多相关文章
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- CF1005F Berland and the Shortest Paths
\(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...
- CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...
- CF1005F Berland and the Shortest Paths 最短路树计数
问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- Shortest Paths
最短路径 APIs 带权有向图中的最短路径,这节讨论从源点(s)到图中其它点的最短路径(single source). Weighted Directed Edge API 需要新的数据类型来表示带权 ...
随机推荐
- 硬盘和显卡的访问与控制(二)——《x86汇编语言:从实模式到保护模式》读书笔记02
上一篇博文我们讲了如何看到实验结果,这篇博文我们着重分析源代码. 书中作者为了说明原理,约定了一种比较简单地用户程序头部格式,示意图如下(我参考原书图8-15绘制的,左边的数字表示偏移地址): 所以, ...
- avalon教程-简介
avalon是什么? avalon是一个MVVM框架, Modle-模型层,即为js中从后台接口中取出的数据,例如一个对象或者对象数组,并对这些数据进行一定的格式化.常见的返回数据是这样的{id : ...
- C++/CLI中的const literal initonly 友元(转)
C++/CLI中的const literal initonly 友元 C++/CLI中的const Visual C++并不允许在const对象上调用成员函数,除非该成员函数也被声明为const. C ...
- Vue.js基础语法(一)
vue学习的一系列,全部来自于表哥---表严肃,是我遇到过的讲课最通透,英文发音最好听的老师,想一起听课就去这里吧 https://biaoyansu.com/i/hzhj1206 前言: 前端解析数 ...
- Docker 清理命令汇总
杀死所有正在运行的容器 docker kill $(docker ps -a -q) 删除所有已经停止的容器 docker rm $(docker ps -a -q) 删除所有未打 dangling ...
- SAP CRM和C4C的产品主数据price维护
SAP CRM 点了Edit List之后,可以直接修改产品主数据的Price信息: C4C 在C4C的product administration工作中心里: 点击Edit按钮进入编辑模式,Pric ...
- Aizu The Maximum Number of Customers
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers Ide ...
- Git学习环境搭建和git对用户的增删改查命令
git安装 windows下安装git git下载进入网址: https://git-scm.com/downloads 点击下载,进行安装,安装成功,你会看到图标,点击进入到git bash 查看g ...
- ABI and compiler
http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...
- 网格中的BFS,逆向(POJ2049)
题目链接:http://poj.org/problem?id=2049 解题报告: 网格中的BFS,最主要的是边界问题. 1.这里在左右,上下两个方向上,分别判断墙,和门,细节是,向上有t个墙,for ...