Codeforces 231e| cactus| Edge Double Unicom component | Lca__codeforces

Source: Internet
Author: User

Given a cactus map, ask a number of paths between two points multiple times.

It is found that the number of paths between two points equals the power of the number of path rings of 2.
So the number of loops that the path passes after the Tarjan is calculated.

Actually want to change into RMQ, but too lazy.

#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 = 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 the of this graph belongs to at most one simple cycle.

A simple cycle in a undirected the graph is a sequence of distinct vertices v1, v2, ..., VT (T > 2), such that to 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 are a sequence of not necessarily distinct vertices v1, v2, ..., VT (T > 0), such that For any I (1≤i < T) exists a edge between vertices VI and VI + 1 and furthermore each edge occurs no more than once . We'll say that's 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, which is a vertex cactus. Also, you ' ve got a list of k pairs of interesting vertices xi., Yi, for which and want to know the following information- The number of distinct simple paths this start at Vertex Xi and end in Vertex Yi. We'll consider two simple paths distinct if the sets of edges of the paths are.

For each pair of interesting vertices count the number of distinct simple paths between them. As this number can is rather large, you should calculate it modulo 1000000007 (109 + 7). Input

The 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 (1≤xi, yi≤n xi≠yi)-the indexes of interesting vertices in the i-th pair.

It is guaranteed this given graph is a vertex cactus. It is guaranteed that 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

1 2
2 3 3 4 1 4 3 5 5 6 8 6 8 7 7 6 7 9 9
ten
6 1 2 3 5
6 9
9 2
9 3
9 10
Output
2
2
2
4
4
1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.