题目大意:给你一个边权为$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 (树上构造最短路树)的更多相关文章

  1. CF1005F Berland and the Shortest Paths

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

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

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

  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_最短路树_堆优化dij

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

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

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

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

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

  9. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

随机推荐

  1. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  2. python中方法与函数的区别与联系

    今天huskiesir在对列表进行操作的时候,用到了sorted()函数,偶然情况下在菜鸟教程上看到了内置方法sort,同样都可以实现我对列表的排序操作,那么方法和函数有什么区别和联系呢? 如下是我个 ...

  3. 优化VR体验的7个建议

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/50392607 作者:ca ...

  4. ASP.NET-三种认证Authentication简介

    ASP.NET 的 认证Authentication主要分为三种 1.Form Authentication   用于互联网的认证,自定性强,功能强大,可以用cookie和ssl 2.Window A ...

  5. GIS+=地理信息+容器技术(2)——Dockers技术介绍

    -------------------------------------------------------------------------------------- Blog:    http ...

  6. pat(A) 1066. Root of AVL Tree

    代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...

  7. Android 输入管理服务-输入事件向详细应用的分发

    输入管理服务接收到输入事件,对输入事件进行处理之后会把输入事件分发到详细的应用中(如WMS.壁纸服务等)去处理的,这里涉及到了JNI从C++层向JAVA层的调用. 详细流程例如以下图所看到的:

  8. 计算机网络 4.网络层与IP协议

    网络中的每一台主机和路由器都有一个网络层部分.而路由器中也没有网络层以上的层次.网络层是协议栈中最复杂的层次. 转发forwarding:当一个分组到达某路由器的输入链路时.该路由器将分组移动到适当的 ...

  9. doT中嵌套for循环的使用

    1.数据结构 var goods = [ { "id": "1", "name": "衣服", "goods& ...

  10. 相辅相成的求最单源短路径算法:(SPFA& dijkstra)

    引用一位老oier的话: 一道题如果边权没有负数,那么一定是在卡SPFA.这时候就用到了堆优化的Dijkstra; 写在前面: 多打代码! 最好都掌握,灵活变通 SPFA: 主要用于稀疏图和有负权边的 ...