Codeforces Round #143 (Div. 2)--E. Cactus--缩点+LCA

https://codeforces.com/problemset/problem/231/E

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.

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 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, modulo 1000000007 (109 + 7).

Examples

input

Copy

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

Copy

2
2
2
4
4
1

给出一个仙人掌无向图,问a-b的简单路径有多少。

就是a-b所经过的简单环cnt相关的答案   2^{cnt}

先缩点,把环缩成点,形成一个树,从头对新点进行标号,进行建立一个树。

for(u=1; u<=n; u++)
    {
        for(int i=0; i<G[u].size(); i++)
        {
            int v=G[u][i];
            if(fa[u]!=fa[v])
                tree[fa[u]].push_back(fa[v]);//建一颗树
        }
    }

然后进行dfs,遍历这棵树,求出dist数组,含义为i点到根点经过的环的个数。

然后进行求lca(类似于求lca的思想),u,v两点之间的环数为

 dist[u]+dist[v]-2*dist[lca]+tot[lca]>1

参考

https://blog.csdn.net/xbb224007/article/details/83692429

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
#include<vector>
#include<algorithm>
#define ll long long
#define mod 1000000007
using namespace std;
const int maxn=1e5+10;
stack<int>S;
vector<int>G[maxn],tree[maxn];
int n,m,q,dfs_clk,bcc_cnt,low[maxn],vis[maxn],p[maxn];
int dist[maxn],pre[maxn],fa[maxn],ans[maxn],tot[maxn];
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll ksm(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)
            res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
void tarjan(int u,int f)
{
    pre[u]=low[u]=++dfs_clk;
    int sz=G[u].size();
    S.push(u);
    for(int i=0; i<sz; i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(v!=f)
            low[u]=min(low[u],pre[v]);
    }
    if(pre[u]==low[u])
    {
        bcc_cnt++;
        while(1)
        {
            int x=S.top();
            S.pop();
            fa[x]=bcc_cnt;//看其属于哪个连通分量
            tot[bcc_cnt]++;//缩点之后形成新的点a'
            if(x==u)
                break;
        }
    }
}
void dfs(int u,int f,int d)
{
    dist[u]=d+(tot[u]>1);
    vis[u]=1;//dfs处理出dist数组
    int sz=tree[u].size();
    for(int i=0; i<sz; i++)
    {
        if(tree[u][i]!=f)
            dfs(tree[u][i],u,dist[u]);
    }
}
struct Query
{
    int v,id;
    Query(int _v=0,int _id=0)
    {
        v=_v;
        id=_id;
    }
};
vector<Query>Q[maxn];
void lca(int u,int f)
{
    int sz=tree[u].size();
    for(int i=0; i<sz; i++)
    {
        int v=tree[u][i];
        if(v==f)
            continue;
        lca(v,u);
        vis[v]=1;
        p[find(v)]=find(u);//并查集
    }
    for(int i=0; i<Q[u].size(); i++)
    {
        int v=Q[u][i].v,id=Q[u][i].id;
        if(vis[v])
        {
            int d=dist[u]+dist[v]-2*dist[find(v)]+(tot[find(v)]>1);
            ans[id]=ksm(2,d);
        }
    }
}
int main()
{
    int u,v;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i=1; i<=n; i++)
        if(!pre[i])
            tarjan(i,-1);
    for(u=1; u<=n; u++)
    {
        for(int i=0; i<G[u].size(); i++)
        {
            int v=G[u][i];
            if(fa[u]!=fa[v])
                tree[fa[u]].push_back(fa[v]);//建一颗树
        }
    }
    for(int i=1; i<=bcc_cnt; i++)
    {
        if(!vis[i])
            dfs(i,-1,0);
        p[i]=i;
    }
    memset(vis,0,sizeof vis);
    scanf("%d",&q);
    for(int i=0; i<q; i++)
    {
        scanf("%d%d",&u,&v);
        if(fa[u]==fa[v])
        {
            ans[i]=2;//在一个环中
            continue;
        }
        Q[fa[u]].push_back(Query(fa[v],i));
        Q[fa[v]].push_back(Query(fa[u],i));
    }
    for(int i=1; i<=bcc_cnt; i++)
    {
        if(!vis[i])
            lca(i,-1);
    }
    for(int i=0; i<q; i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值