CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出
性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一
边权为$1$,$bfs$出单源最短路,然后构建最短路树即可
代码实现需要思考
可以用$vector$记录最短路树中,每个点可能的父亲,对于合法父亲数量$>1$的点,$dfs$出所有可能的方案
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200100
#define ll long long
using namespace std; int T;
int n,m,K,cte;
int head[N],dis[N];
struct Edge{int to,nxt;}edge[N*];
void ae(int u,int v){
cte++;edge[cte].nxt=head[u];
head[u]=cte,edge[cte].to=v;
}
vector<int>fa[N];
void bfs()
{
queue<int>q;
memset(dis,0x3f,sizeof(dis));
dis[]=;q.push();
int tot=;
while(!q.empty())
{
int u=q.front();q.pop();
for(int j=head[u];j;j=edge[j].nxt){
int v=edge[j].to;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;
fa[v].push_back(j);
q.push(v);
}else if(dis[v]==dis[u]+){
fa[v].push_back(j);
}
}
}
}
int now[N],ans[N],sum,stk[N],tp;
ll tot;
void dfs_ans(int i)
{
if(i>tp){
for(int i=;i<=m;i++)
printf("%d",ans[i]);
puts("");sum++;
if(sum>=tot) exit();
return;
}
int u=stk[i];
now[u]=;
for(;now[u]<fa[u].size();now[u]++){
ans[fa[u][now[u]]>>]=;
dfs_ans(i+);
ans[fa[u][now[u]]>>]=;
}
}
void solve()
{
bfs();tot=;
for(int i=;i<=n;i++){
int w=fa[i].size();
tot*=1ll*max(,w);
if(tot>K) break;
}tot=min(1ll*K,tot);
printf("%lld\n",tot);
for(int i=;i<=n;i++)
{
if(fa[i].size()>)
stk[++tp]=i;
else ans[fa[i][]>>]=;
}
dfs_ans();
} int main()
{
freopen("t1.in","r",stdin);
scanf("%d%d%d",&n,&m,&K);
int x,y;cte=;
for(int i=;i<=m;i++)
scanf("%d%d",&x,&y),ae(x,y),ae(y,x);
solve();
return ;
}
CF1005F Berland and the Shortest Paths (树上构造最短路树)的更多相关文章
- CF1005F Berland and the Shortest Paths
\(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...
- CF1005F Berland and the Shortest Paths 最短路树计数
问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
随机推荐
- 根据 thread id 停止一个线程
出自 https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/middleware/threading.py#L62 thread_id = ...
- DRF lazy Serializer
class LazySerializer: def __init__(self, cls_name, **kwargs): self.cls_name = cls_name self.kwargs = ...
- PAT 天梯赛练习集 L2-004. 这是二叉搜索树吗?
题目链接: https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点 ...
- 【codeforces 743E】Vladik and cards
[题目链接]:http://codeforces.com/problemset/problem/743/E [题意] 给你n个数字; 这些数字都是1到8范围内的整数; 然后让你从中选出一个最长的子列; ...
- 记一次BootStrap的使用
效果图如下: 一.简介: 什么是Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. 什么是响应式布局? 引用一句Bootstrap的标题语 “Boots ...
- HDU 4316 Contest 2
三个摄像头,在XOY上与立体的点求出在平面上的交点,然后求出凸包.三个凸包相交的面积即是所求,即是可以用半平面交的方法求解了. 模板题了.代码拿别人的. #include<cmath> # ...
- ZOJ 2705
这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...
- [ES2018] Two ways to write for-await-of
// Asynchronous iteration --> Symbol.asyncIterator async function main() { const syncIterable = [ ...
- linux下通过命令启动多个终端运行对应的命令和程序
作者:张昌昌 在一些情况下,往往须要同一时候启动多个终端并让终端运行自己主动运行对应的命令,进而达到提高操作效率的目的.在linux下gnome-terminal启动终端命令, gnome-t ...
- CSS3可伸缩框属性,可用于等分显示子元素或按比例显示子元素的大小
使用方法跟Android的android:layout_weight属性类似.可类比Android中的使用方法.这样比較好记,因为眼下全部浏览器都不支持大部分的属性,所以全部的属性都须要加上Firef ...