最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边

然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e6+, INF = 0x7fffffff; int n, m, k, cnt;
int head[maxn], d[maxn];
vector<int> f[maxn];
vector<string> g;
char str[maxn];
struct node
{
int u, v, next;
}Node[maxn<<]; void add_(int u, int v)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v)
{
add_(u, v);
add_(v, u);
} void init()
{
mem(head, -);
cnt = ;
} void dfs(int u)
{
if(g.size() >= k) return;
if(u == n+) { g.push_back(str); return; }
//cout<< 111 <<endl;
for(int i=; i<f[u].size(); i++)
{
str[f[u][i]/] = '';
// cout<< str <<endl;
dfs(u+);
str[f[u][i]/] = '';
}
} void bfs(int u)
{
mem(d, -);
queue<int> Q;
Q.push(u);
d[u] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == -)
{
d[e.v] = d[u] + ;
Q.push(e.v);
}
}
}
} int main()
{
init();
int u, v;
cin>> n >> m >> k;
for(int i=; i<m; i++)
{
cin>> u >> v;
add(u, v);
}
bfs();
// cout<< 11 <<endl;
for(int i=; i<=n; i++)
{
for(int j=head[i]; j!=-; j=Node[j].next)
if(d[Node[j].v] + == d[i])
{
f[i].push_back(j);
// cout<< i << " " << j/2 <<endl;
}
}
for(int i=; i<m; i++) str[i] = '';
dfs();
cout<< g.size() <<endl;
for(int i=; i<g.size(); i++)
{
cout<< g[i] <<endl;
} return ;
}

Berland and the Shortest Paths CodeForces - 1005F(最短路树)的更多相关文章

  1. CF1005F Berland and the Shortest Paths (树上构造最短路树)

    题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...

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

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

  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 最短路树计数

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

  7. CF1005F Berland and the Shortest Paths

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

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

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

  9. Shortest Paths

    最短路径 APIs 带权有向图中的最短路径,这节讨论从源点(s)到图中其它点的最短路径(single source). Weighted Directed Edge API 需要新的数据类型来表示带权 ...

随机推荐

  1. js 根据相对路径url获得完整路径url

    自定义方法 GetPath(url) ///根据相对路径得到完整URL ///strUrl:URL相对地址 var GetPath = function (strUrl) { if (strUrl.t ...

  2. Fiddler抓包调试前端脚本代码

    0.写在前面的话 之前看了阮一峰老师关于互联网协议入门的博客,受益匪浅,接着再去体会了下HTTP协议,就想着看实际网络访问中的那些HTTP请求头和响应是什么样的.Chrome的调试工具的Network ...

  3. 2017-2018-2 20155230《网络对抗技术》实验1:PC平台逆向破解(5)M

    1.直接修改程序机器指令,改变程序执行流程 2.通过构造输入参数,造成BOF攻击,改变程序执行流 3.注入Shellcode并执行 4.实验感想 注:因为截图是全屏所以右键图片在新的标签页打开观看更加 ...

  4. web窗体的运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAp ...

  5. Educational Codeforces Round 41 (Rated for Div. 2)(A~D)

    由于之前打过了这场比赛的E题,而后面两道题太难,所以就手速半个多小时A了前4题. 就当练手速吧,不过今天除了C题数组开小了以外都是1A A Tetris 题意的抽象解释可以在Luogu里看一下(话说现 ...

  6. Bluedroid协议栈HCI线程分析

    蓝牙进程中有多个线程,其中HCI 线程是负责处理蓝牙主机端和控制器的数据处理和收发的工作. 本篇文章就是分析一下该线程的数据处理流程. 1.跟HCI相关的接口 首先看看hci的相关的接口:在hci_l ...

  7. HTML 中使 footer 始终处于页面底部

    通常在页面中,需要使页脚 footer 部分始终处于底部.当页面高度不够 100% 时, footer 处于页面最底部,当页面内容高于 100% 时,页脚元素可以被撑到最底部. 方法一:绝对定位 &l ...

  8. [BZOJ2687]交与并[决策单调性]

    题意 给定 \(n\) 个区间,我们定义区间集合 \(S(|S|>1)\) 的权值为 区间交 \(\times\) 区间并,找出权值最大的区间集合. \(n\le 10^6\) 分析 首先排除区 ...

  9. PowerBI开发 第一篇:设计PowerBI报表

    PowerBI是微软新一代的交互式报表工具,把相关的静态数据转换为酷炫的可视化的,能够根据filter条件,对数据执行动态筛选,从不同的角度和粒度上分析数据.PowerBI主要由两部分组成:Power ...

  10. BigDecimal工具类

    package config_service.slowcity; import java.math.BigDecimal; public class ConfigServerApp { /* * 小数 ...