Cactus CodeForces - 231E (无向图缩环)
大意: 给定无向图, 每个点最多属于一个简单环, 多组询问, 求给定起点终点, 有多少条简单路径.
先缩环, 然后假设两点树上路径经过$cnt$个环, 那么答案就为$2^{cnt}$.
要注意缩环建树时要加单向边.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
using namespace std; template <class T> void rd(T &x){x=0;bool f=0;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}if(f)x=-x;} const int N = 1e6+10, P = 1e9+7;
int n,m,dep[N],fa[N];
int s[N],vis[N],sz[N];
int son[N],top[N],v[N],fac[N];
vector<int> g[N],gg[N];
int Find(int x) {return s[x]?s[x]=Find(s[x]):x;}
void add(int x, int y) {
x=Find(x),y=Find(y);
if (x!=y) s[x]=y;
}
void get(int x, int y) {
if (dep[x]<dep[y]) return;
vis[y] = 1;
for (; x!=y; x=fa[x]) add(x,y),vis[x]=1;
}
void dfs(int x, int f) {
fa[x]=f,dep[x]=dep[f]+1;
for (int y:g[x]) if (y!=f) {
if (dep[y]) get(x,y);
else dfs(y,x);
}
}
void dfs2(int x, int f, int d) {
sz[x]=1,fa[x]=f,dep[x]=d,v[x]=v[f]+vis[x];
for (int y:gg[x]) if (y!=f) {
dfs2(y,x,d+1),sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
}
void dfs3(int x, int tf) {
top[x] = tf;
if (son[x]) dfs3(son[x],tf);
for (int y:gg[x]) if (!top[y]) dfs3(y,y);
}
int lca(int x, int y) {
while (top[x]!=top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
x = fa[top[x]];
}
return dep[x]<dep[y]?x:y;
} int main() {
fac[0]=1;
REP(i,1,N-1) fac[i]=fac[i-1]*2%P;
rd(n),rd(m);
REP(i,1,m) {
int u, v;
rd(u),rd(v);
if (u==v) continue;
g[u].pb(v),g[v].pb(u);
}
dfs(1,0);
int rt = 1;
REP(i,1,n) if (vis[i]) rt = Find(i);
REP(i,1,n) {
for (int j:g[i]) {
int u=Find(i),v=Find(j);
if (u!=v) gg[u].pb(v);
}
}
dfs2(rt,0,0),dfs3(rt,rt);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
x = Find(x), y = Find(y);
int l = lca(x,y);
printf("%d\n", fac[v[x]+v[y]-v[l]-v[fa[l]]]);
}
}
Cactus CodeForces - 231E (无向图缩环)的更多相关文章
- Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
E. Cactus A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...
- HDU 2242 考研路茫茫——空调教室 无向图缩环+树形DP
考研路茫茫——空调教室 Problem Description 众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们.Lele也是其中一个.而某教室旁边又摆着两个未装上的空调,更 ...
- bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列
1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1141 Solved: 435[Submit][ ...
- We Need More Bosses CodeForces - 1000E (无向图缩点)
大意: 给定无向连通图, 定义两个点$s,t$个价值为切断一条边可以使$s,t$不连通的边数. 求最大价值. 显然只有桥会产生贡献. 先对边双连通分量缩点建树, 然后求直径即为答案. #include ...
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- poj-3177(无向图缩点)
题意:给你n个点,m条边的无向联通图,问你最少增加几条边,使得这个图每对点至少有两条路径 解题思路:考虑每个环内的点必定有>=2条路径,所以先把这个无向图中的环去掉,用并查集缩环,然后剩下的图一 ...
- zstu.4191: 无向图找环(dfs树 + 邻接表)
4191: 无向图找环 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 117 Solved: 34 Description 给你一副无向图,每条边有 ...
- hdu-4612(无向图缩点+树的直径)
题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...
- [Bzoj1051][HAOI2006]受欢迎的牛(缩环)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6676 Solved: 3502[Submit][Sta ...
随机推荐
- 将elasticsearch设置为windows系统服务
目前我都是在windows的环境下操作是Elasticsearch,并且喜欢使用命令行 启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch 这样直接启动的话集 ...
- mysql.zip版本的安装教程
MySQL zip版本安装 一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev. ...
- Ajax简单异步上传图片并回显
前台代码 上传图片按钮 <a href="javascript:void(0)" onclick="uploadPhoto()">选择图片</ ...
- OpenResty之指令与常用API
1. 指令 通过 Lua 编写 Nginx 脚本的基本构建块是指令.指令常用于指定 Lua 代码是几时执行的以及如何使用运行的结果.下图展示了指令执行的顺序. lua_capture_error_lo ...
- 关于springboot访问html页面讨论
一.springboot项目无法直接访问static和templates文件夹html Spring Boot 默认将 /** 所有访问映射到以下目录: classpath:/static class ...
- Flutter移动电商实战 --(17)首页_楼层区域的编写
1.楼层标题组件 该组件非常简单,只接收一个图片地址,然后显示即可: class FloorTitle extends StatelessWidget { final String picture_a ...
- 数据分析 - Excel 综合实例 - 杜邦分析法
项目背景 杜邦分析法 利用 杜邦分析法完成对一份数据的动态分析 流程图如下 项目源数据 左侧为竖向的数据管理, 右侧为横向的数据管理 横向的数据管理在数据透视的时候会很不方便, 并不推荐 常用叫法左边 ...
- c++ STL find search
#include <iostream>#include <algorithm>#include <deque>#include <list>#inclu ...
- 目标检测 - TridentNet
转载: https://zhuanlan.zhihu.com/p/54334986 http://haha-strong.com/2019/07/25/20190725-TridentNet/ 开源代 ...
- 使用sproto buff 的陷阱
当sproto协议包中的数组元素,长度为0时,会出现接收异常.在没有调试断点的情况下,会停止接收其它协议.