Typical ICPC Problem
Suppose that you advanced to the ICPC finals. Of course, there will be some cactus problem! To make all participants suffer, even more, it's an edge cactus. As a cherry on top, the input format is unusual. Perfect problem!
You are given an edge cactus on nodes. Each node contains an integer from to such that the numbers in the nodes form a permutation. Let's call a pair of numbers with good, if the following condition holds:
Consider only those nodes, integers on which are in a range . Consider only those edges between these nodes, which are present in the original cactus. Then the pair is called good if and only if the obtained graph is connected.
Count the number of good pairs.
As a reminder, an edge cactus is a graph such that each edge in it is contained in at most one simple cycle.
As another reminder, a simple cycle is a sequence of distinct nodes (at least ), such that every two adjacent nodes are connected with an edge, and also the first and the last nodes are connected with an edge.
Giriş verilənləri
The first line of the input contains integers (). The edges of the cactus are given by edge-simple paths.
The second line of the input contains integers (, all are distinct), where is the number written in the -th node.
Next lines contain descriptions of paths, each of which looks as follows:
The first number in a line is an integer () — the number of nodes on the path. integers (, are not neccessarily distinct) follow, meaning that there is an edge between nodes and for each from to . Note that the same node can appear more than once here.
It's guaranteed that each edge will be described at most once and that there are no multi-edges or self-loops.
Çıxış verilənləri
Output a single integer — the number of pairs (, such that pair is good.
Nümunələr
Qeyd
In the first sample we have a cactus with edges and . Pairs are good, while pair isn't.
In the second sample we have a cactus with edges , , , , , .