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 ...
随机推荐
- mysql读写分离[高可用]
顾名思义, 在mysql负载均衡中有多种方式, 本人愚钝,只了解驱动中间件和mysql_proxy两种方式, 对于驱动,利用的是ReplicationDriver,具体请看远哥的这篇文章: MySQL ...
- 从输入URL到页面显示发生了什么
阅读目录 1.输入地址 2.浏览器查找域名的 IP 地址 3.浏览器向 web 服务器发送一个 HTTP 请求 4.服务器的永久重定向响应 5.浏览器跟踪重定向地址 6.服务器处理请求 7.服务器返回 ...
- linux --- 2.常用命令 , python3, django安装
一.常用命令 1.常识命令 ① w 显示终端连接数 ②pwd 我在哪 ③whoami 我是谁 ④which 命令 找到命令的绝对路径 2.linux 命令行的组 ...
- 配置maven默认jdk版本
1.在setting.xml中配置.对所有通过该配置文件构建的maven项目有效. <profile> <id>jdk-1.8</id> <activatio ...
- android设备如何进入深度休眠还能继续使用定时器【求解】
经过试验,andriod设备进入深度休眠的时候,定时器是不能使用.但是阻止设备进入深度休眠,可以获取一把锁,但是拿了锁之后,设备不能进入休眠,系统的功耗会增加.怎么能够在系统进入休眠,定时器还能正常工 ...
- UVALive 7501 Business Cycle(二分)题解
题意:n个数,有一个起始值,按顺序从第一个开始不断循环取数,如果取完后相加小于0就变为0,最多取p个数,问你得到大于等于值g所需要的最小起始值为多少 思路:这题目爆long long爆的毫无准备,到处 ...
- C语言变量的作用域和存储类型
1.动态局部变量:也称局部变量.自动变量,是指在函数内部定义的自动变量,不带static修饰,作用域是定义该变量的子程序.在退出函数后,变量自带内存会自动释放. 2.静态局部变量:是指在函数内部定义的 ...
- sql 指定数据库中的信息操作
查是否有该表名 SELECT * FROM sys.objects WHERE name='表名'查表字段的信息select * from syscolumns where id=Object_Id( ...
- Tutorial on GoogleNet based image classification --- focus on Inception module and save/load models
Tutorial on GoogleNet based image classification 2018-06-26 15:50:29 本文旨在通过案例来学习 GoogleNet 及其 Incep ...
- JavaScript——语法与数据类型
严格模式 ECMA5引入了严格模式的概念.严格模式是为JavaScript定义了一种不同的解析与执行模型.在严格模式下,ECMA3中的一些不确定的行为将得到处理,而且对某些不安全的操作也会抛出错误.要 ...