Codeforces Round #143 (Div. 2) E. Cactus(LCA+无向图缩点)

E. Cactus
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 vertexyi. 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).

Sample test(s)
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


题意:

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

一般简单路的定义是一条无重复边和不经过重复点的路径,题述的定义是:可以经过重复点但无重复边的路径,所以这种定义性问题还是仔细读读题。

思路:由此图的特殊性可知,缩点成树以后,路径每经过一个缩点,就会有两种不同的选择,所以可以lca求任何两个节点间的缩点个数



此图的特殊性缩点很简单,dfs一下就可以了,但是一般的无向图的缩点需要tarjan算法,自己的无向图缩点模版是:

int dfn[N],sta[N],top,scc,index,col[N],low[N];
void tarjan(int u,int pa)
{
    dfn[u] = low[u] = ++index;
    sta[++top] = u;
    for(int i = head[u];~i;i = es[i].nxt)
    {
        int v = es[i].v;
        if(v == pa) continue;
        if(dfn[v] == 0)
        {
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u])
            {
                ++scc;
                int vv;
                do
                {
                    vv = sta[top--];
                    col[vv] = scc;
                }while(vv != v);
            }
        }
        else low[u] = min(low[u],dfn[v]);
    }
}

scc++;
while(top) col[sta[top--]] = scc; //栈中剩余的双连通分量别漏了



#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii;


int n,m;
const int N = 1e5+100;
struct Edge{
        int v,nxt;
        Edge(){}
        Edge(int v,int nxt):v(v),nxt(nxt){};
}es[N*2],es2[N*2];
int ecnt,ecnt2;
int head[N],head2[N];
inline void add_edge(int u,int v){
        es[++ecnt] = Edge(v,head[u]);
        head[u] = ecnt;
        es[++ecnt] = Edge(u,head[v]);
        head[v] = ecnt;
}
bool is_scc[N];
int dfn[N],sta[N],top,scc,index,col[N],low[N];
void tarjan(int u,int pa)
{
    dfn[u] = low[u] = ++index;
    sta[++top] = u;
    for(int i = head[u];~i;i = es[i].nxt)
    {
        int v = es[i].v;
        if(v == pa) continue;
        if(dfn[v] == 0)
        {
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u])
            {
                ++scc;
                int vv;
                do
                {
                    vv = sta[top--];
                    col[vv] = scc;
                }while(vv != v);
            }
        }
        else low[u] = min(low[u],dfn[v]);
    }
}
int dp[N];
int dep[N];
bool vis[N];
int pa[N][20];
void bfs()
{
    queue<int>q;
    q.push(1);
    pa[1][0] = 1;
    vis[1] = 1;
    dp[1] = is_scc[1];
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        for(int i = 1;i < 20; i++) pa[u][i] = pa[pa[u][i-1]][i-1];
        for(int i = head2[u];~i;i = es2[i].nxt)
        {
            int v = es2[i].v;
            if(vis[v] == 0)
            {
                vis[v] = 1;
                dp[v] = dp[u]+is_scc[v];
                pa[v][0] = u;
                dep[v] = dep[u]+1;
                q.push(v);
            }
        }
    }
}
int LCA(int u,int v)
{
    if(dep[u]>dep[v]) swap(u,v);
    for(int det=dep[v]-dep[u],i=0;det;i++,det>>=1)
        if(det&1) v=pa[v][i];
    if(v==u) return v;
    for(int i=20-1;i>=0;i--)
        if(pa[u][i]!=pa[v][i]) v=pa[v][i],u=pa[u][i];
    return pa[u][0];
}
int pw[N];
const int MOD = 1e9+7;
int main(){

        memset(head,-1,sizeof(head));
        memset(head2,-1,sizeof(head2));
        pw[0] = 1;
        REP(i,N-10) pw[i] = pw[i-1]*2%MOD;
        cin>>n>>m;
        REP(i,m){
                int u,v;
                scanf("%d%d",&u,&v);
                add_edge(u,v);
        }
        REP(i,n) if(dfn[i] == 0) tarjan(i,-1);
        scc++;
        while(top) col[sta[top--]] = scc;
        REP(u,n){
                for(int i = head[u];~i;i = es[i].nxt){
                        int v = es[i].v;
                        if(col[u] == col[v]) {
                                is_scc[col[u]] = 1;
                                continue;
                        }
                        es2[++ecnt2] = Edge(col[v],head2[col[u]]);
                        head2[col[u]] = ecnt2;
                }
        }
        bfs();
        int Q;
        cin>>Q;
        REP(i,Q){
                int u,v;
                scanf("%d%d",&u,&v);
                u = col[u],v = col[v];
                int lca = LCA(u,v);
                int cnt = dp[u]+dp[v]-2*dp[lca]+is_scc[lca];
                printf("%d\n",pw[cnt]);
        }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值