Codeforces 1005 F - Berland and the Shortest Paths
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的更多相关文章
- 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为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- 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 数据范围:略. 题解: 太鬼 ...
- 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 ...
随机推荐
- Apache正向代理和反向代理
一.正向代理 先说一正向代理(Forward Proxy),通常普通用户使用的比较多的,是正向代理.也就是在浏览器的网络连接属性框中,填写上一个代理服务器的ip和端口,即可通过代理服务器中转,去浏览网 ...
- Spring是如何处理注解的
如果你看到了注解,那么一定有什么代码在什么地方处理了它. Alan Hohn 我教Java课程时强调的一点是注解是惰性的.换句话说,它们只是标记,可能具有某些属性,但没有自己的行为.因此,每当你在一段 ...
- ubuntu18.04 安装新版本openssl
首先我们应该知道ubuntu18.04内置了1.1.0g版本的openssl: 使用下面的apt命令更新Ubuntu存储库并安装软件包编译的软件包依赖项: sudo apt update sudo a ...
- 20145208 蔡野 《网络对抗》Exp8 Web基础
20145208 蔡野 <网络对抗>Exp8 Web基础 本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POS ...
- python --- 21 MRO C3算法
一.python2.2之前用的是 经典类的MRO继承 ①深度递归继承 从左到右 ,一条路走到黑 ②广度继承 一层一层的继承 深度继承时 为 R 1 2 3 4 ...
- Bootstrap3基础 list-inline 无序列表横向显示
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- Flutter基础用法解析
解析开始 Flutter中一切皆widget,一切皆组件.学习Flutter中,必须首先了解Flutter的widget.先从最基本的MaterialApp和Scaffold开始了解 1 Materi ...
- HDU - 1875 畅通工程再续【最小生成树】
Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖 ...
- What are the differences between Flyweight and Object Pool patterns?
What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...
- How Flyway works
The easiest scenario is when you point Flyway to an empty database. It will try to locate its schema ...