最短路树就是用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. windows linux hosts文件的配置,开发项目中域名跳转等。

    我们通常都知道Windows中hosts文件(C:\Windows\System32\drivers\etc),用来映射域名的.linux上当然也有,一般在/etc/hosts下. 当工作的项目,在开 ...

  2. iOS9中http不能使用的解决

    用xcode7写程序的时候发现webview不能显示http的链接网页,发现原来是由于ios9的一个新特性,iOS9引入了新特性App Transport Security (ATS),新特性要求Ap ...

  3. jupyter notebook的两个使用技巧

    1.设置jupyter notebook的密码: Jupyter-notebook password 2.查看jupyter notebook的token: Jupyter notebook list

  4. 【H5】dropload (移动端下拉刷新,上拉加载)

    插件概要地址:http://ximan.github.io/dropload/ 一般下载其中的demo2对照修改即可使用. 小吐槽.我在项目中用的时候,有个后端说ajax麻烦但是还是要做体现他很热爱工 ...

  5. 关于this指向,翻到的

    关于JavaScript函数执行环境的过程,IBM developerworks文档库中的一段描述感觉很不错,摘抄如下: “JavaScript 中的函数既可以被当作普通函数执行,也可以作为对象的方法 ...

  6. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

  7. Spring + SpringMVC配置

    代码结构如下 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  8. 2017-2018-2 20155224『网络对抗技术』Exp5:MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课举的火箭和卫星的例子非常形象,火箭只是 ...

  9. POJ 3349&&3274&&2151&&1840&&2002&&2503

    (今天兴致大发学了Markdown,第一篇博客) 这次的主要都是hash的题目(当然这就意味这可以用map) hash的方式也有很多: 普通hash hash挂链 双hash以及自然溢出等 当然我还是 ...

  10. 阿里云Redis外网转发访问

    1.前提条件 如果您需要从本地 PC 端访问 Redis 实例进行数据操作,可以通过在 ECS 上配置端口映射或者端口转发实现.但必须符合以下前提条件: 若 Redis 实例属于专有网络(VPC),E ...