ContestHunter#24-C 逃不掉的路
Description:
求无向图的必经边
思路:一眼题 将无向图缩成树,然后求两点树上距离
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int N = 2e5 + ; int head[N], now = ;
struct edges{
int to, next;
}edge[N<<];
void add(int u,int v){ edge[++now] = {v, head[u]}; head[u] = now;} int n, m, dfn[N], low[N], cnt, tot, dict[N], Q, ans[N], dis[N], fa[N], vis[N];
bool bri[N<<];
struct query{
int to, id;
};
vector<query> q[N];
void q_add(int u, int v, int y){ q[u].push_back({v, y}); q[v].push_back({u, y}); }
void tarjan(int x, int in_edge){
dfn[x] = low[x] = ++cnt;
for(int i = head[x]; i; i = edge[i].next){
int v = edge[i].to;
if(!dfn[v]){
tarjan(v, i);
low[x] = min(low[x], low[v]);
if(low[v] > dfn[x]) bri[i] = bri[i ^ ] = ;
}
else if(i != (in_edge ^ )) low[x] = min(low[x], dfn[v]);
}
} void dfs(int x){
dict[x] = tot; vis[x] = ;
for(int i = head[x]; i; i = edge[i].next){
int v = edge[i].to;
if(vis[v] || bri[i]) continue;
dfs(v);
}
}
int get(int x){
if(x != fa[x]) return fa[x] = get(fa[x]);
return x;
}
void LCA(int x){
vis[x] = ;
for(int i = head[x]; i; i = edge[i].next){
int v = edge[i].to;
if(vis[v]) continue;
dis[v] = dis[x] + ;
LCA(v);
fa[v] = x;
}
for(int i = ; i < q[x].size(); i++){
int v = q[x][i].to, id = q[x][i].id;
if(vis[v] == ){
int lca = get(v);
ans[id] = min(ans[id], dis[x] + dis[v] - * dis[lca]);
}
}
vis[x] = ;
}
struct input{
int x, y;
}in[N];
int main(){
scanf("%d%d", &n, &m);
int x, y;
for(int i = ; i <= m; i++){
scanf("%d%d", &x, &y);
in[i] = {x, y};
add(x, y); add(y, x);
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i, );
for(int i = ; i <= n; i++)
if(!vis[i]) tot++, dfs(i);
memset(head, , sizeof(head));
memset(edge, , sizeof(edge)); now = ;
memset(vis, , sizeof(vis));
for(int i = ; i <= m; i++){
x = dict[in[i].x], y = dict[in[i].y];
if(x == y) continue;
add(x, y), add(y, x);
}
scanf("%d", &Q);
for(int i = ; i <= Q; i++){
scanf("%d%d", &x, &y);
x = dict[x], y = dict[y];
if(x == y) ans[i] = ;
else{
q_add(x, y, i);
ans[i] = 1e9;
}
}
for(int i = ; i <= n; i++) fa[i] = i;
LCA();
for(int i = ; i <= Q; i++)
printf("%d\n", ans[i]);
return ;
}
ContestHunter#24-C 逃不掉的路的更多相关文章
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- CH Round #24 - 三体杯 Round #1-C 逃不掉的路
留个e-DCC的板子 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- CH24C 逃不掉的路
edcc缩点之后跳倍增lca 丢个edcc缩点模板 Code: #include <cstdio> #include <cstring> using namespace std ...
- 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap
平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...
- 逃不掉的mysql数据库安装方式大全yum rpm 源码
数据库虽然也不是天天安装,但每次安装都要找来找去挺烦,特整理记录在此. 系统基于:Centos 7.x 数据库版本: MySQL 5.7.x 转载请注明出处 Yum 安装方式 1.下载 yum rep ...
- Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务
Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...
- D8 双连通分量
记得有个梗那一天,zw学生zzh大佬说逃不掉的路变成a不掉的题哈哈哈哈: 分离的路径: BZOJ 1718POJ 3177LUOGU 286: 思路:在同一个边双连通分量中,任意两点都有至少两条独立路 ...
- 『Tarjan算法 无向图的双联通分量』
无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...
- P6跨级晋升P8再到P10,我的11年成长之路
来自:语雀,作者:玉伯 链接:https://www.yuque.com/yubo/morning/grow-up-at-alibaba 注:这是在阿里内部前端大学的一个分享,整理了一份对外的版本,希 ...
随机推荐
- 安装Vue.js的方法有三种
1 使用独立的版本 在Vue.js官网上直接下载,在script标签里引用. 2 使用CND方法(不推荐) 3 NMP 方法 在用Vue.js构建大型应用的时候推荐使用NMP安装方法,NMP能很好的和 ...
- artDialog基本使用
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口l 自适应内容artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适 ...
- Lua学习笔记(8): 元表
元表 Lua中提供了元表(Metatable),其实这个元表就比较像面向对象中的类了,可以通过给某些特殊的标识符赋值达到重载的效果,这些特殊的标识符有: 名称 功能 __index 元表的索引方法 _ ...
- 【springmvc+mybatis项目实战】杰信商贸-6.重点知识回顾
1.重点知识回顾 Maven1)覆盖仓库文件,实际企业开发,公司会架一个测试服务器,在测试服务器中架私服.我们开发人员的程序,都连接私服.当本地没有项目中要使用的jar,Myeclipse maven ...
- python数据可视化——matplotlib 用户手册入门:使用指南
参考matplotlib官方指南: https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introd ...
- JavaScript学习笔记(三)——对象
第四章 理解对象 1 说明 对象的状态:属性,行为:方法: 对象定义放在花括号内: 用冒号分隔属性名和属性值: 用逗号分隔属性名和属性值对,包括方法: 最后一个属性值后面不加逗号: 属性名可以是任何字 ...
- Smokeping配置
参考文档: 官网:http://oss.oetiker.ch/smokeping/ 参考:http://jaminzhang.github.io/monitoring/smokeping-deploy ...
- 支持向量机SVM 初识
虽然已经学习了神经网络和深度学习并在几个项目之中加以运用了,但在斯坦福公开课上听吴恩达老师说他(在当时)更喜欢使用SVM,而很少使用神经网络来解决问题,因此来学习一下SVM的种种. 先解释一些概念吧: ...
- 2.hive里的增删改查
1.hive的增删改查 查询数据库 hive> show databases; OK default Time taken: 0.254 seconds, Fetched: 1 row(s) h ...
- 浅谈TSM概念、系统架构及技术发展
NFC作为一种近距离的无线通信技术,提供了一种更直接.更安全的现场交互解决方案.它能够允许电子设备之间进行非接触式点对点数据传输,实现数据交换.访问内容与服务.有了它,手机不再只是打电话.发短信以及上 ...