zoukankan      html  css  js  c++  java
  • 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 belongs to at most one simple cycle.

    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.

    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 pathv1, 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 verticesxi, 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 xiyi (1 ≤ xi, yi ≤ nxi ≠ 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, modulo1000000007 (109 + 7).

    Examples
    input
    10 11
    1 2
    2 3
    3 4
    1 4
    3 5
    5 6
    8 6
    8 7
    7 6
    7 9
    9 10
    6
    1 2
    3 5
    6 9
    9 2
    9 3
    9 10
    output
    2
    2
    2
    4
    4
    1

     题意:

       给你n个点,m条边的无向图,给出下面定义

      一般简单路的定义是一条无重复边和不经过重复点的路径,题述的定义是:可以经过重复点但无重复边的路径  

      无向图中的任意一点只属于一个简单环,然后询问任何两点间有多少条不同的简单路。

    题解:  

      任意一点只属于一个简单环

      我们先缩环

      每个环当做点,那么在询问a到b的时候,环中点个数超过1的时候 就是存在两种走法,否则是1种,这个我们将它当作点权就好

      就相当于 求出一个树的LCA和点权乘

      每个点权求法和求lca中fa数组是一样的

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 1e5+10, M = 1e6, mod = 1e9+7, inf = 2e9;
    
    int n,m,low[N],dfn[N],inq[N],q[N],top,tot,t,head[N],hav[N],scc,belong[N];
    
    struct node{int to,next,id;}e[N * 2];
    void add(int u,int v) {e[t].next=head[u];e[t].to=v;e[t].id=0;head[u]=t++;}
    
    int dp[N][30],fa[N][30],dep[N];
    vector<int > G[N];
    void dfs(int u) {
            dfn[u] = low[u] = ++tot;
            q[++top] = u; inq[u] = 1;
            for(int i = head[u]; i; i = e[i].next) {
                int to = e[i].to;
                if(e[i].id) continue;
                e[i].id = e[i^1].id = 1;
                if(!dfn[to]) {
                    dfs(to);
                    low[u] = min(low[u],low[to]);
                } else if(inq[to]) low[u] = min(low[u],dfn[to]);
            }
            if(low[u] == dfn[u]) {
                scc++;
                do{
                    inq[q[top]] = 0;
                    belong[q[top]] = scc;
                    hav[scc] += 1;
                } while(u != q[top--]);
            }
    }
    void rebuild() {
            for(int i = 1; i <= n; ++i) {
                for(int j = head[i]; j; j = e[j].next) {
                    int to = e[j].to;
                    int x = belong[to];
                    int y = belong[i];
                    if(x != y) {
                        G[x].push_back(y);
                    }
                }
            }
    }
    void Tarjan() {
            for(int i = 1; i <= n; ++i) if(!dfn[i]) dfs(i);
            rebuild();
            for(int i = 1; i <= scc; ++i) hav[i] = min(hav[i],2);
    }
    
    ////
    void lca_dfs(int u,int p,int d) {
            fa[u][0] = p, dep[u] = d;
            dp[u][0] = hav[u];
            for(int i = 0; i < G[u].size(); ++i) {
                int to = G[u][i];
                if(to == p) continue;
                lca_dfs(to,u,d+1);
            }
    }
    void lca_init() {
            for(int i = 1; i <= 22; ++i) {
                for(int j = 1; j <= n; ++j) {
                    if(fa[j][i-1]) {
                        dp[j][i] = (1ll * dp[j][i-1] * dp[fa[j][i-1]][i-1]) % mod;
                        fa[j][i] = fa[fa[j][i-1]][i-1];
                    } else {
                        fa[j][i] = 0;
                        dp[j][i] = 1;
                    }
                }
            }
    }
    int lca(int x,int y) {
            if(dep[x] > dep[y]) swap(x,y);
            int ret = 1;
            for(int k = 0; k < 22; ++k) {
                if( (dep[y] - dep[x])>>k & 1)
                    ret = 1LL * ret * dp[y][k] % mod, y = fa[y][k];
            }
            if(x == y) return 1LL * ret * hav[x] % mod;
            for(int k = 21; k >= 0; --k) {
                if(fa[x][k] != fa[y][k]) {
                    ret = 1LL * ret * dp[x][k] % mod;
                    ret  = 1LL * ret * dp[y][k] % mod;
                    x = fa[x][k];
                    y = fa[y][k];
                }
            }
            return 1LL * ret * dp[x][0] % mod * dp[y][0] % mod * hav[fa[x][0]]% mod;
    }
    int main() {
            scanf("%d%d",&n,&m);
            for(int i = 1; i <= m; ++i) {
                int a,b;
                scanf("%d%d",&a,&b);
                add(a,b);
                add(b,a);
            }
            Tarjan();
            lca_dfs(1,0,0);
            lca_init();
            int q;
            scanf("%d",&q);
            while(q--) {
                int a,b;
                scanf("%d%d",&a,&b);
                if(belong[a] == belong[b]) {
                    puts("2");continue;
                }
                printf("%d
    ",lca(belong[a],belong[b]));
            }
            return 0;
    }

      

  • 相关阅读:
    UEFI启动 安装win8 win10 及windows server 2012 最简单的方法
    Android SDK中国在线更新镜像服务器 解决GOOGLE更新无法下载 更新失败的问题
    DELPHI 单元文件结构
    获取程序自身大小的2个函数
    实时获取网络时间 并转换为北京时间的函数
    部署maven的一些要点、遇到的问题
    cron表达式详解
    redhat安装xwindow环境
    tomcat执行文件权限
    一个方便的java分页算法
  • 原文地址:https://www.cnblogs.com/zxhl/p/5929191.html
Copyright © 2011-2022 走看看