F - Berland and the Shortest Paths

思路:

bfs+dfs

首先,bfs找出1到其他点的最短路径大小dis[i]

然后对于2...n中的每个节点u,找到它所能改变的所有前驱(在保证最短路径不变的情况下),即找到v,使得dis[v] + 1 == dis[u],并把u和v所连边保存下来

最后就是dfs递归暴力枚举每个点的前驱,然后输出答案

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
int n, m, k;
vector<pii>g[N];
vector<int>pre[N];
int dis[N];
bool vis[N];
char s[N];
vector<string>res;
void bfs(int st) {
queue<pii>q;
dis[] = ;
vis[] = true;
q.push({, });
while(!q.empty()) {
pii p = q.front();
q.pop();
for (int i = ; i < g[p.fi].size(); i++) {
int v = g[p.fi][i].fi;
if(!vis[v]) {
vis[v] = true;
dis[v] = p.se + ;
q.push({v, p.se + });
}
}
}
}
void dfs(int u) {
if((int) res.size() >= k) return ;
if(u > n) {
res.pb(s+);
return ;
}
for (int i = ; i < pre[u].size(); i++) {
s[pre[u][i]] = '';
dfs(u+);
s[pre[u][i]] = '';
}
}
int main() {
fio;
int u, v;
cin >> n >> m >> k;
for (int i = ; i <= m; i++) {
cin >> u >> v;
g[u].pb({v, i});
g[v].pb({u, i});
}
bfs();
for (int i = ; i <= n; i++) {
for (int j = ; j < g[i].size(); j++) {
pii p = g[i][j];
if(dis[p.fi]+ == dis[i]) pre[i].pb(p.se);
}
}
for (int i = ; i <= m; i++) s[i] = '';
dfs();
cout << (int)res.size() << endl;
for (int i = ; i < res.size(); i++) cout << res[i] << endl;
return ;
}

Codeforces 1005 F - Berland and the Shortest Paths的更多相关文章

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

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

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

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

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

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

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

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

  5. CF1005F Berland and the Shortest Paths

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

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

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

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

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

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

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

  9. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths

            F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. python3 独立环境 virtualenv & conda

    python3 独立环境 virtualenv & conda virtualenv 创建独立python环境 virtualenv env_name -p /usr/bin/python3 ...

  2. python简说(二十四)发送网络请求

    一.get,post请求import requestsurl='http://127.0.0.1:8999/api/upload'# data = {'username':'testuser1','p ...

  3. 动态规划之115 Distinct Subsequences

    题目链接:https://leetcode-cn.com/problems/distinct-subsequences/description/ 参考链接:https://www.cnblogs.co ...

  4. uniGUI试用笔记(三)

    uniGUI下的MessageDlg使用发生了变化,最大的特点是: 1.成为了uniGUIForm的成员函数: 2.变成过程(procedure)了,也就是没有返回值了,使得程序不再具有线程阻塞性. ...

  5. 源码编译安装libtool工具

    1. 获取源码 wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz tar xvf libtool-2.4.6.tar.gz -C ~ ...

  6. tp框架中的一些疑点知识-1

    tp默认的编码是utf-8 Runtime中的Cache和Logs都是分模块的,因为在应用app下可以有多个模块,但是 公共模块和Runtime模块只有一个, 所以, Runtime要包含各个模块的内 ...

  7. hihoCoder week2 Trie树

    题目链接 https://hihocoder.com/contest/hiho2/problems 字典树 #include <bits/stdc++.h> using namespace ...

  8. NOI 2011 阿狸的打字机(AC自动机+主席树)

    题意 https://loj.ac/problem/2444 思路 ​多串匹配,考虑 \(\text{AC}\) 自动机.模拟打字的过程,先建出一棵 \(\text{Trie}\) 树,把它变成自动机 ...

  9. Oracle 基础学习笔记

    知识点 一.登陆数据库: 登陆数据库: sqlplus system/oracle123456 二.新建用户.授权(连接数据库.创建表.表空间.查询某用户下的表) 语法: create user [用 ...

  10. 【C#】委托的发展

    "用事件去处理程序, 进而解决问题" ---- 委托的目的 委托早在C#2的时候就已经初具模型, 但是并不是特别灵活, 制止C#3才在代码中被广泛使用. C#4中泛型委托, C#5 ...