300919: CF175F. Gnomes of Might and Magic

Memory Limit:256 MB Time Limit:8 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Gnomes of Might and Magic

题目描述

Vasya plays a popular game the Gnomes of Might and Magic. In this game Vasya manages the kingdom of gnomes, consisting of several castles, connected by bidirectional roads. The kingdom road network has a special form. The kingdom has $ m $ main castles $ a_{1},a_{2},...,a_{m} $ , which form the Good Path. This path consists of roads between the castles $ a_{i},a_{i+1} $ $ (1<=i&lt;m) $ as well as the road between $ a_{m} $ and $ a_{1} $ . There are no other roads between the castles of the Good Path. In addition, for each pair of neighboring Good Path castles $ u $ and $ v $ there is exactly one Evil Shortcut — a path that goes along the roads leading from the first castle $ (u) $ to the second one $ (v) $ and not having any common vertexes with the Good Path except for the vertexes $ u $ and $ v $ . It is known that there are no other roads and castles in the kingdom there, that is, every road and every castle lies either on the Good Path or the Evil Shortcut (castles can lie in both of them). In addition, no two Evil Shortcuts have any common castles, different than the castles of the Good Path. At the beginning of each week in the kingdom appears one very bad gnome who stands on one of the roads of the kingdom, and begins to rob the corovans going through this road. One road may accumulate multiple very bad gnomes. Vasya cares about his corovans, so sometimes he sends the Mission of Death from one castle to another. Let's suggest that the Mission of Death should get from castle $ s $ to castle $ t $ . Then it will move from castle $ s $ to castle $ t $ , destroying all very bad gnomes, which are on the roads of the Mission's path. Vasya is so tough that his Mission of Death can destroy any number of gnomes on its way. However, Vasya is very kind, so he always chooses such path between castles $ s $ and $ t $ , following which he will destroy the smallest number of gnomes. If there are multiple such paths, then Vasya chooses the path that contains the smallest number of roads among them. If there are multiple such paths still, Vasya chooses the lexicographically minimal one among them. Help Vasya to simulate the life of the kingdom in the Gnomes of Might and Magic game. A path is a sequence of castles, such that each pair of the neighboring castles on the path is connected by a road. Also, path $ x_{1},x_{2},...\ ,x_{p} $ is lexicographically less than path $ y_{1},y_{2},...\ ,y_{q} $ , if either $ p&lt;q $ and $ x_{1}=y_{1},x_{2}=y_{2},...\ ,x_{p}=y_{p} $ , or exists such number $ r $ $ (r&lt;p,r&lt;q) $ , that $ x_{1}=y_{1},x_{2}=y_{2},...\ ,x_{r}=y_{r} $ and $ x_{r+1}&lt;y_{r+1} $ .

输入输出格式

输入格式


The first line contains two integers $ n $ and $ m $ $ (3<=m<=n<=100000) $ — the number of castles in the kingdom, and the number of castles on the Good Path, respectively. The second line contains $ m $ integers, which are numbers of Good Path castles (the castles are numbered from $ 1 $ to $ n $ ) in the order of occurrence on the Path, starting with some castle. All Good Path castles are different. Each of the following $ m $ lines describes an Evil Shortcut. First a line contains an integer $ k_{i} $ $ (3<=k_{i}<=100000) $ — the number of castles on the corresponding Evil Shortcut (with the two castles which are on the Good Path), followed by a $ k_{i} $ integers — number of castles in the order of occurrence in the given Shortcut. All castles in one Evil Shortcut are different. It is guaranteed that the first and the last castles from the Shortcut are on the Good Path and the first castles in the Evil Shortcuts form the Good Path and are presented in the same order in which the Path was represented on the second line. The next line contains an integer $ q $ $ (1<=q<=100000) $ — the number of events in the life of the kingdom. Each of the following $ q $ lines describes a single event. An event is described by the symbol $ c_{j} $ and two numbers or castles $ s_{j} $ and $ t_{j} $ (the character and numbers of castles are separated by a single space). If the character of $ c_{j} $ is equal to "+" (a plus), it means that a very bad gnome (probably not the first one) has appeared on the road between castles $ s_{j} $ and $ t_{j} $ . If $ c_{j} $ equals "?" (a question), then Vasya sent a Mission of Death from castle $ s_{j} $ to castle $ t_{j} $ . It is guaranteed that for each request "+", the road between castles $ s_{j} $ and $ t_{j} $ exists. The events are given in chronological order, starting with the earliest one. Initially there are no very bad gnomes on the roads. All numbers in all lines are separated by single spaces. It is guaranteed that all the given Evil Shortcuts and Good Path fit in the limitations given in the problem statement.

