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 ...
随机推荐
- Golang文件操作整理
基本操作 文件创建 创建文件的时候,一定要注意权限问题,一般默认的文件权限是 0666 关于权限的相关内容,具体可以参考鸟叔p141 这里还是再回顾下,文件属性 r w x r w x r w x,第 ...
- xman随机数相关题目
参加xman夏令营,大佬给我们带来了密码学课程.其中随机数部分感受颇深,记录下几个脚本. 1. 以时间作为种子的随机数 https://www.jarvisoj.com/ 的[xman2019]bab ...
- mybatis+mysql批量插入和批量更新、存在及更新
mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...
- 黑马vue---37-38、vue实例的生命周期
黑马vue---37-38.vue实例的生命周期 一.总结 一句话总结: created:实例已经在内存中创建OK,此时 data 和 methods 已经创建OK,此时还没有开始 编译模板 moun ...
- Geth安装和使用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011680118/article/details/82378509 一.简介 Geth是Go ...
- ZT:阿里合伙人发文:十年磨一剑,自研数据库终拿世界第一
按:真正做技术的,就该这样. 以下为全文转载 (观察者网讯) 10月24日,阿里巴巴合伙人.高德总裁刘振飞在阿里云开发者社区发文,回忆从2009年启动“去IOE”工程到2019年OceanBase拿下 ...
- linux中怎样会引起进程睡眠呢?
答: 使用信号量,wait队列,completion,调用schedule,用GFP_KERNEL指定的内存分配malloc,get,free,page等都会引起睡眠 思考: Q: 为什么会引起睡眠呢 ...
- Flex 弹性盒子布局使用教程
本文转载于<https://blog.csdn.net/lyznice/article/details/53981062>
- 启动Spring boot报错:nested exception is java.sql.SQLException: Field 'id' doesn't have a default value
先看具体日志: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with n ...
- tomcat 8.xxx ssl使用websocket Failed to close the ServletOutputStream connection cleanly
报错: 2019-09-17 10:30:17.277 default [https-jsse-nio-8110-exec-20] INFO o.a.t.w.s.WsRemoteEndpointImp ...