luogu#P4434. [COCI2017-2018#2] ​​Usmjeri

[COCI2017-2018#2] ​​Usmjeri

题目描述

We are given a tree with N nodes denoted with different positive integers from 1 to N. Additionally, you are given M node pairs from the tree in the form of (a1a_1 , b1b_1 ), (a2a_2 , b2b_2 ), …, (aMa_M , bMb_M ).

We need to direct each edge of the tree so that for each given node pair (aia_i , bib_i ) there is a path from aia_i to bib_i or from bib_i to aia_i . How many different ways are there to achieve this? Since the solution can be quite large, determine it modulo 109+710^{9}+7.

输入格式

The first line of input contains the positive integers N and M (1 ≤ N, M ≤ 31053*10^5), the number of nodes in the tree and the number of given node pairs, respectively.

Each of the following N - 1 lines contains two positive integers, the labels of the nodes connected with an edge.

The ithi_{th} of the following M lines contains two different positive integers aia_{i} and bib_{i} , the labels of the nodes from the ithi_{th} node pair. All node pairs will be mutually different.

输出格式

You must output a single line containing the total number of different ways to direct the edges of the tree that meet the requirement from the task, modulo 109+710^{9}+7.

题目大意

给定一颗从1到n编号的n个结点的树

同时给定m个约束,诸如(ai,bi)(a_i,b_i)

给每一条边定向,使得对于每一对约束对存在一条从aia_ibib_i或从bib_iaia_i的路径。

求可行的方案数,答案对109+710^9 + 7取模

1n,m3×1051 \le n,m \le 3 \times 10^5

给定一颗从1到n编号的n个结点的树

同时给定m个约束,诸如$(a_i,b_i)$

给每一条边定向,使得每一对约束对存在一条从$a_i$到$b_i$或从$b_i$到$a_i$的路径。

求可行的方案数,答案对$10^9 + 7$取模

$1 \le n,m \le 3 \times 10^5$
4 1
1 2
2 3
3 4
2 4
4
7 2
1 2
1 3
4 2
2 5
6 5
5 7
1 7
2 6

8
4 3
1 2
1 3
1 4
2 3
2 4
3 4

0

提示

In test cases worth 20% of total points, the given tree will be a chain. In other words, node i will be connected with an edge to node i + 1 for all i < N.

In additional test cases worth 40% of total points, it will hold N, M ≤ 51035*10^3.

A tree is a graph that consists of N nodes and N - 1 edges such that there exists a path from each node to each other node.