输出格式


For each query "?" print a single number on a single line — the number of very bad gnomes destroyed by the corresponding Mission of Death. Print the answers to queries in the chronological order.

输入输出样例

输入样例 #1

6 3
1 2 3
3 1 4 2
3 2 5 3
3 3 6 1
10
+ 1 2
+ 4 2
+ 1 3
+ 2 3
? 1 2
+ 2 5
? 1 2
? 1 2
+ 1 2
? 1 2

输出样例 #1

0
1
0
1

说明

In the example after the first four requests there is only one path from castle 1 to castle 2, which does not contain roads with very bad gnomes: 1 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 6 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 3 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 5 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 2. After a gnome stood on the road (2, 5), the next Mission of Death moves along path 1 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 2, and destroys the gnome, who was on the road (1, 2). The next Mission of Death follows the same path which is already free of gnomes. After yet another gnome stood on the road (1, 2), the next Mission of Death goes on the path 1 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF175F/355fee5161a1808ee95ea5dc6d815d4071657131.png) 2, and kills the gnome.

Input

题意翻译

Vasya 最近正在玩一款叫作`迷你世界之侏儒魔法门`的游戏。 游戏中 Vasya 管理着迷你世界侏儒王国,王国中总共有 $n$ 个城堡,其中有 $m$ 个主要的城堡,中间用好路链接,第 $i$ 条好路连接着 $a_i$ 与 $a_{i+1}$,特别的,第 $m$ 条好路连接 $a_m$ 与 $a_1$,除了这 $m$ 条“好路”,城堡与城堡之间没有其他“好路”。 相应的,每条好路连接的两个端点都引出去一条坏路,这个坏路保证: 1. 坏路的开头和结尾一定是有好路连接的两个城堡。 2. 坏路除开头和结尾外的城堡都不是主要城堡。 3. 没有任何一个非主要城堡被两条坏路经过。 4. 每个城堡都在坏路上,其中有一些在好路上。 好路和坏路都是无向边。 每个时刻,都会有某一条道路上出现了张献忠,如果有人经过这条道路,那么张献忠就会把那个人图图了,Vasya 作为这个国家的领导人,发现这样不行,决定派出刘进忠从 $S$ 点前往 $T$ 点,刘进忠走到哪图到哪,他会选择一条从 $S$ 点到 $T$ 点的路径使得走这条路径图图的张献忠最少,如果有多条路径,选择长度最小的,如果有多条路径,选择字典序最小的。 注意:每次询问不独立。 输入格式: 第一行,两个整数 $n$ 和 $m$ 表示王国中共有多少城堡以及其中主要城堡的数量。 第二行,$n$ 个整数 $a_i$,表示主要城堡的编号。 后面 $m$ 行,第 $i$ 行首先输入一个整数 $k_i$,表示这条坏路的长度,然后接 $k_i$ 个整数来描述这条坏路。 然后一个整数 $q$ 表示询问或修改的次数。 后面 $q$ 行,每行一个操作,形如: 1. `+ u v`,表示从 $u$ 到 $v$ 的边中多出现了一个张献忠。 2. `? u v`,表示求出从 $u$ 图到 $v$,取到的最合法的路径中图图的侏儒数量,并动手把这条路径上的所有侏儒图图干净。(具体要求看题面) 输出格式: 对于每个 $?$ 操作,输出答案。 翻译者:Reliauk

加入题单

上一题 下一题 算法标签: