CodeForces 231E|Cactus|边双联通分量|LCA
来源:互联网 发布:主营收入增长率源码 编辑:程序博客网 时间:2024/06/08 09:53
给定一个仙人掌图,多次询问某两点间的路径数。
发现某两点间的路径数就等于2的路径间环的个数的幂。
因此Tarjan缩点后计算路径经过的环的个数即可。
其实想改成RMQ的,但是太懒了。。
#include <cstdio>#include <algorithm>#define FOR(i,j,k) for(i=j;i<=k;++i)using namespace std;typedef long long ll;const int N = 100005, M = 700005, mod = 1000000007, K = 18;ll qpow(ll a, ll b) { ll c = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) c = c * a % mod; return c;}struct Graph { int h[N], p[M], v[M], cnt, pos[N], fa[N][19], num[N]; int belong[N], sk[N], instack[N], dfn[N], low[N], bcc, n; ll dis[N]; int ts, vis[M], top, dep[N], id[N]; Graph() { cnt = 1; bcc = 0; ts = 0; top = 0; } void add(int a, int b) { p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt; } void tarjan(int x) { int i; dfn[x] = low[x] = ++ts; sk[top++] = x; instack[x] = 1; for (i = h[x]; i; i = p[i]) { if (vis[i]) continue; vis[i] = vis[i ^ 1] = 1; if (!dfn[v[i]]) { tarjan(v[i]); low[x] = min(low[x], low[v[i]]); } else if (instack[v[i]]) low[x] = min(low[x], dfn[v[i]]); } if (low[x] == dfn[x]) { num[++bcc] = 0; do { i = sk[--top]; instack[i] = 0; belong[i] = bcc; num[bcc]++; } while (i != x); num[bcc] = num[bcc] > 1; } } void build_from(const Graph &g) { for (int i = 1; i <= g.n; ++i) for (int j = g.h[i]; j; j = g.p[j]) if (g.belong[i] != g.belong[g.v[j]]) add(g.belong[i], g.belong[g.v[j]]); } void dfs(int x, int f, int *data) { int i; pos[x] = ++ts; id[ts] = x; fa[x][0] = f; dep[x] = dep[f] + 1; dis[x] = dis[f] + data[x]; FOR(i,1,K) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for (i = h[x]; i; i = p[i]) if (v[i] != f) dfs(v[i], x, data); } int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); int t = dep[x] - dep[y], i; FOR(i,0,K) if ((1 << i) & t) x = fa[x][i]; for(i=K;i>=0;--i) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return x == y ? x : fa[x][0]; }} g, ng;ll query(int a, int b) { a = g.belong[a]; b = g.belong[b]; int c = ng.lca(a, b); return ng.dis[a] + ng.dis[b] - 2 * ng.dis[c] + g.num[c];}int main() { int n, m, i, q, a, b; scanf("%d%d", &n, &m); FOR(i,1,m) scanf("%d%d",&a,&b),g.add(a,b),g.add(b,a); g.n = n; g.tarjan(1); ng.build_from(g); ng.dfs(1, 0, g.num); scanf("%d", &q); while (q--) { scanf("%d%d", &a, &b); printf("%I64d\n", qpow(2, query(a, b))); } return 0;}
E. Cactus
A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle.
A simple cycle in a undirected graph is a sequence of distinct vertices v1, v2, …, vt (t > 2), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1, and also exists an edge between vertices v1 and vt.
A simple path in a undirected graph is a sequence of not necessarily distinct vertices v1, v2, …, vt (t > 0), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1 and furthermore each edge occurs no more than once. We’ll say that a simple path v1, v2, …, vt starts at vertex v1 and ends at vertex vt.
You’ve got a graph consisting of n vertices and m edges, that is a vertex cactus. Also, you’ve got a list of k pairs of interesting vertices xi, yi, for which you want to know the following information — the number of distinct simple paths that start at vertex xi and end at vertex yi. We will consider two simple paths distinct if the sets of edges of the paths are distinct.
For each pair of interesting vertices count the number of distinct simple paths between them. As this number can be rather large, you should calculate it modulo 1000000007 (109 + 7).
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105) — the number of vertices and edges in the graph, correspondingly. Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n) — the indexes of the vertices connected by the i-th edge.
The next line contains a single integer k (1 ≤ k ≤ 105) — the number of pairs of interesting vertices. Next k lines contain the list of pairs of interesting vertices: the i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi) — the indexes of interesting vertices in the i-th pair.
It is guaranteed that the given graph is a vertex cactus. It is guaranteed that the graph contains no loops or multiple edges. Consider the graph vertices are numbered from 1 to n.
Output
Print k lines: in the i-th line print a single integer — the number of distinct simple ways, starting at xi and ending at yi, modulo 1000000007 (109 + 7).
Examples
input
10 111 22 33 41 43 55 68 68 77 67 99 1061 23 56 99 29 39 10
output
222441
- CodeForces 231E|Cactus|边双联通分量|LCA
- [Codeforces 231E] Cactus (环缩点+LCA)
- Codeforces 231E - Cactus
- POJ 3694Network 双联通分量 + lca
- poj 3694Network 双联通分量+lca
- 边双联通分量
- 边双联通分量
- 边双联通分量
- CodeForces 567E President and Roads(最短路 + 双联通分量)
- codeforces 732F 双联通分量
- poj 3694 Network(桥+边联通分量+LCA)
- POJ3694-Network(LCA+边联通分量+桥)
- Codeforces Round #143 (Div. 2) E. Cactus(LCA+无向图缩点)
- UVALive5796点双联通分量or边双联通分量
- 点双联通分量和边双联通分量小结
- poj3694(边双联通分量)
- 边双联通分量poj3352
- 边的双联通分量
- centos7 install devstack
- Bzoj1004:[HNOI2008]Cards:置换群+dp
- php对html标签的处理
- 关于使用getActionBar()报空指针异常的错误
- selector使用时遇到的问题
- CodeForces 231E|Cactus|边双联通分量|LCA
- Java基本数据类型
- mvp模式
- oracle复制一张表结构到另外一张新表中
- Java Web的Model1和Model2简介
- AndroidStudio获取(debug/release)签名的sha1值
- 规划SharePoint2010的管理员密码更改
- iOS开发如何提高
- bootstrap组件