codeforces1472 G. Moving to the Capital
G. Moving to the Capital
然后反向边建图,从上面bfs的叶子节点开始,就像“拨叶子”一样更新d2[]
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200010,M=2*N;
const ll mod=1e9+7;
int n,m;
int h1[N],h2[N],e[M],ne[M],idx;
int d1[N],d2[N];
bool st[N],in[N];
void add(int h[],int a,int b)
{e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
queue<int> q;
void bfs()
{for(int i=1;i<=n;i++)in[i]=st[i]=0,d1[i]=-1,d2[i]=0x3f3f3f3f;d1[1]=d2[1]=0;q.push(1);while(q.size()){int t=q.front();q.pop();int cnt=0;for(int i=h1[t];i!=-1;i=ne[i]){int j=e[i];if(d1[j]!=-1){d2[t]=min(d2[t],d1[j]);continue;}cnt++;d1[j]=d2[j]=d1[t]+1;q.push(j);}if(!cnt) st[t]=1;}for(int i=1;i<=n;i++)if(st[i]) {q.push(i);in[i]=1;}while(q.size()){int t=q.front();q.pop();in[t]=0;for(int i=h2[t];i!=-1;i=ne[i]){int j=e[i];if(d1[t]>d1[j]){d2[j]=min(d2[t],d2[j]);if(!in[j]) q.push(j);in[j]=1;}}}
}
int main()
{IO;int T=1;cin>>T;while(T--){cin>>n>>m;for(int i=1;i<=n;i++) h1[i]=h2[i]=-1;idx=0;while(m--){int a,b;cin>>a>>b;add(h1,a,b);add(h2,b,a);}bfs();for(int i=1;i<=n;i++) cout<<d2[i]<<' ';cout<<'\n';}return 0;
}
codeforces1472 G. Moving to the Capital相关推荐
- Codeforces Round #693 (Div. 3) G. Moving to the Capital dp + 思维
传送门 题意: 给一个图,111号点为中心点,定义dis[i]dis[i]dis[i]表示111号点到iii的距离.现在有三种移动方式 (1)(1)(1)从iii移动到jjj且dis[i]<di ...
- Codeforces Round #693 (Div. 3)G. Moving to the Capital
题目链接:Problem - G - Codeforces 题目大意:给定一张n个节点m条边的图,定义d数组为每个结点到结点1的距离. 每次可以选择两个操作:1,跳到结点x,dx>d当前 2.跳 ...
- Codeforces Round #693 (Div. 3)A~G解题报告
Codeforces Round #693 (Div. 3)A~G解题报告 A Cards for Friends 原题信息 http://codeforces.com/contest/1472/pr ...
- CF1472(div3):总结
文章目录 前言 A. Cards for Friends 题意简述 解析 代码 B. Fair Division 题意简述 解析 代码 C. Long Jumps 题意简述 解析 代码 D. Even ...
- codeforces round div2,3周赛补题计划(从开学到期末)
1. 本学期场次 从2020.09.19-2021.01.18,一共18周. 题号 场次 日期 备注 1475 Codeforces Round #697 (Div. 3) 1.25 1474 Cod ...
- 终端定时任务 开始缓冲_如何开始使用终端以提高生产力
终端定时任务 开始缓冲 by Luciano Strika 通过卢西亚诺·斯特里卡(Luciano Strika) 如何开始使用终端以提高生产力 (How to start using the ter ...
- 尼古拉斯 android_圣尼古拉斯和Alexa的访问
尼古拉斯 android 祝大家圣诞节快乐,并祝大家晚安! (Happy Christmas to all, and to all a good night!) Inspired by the hol ...
- 【ZOJ - 2968 】Difference Game (贪心,思维模拟)
题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...
- 百度陆奇:AI是5G网络下最好的加速器,技术商业化还要更快
问耕 假装发自 LV 量子位 出品 | 公众号 QbitAI 下一代通信网络5G,意味着什么? 百度集团总裁兼COO陆奇美国时间10日在出席CES一个对话活动时说,AI是5G网络下最好的加速器,陆奇表 ...
最新文章
- 如何参与贡献Dubbo社区
- docker build 指定dockerfile
- PyQt5 技术篇-plainTextEdit控件获得文本内容方法、设置文本内容方法。
- vue中使用better-scroll实现滑动
- 软件工程详细设计说明书_软件设计师
- android分屏模式_浅谈 Android 7.0 多窗口分屏模式的实现
- delta3d中,读取自己的xml配置文件。
- Java-访问修饰符
- 付费率第一 亏损显著收窄 网易云音乐将实现盈利?
- 基于数据挖掘的商业银行客户关系管理系统应用研究
- 华为员工能拿多少钱,揭秘一个真实的华为
- Sprite Atlas与Sprite Mask详解
- Squid缓存服务器和代理介绍
- Java官方教程目录2021最新版
- qq电脑管家抽奖页面源代码
- java基础巩固-宇宙第一AiYWM:为了维持生计,四大基础之计网_Part_2(在浏览器中输入www.baidu.com后执行的全部过程、DNS的域名<->IP地址、OS协议栈的样子、CDN)整起
- 微信小程序:页面布局摘要
- python中华氏度与摄氏度的转化_【Python】摄氏度与华氏度互相转化
- 中国最美的一千个汉字 : 千字文3
- html页面不论如何缩放也不改变布局的相对位置
热门文章
- qbuttongroup如何都不选中_全程马拉松,半程马拉松该如何跑?很多人都不知道这些细节...
- leetcode15. 三数之和(三指针)
- pytorch tensor 初始化_PyTorch简明笔记[1]-Tensor的初始化和基本操作
- 素数-试除法和埃式筛选法模板
- 工具类—KeyValuePair
- 数据结构与算法--死磕二叉树
- 使用Hash碰撞进行DoS攻击
- 深入理解 Spring Boot Starters 原理(手写Spring boot Start)
- 半平面交练习(计算几何)
- #6229. 这是一道简单的数学题(反演 + 杜教筛)