CF231E.Cactus

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 _v_1, _v_2, ..., v__t (t > 2), such that for any i (1 ≤ i < t) exists an edge between vertices v__i and v__i + 1, and also exists an edge between vertices _v_1 and v__t.

A simple path in a undirected graph is a sequence of not necessarily distinct vertices _v_1, _v_2, ..., v__t (t > 0), such that for any i (1 ≤ i < t) exists an edge between vertices v__i and v__i + 1 and furthermore each edge occurs no more than once. We'll say that a simple path _v_1, _v_2, ..., v__t starts at vertex _v_1 and ends at vertex v__t.

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 x__i, y__i, for which you want to know the following information — the number of distinct simple paths that start at vertex x__i and end at vertex y__i. 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).

一个连通的无向图被称为点仙人掌图(vertex cactus),当且仅当该图中每个顶点至多属于一个简单环。

无向图中的一个简单环(simple cycle) 是一个由互不相同的顶点 v1, v2, …, vtv_1,\,v_2,\,\dots,\,v_t(其中 t>2t > 2)构成的序列,使得对任意 ii(1≤i<t1 \le i < t),顶点 viv_i 与 vi+1v_{i+1} 之间存在一条边,且顶点 v1v_1 与 vtv_t 之间也存在一条边。

无向图中的一个简单路径(simple path) 是一个顶点序列 v1, v2, …, vtv_1,\,v_2,\,\dots,\,v_t(其中 t>0t > 0),这些顶点不一定互不相同,满足:对任意 ii(1≤i<t1 \le i < t),顶点 viv_i 与 vi+1v_{i+1} 之间存在一条边,且图中每条边在该路径中至多出现一次。我们称简单路径 v1, v2, …, vtv_1,\,v_2,\,\dots,\,v_t 起始于顶点 v1v_1、终止于顶点 vtv_t。

现给定一个由 nn 个顶点和 mm 条边构成的图,它是一个点仙人掌图;同时还给定一个包含 kk 对“感兴趣顶点” (xi, yi)(x_i,\,y_i) 的列表。对于每一对 (xi, yi)(x_i,\,y_i),你需要计算:起始于顶点 xix_i、终止于顶点 yiy_i 的不同简单路径的数量。若两条简单路径所含的边集不同,则认为它们是不同的路径。

对每一对感兴趣顶点,计算它们之间不同简单路径的数量。由于该数量可能非常大,请将结果对 10000000071000000007(即 109+710^9 + 7)取模。

输入格式

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 a__i, b__i (1 ≤ a__i, b__i ≤ 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 x__i, y__i (1 ≤ x__i, y__i ≤ n; x__i ≠ y__i) — 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.

第一行包含两个以空格分隔的整数 nn、mm(2≤n≤1052 \leq n \leq 10^5;1≤m≤1051 \leq m \leq 10^5),分别表示图中的顶点数和边数。接下来的 mm 行描述各条边:第 ii 行包含两个以空格分隔的整数 aia_i、bib_i(1≤ai,bi≤n1 \leq a_i, b_i \leq n),表示第 ii 条边所连接的两个顶点的编号。

下一行包含一个整数 kk(1≤k≤1051 \leq k \leq 10^5),表示“感兴趣顶点对”的数量。接下来的 kk 行列出所有感兴趣顶点对:第 ii 行包含两个以空格分隔的数 xix_i、yiy_i(1≤xi,yi≤n1 \leq x_i, y_i \leq n;xi≠yix_i \neq y_i),表示第 ii 对中两个感兴趣顶点的编号。

保证所给图是一个顶点仙人掌图(vertex cactus)。保证图中不含自环或重边。图中顶点编号为 11 到 nn。

输出格式

Print k lines: in the i-th line print a single integer — the number of distinct simple ways, starting at x__i and ending at y__i, modulo 1000000007 (109 + 7).

输出 k 行:第 i 行输出一个整数——从 x__i 出发、到 y__i 结束的不同的简单路径的数量,对 1000000007(即 109+710^9 + 7)取模。

输入输出样例

  • 输入#1

    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

    输出#1

    2
    2
    2
    4
    4
    1

输入解题思路,AI测评打分。不知道怎么写?

首页