题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出

性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一

边权为$1$,$bfs$出单源最短路,然后构建最短路树即可

代码实现需要思考

可以用$vector$记录最短路树中,每个点可能的父亲,对于合法父亲数量$>1$的点,$dfs$出所有可能的方案

  1. #include <queue>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #define N 200100
  7. #define ll long long
  8. using namespace std;
  9.  
  10. int T;
  11. int n,m,K,cte;
  12. int head[N],dis[N];
  13. struct Edge{int to,nxt;}edge[N*];
  14. void ae(int u,int v){
  15. cte++;edge[cte].nxt=head[u];
  16. head[u]=cte,edge[cte].to=v;
  17. }
  18. vector<int>fa[N];
  19. void bfs()
  20. {
  21. queue<int>q;
  22. memset(dis,0x3f,sizeof(dis));
  23. dis[]=;q.push();
  24. int tot=;
  25. while(!q.empty())
  26. {
  27. int u=q.front();q.pop();
  28. for(int j=head[u];j;j=edge[j].nxt){
  29. int v=edge[j].to;
  30. if(dis[v]>dis[u]+){
  31. dis[v]=dis[u]+;
  32. fa[v].push_back(j);
  33. q.push(v);
  34. }else if(dis[v]==dis[u]+){
  35. fa[v].push_back(j);
  36. }
  37. }
  38. }
  39. }
  40. int now[N],ans[N],sum,stk[N],tp;
  41. ll tot;
  42. void dfs_ans(int i)
  43. {
  44. if(i>tp){
  45. for(int i=;i<=m;i++)
  46. printf("%d",ans[i]);
  47. puts("");sum++;
  48. if(sum>=tot) exit();
  49. return;
  50. }
  51. int u=stk[i];
  52. now[u]=;
  53. for(;now[u]<fa[u].size();now[u]++){
  54. ans[fa[u][now[u]]>>]=;
  55. dfs_ans(i+);
  56. ans[fa[u][now[u]]>>]=;
  57. }
  58. }
  59. void solve()
  60. {
  61. bfs();tot=;
  62. for(int i=;i<=n;i++){
  63. int w=fa[i].size();
  64. tot*=1ll*max(,w);
  65. if(tot>K) break;
  66. }tot=min(1ll*K,tot);
  67. printf("%lld\n",tot);
  68. for(int i=;i<=n;i++)
  69. {
  70. if(fa[i].size()>)
  71. stk[++tp]=i;
  72. else ans[fa[i][]>>]=;
  73. }
  74. dfs_ans();
  75. }
  76.  
  77. int main()
  78. {
  79. freopen("t1.in","r",stdin);
  80. scanf("%d%d%d",&n,&m,&K);
  81. int x,y;cte=;
  82. for(int i=;i<=m;i++)
  83. scanf("%d%d",&x,&y),ae(x,y),ae(y,x);
  84. solve();
  85. return ;
  86. }

CF1005F Berland and the Shortest Paths (树上构造最短路树)的更多相关文章

  1. CF1005F Berland and the Shortest Paths

    \(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...

  2. CF1005F Berland and the Shortest Paths 最短路树计数

    问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...

  3. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

  4. Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...

  5. 【例题收藏】◇例题·II◇ Berland and the Shortest Paths

    ◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...

  6. [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij

    Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...

  7. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  8. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

    最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...

  9. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

随机推荐

  1. jQuery选择器、事件、节点、动画效果

    一.选择器  基本选择器:    标签选择器:  $("h1").css()    类选择器:  $(".c").css()    id选择器:  $(&quo ...

  2. set 集合————两个数组的交集

    class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int& ...

  3. Pyhton学习——Day11

    # Python中的内部模块# 函数学习的意义:抽取重复代码# 模块:不用重复写,模块及py文件,提高了代码的可维护性,其次,编写代码不必从零开始,当一个模块编写完毕,不必再重复编写# import ...

  4. CF739E Gosha is hunting(费用流,期望)

    根据期望的线性性答案就是捕捉每一只精灵的概率之和. 捕捉一只精灵的方案如下: 1.使用一个\(A\)精灵球,贡献为\(A[i]\) 2.使用一个\(B\)精灵球,贡献为\(B[i]\) 3.使用一个\ ...

  5. 每日Linux命令--不完整命令

    配置文件优化,即把默认的空行还有#注释行去掉,优化前先拷贝一份配置文件 egrep -v '^$|#' 拷贝的配置文件 > 原配置文件 mysql如何修改root用户的密码 方法1: 用SET ...

  6. VUE:过渡&动画

    VUE:过渡&动画 vue动画的理解 1)操作css的 trasition 或 animation 2)vue会给目标元素添加/移除特定的class 3)过渡的相关类名 xxx-enter-a ...

  7. java中的接口中的方法

    题目如下:(多选题)请选择以下接口定义正确的方法() A:public static void main (String[] args); B:private void test(); C:publi ...

  8. 五大最佳开源java性能监控工具

    如果你正在寻找性能监控工具,不妨看看以下推荐的这五款开源工具,这些工具目前已经可以替代付费工具了,你可以看看是否是你的最佳选择.本文推荐的五款开源工具目前是开源社区中最受欢迎的. 1. Stagemo ...

  9. 修改UTC时间

    /sbin/hwclock --systohc date按照时间修正.

  10. SQL学习之使用order by 依照指定顺序排序或自己定义顺序排序

    我们通常须要依据客户需求对于查询出来的结果给客户提供自己定义的排序方式,那么我们通常sql须要实现方式都有哪些,參考很多其它资料总结例如以下(不完好的和错误望大家指出): 一.假设我们仅仅是对于在某个 ...