A10849 | Timofey and our friends animals
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
After his birthday party, Timofey went to his favorite tree alley in a park. He wants to feed there his favorite birds — crows.
It's widely known that each tree is occupied by a single crow family. The trees in the alley form a row and are numbered from $1$ to $n$ . Some families are friends to each other. For some reasons, two families can be friends only if they live not too far from each other, more precisely, there is no more than $k-1$ trees between any pair of friend families. Formally, the family on the $u$ -th tree and the family on the $v$ -th tree can be friends only if $|u-v|<=k$ holds.
One of the friendship features is that if some family learns that Timofey is feeding crows somewhere, it notifies about this all friend families. Thus, after Timofey starts to feed crows under some tree, all the families that are friends to the family living on this tree, as well as their friends and so on, fly to the feeding place. Of course, the family living on the tree also comes to the feeding place.
Today Timofey came to the alley and noticed that all the families that live on trees with numbers strictly less than $l$ or strictly greater than $r$ have flown away. Thus, it is not possible to pass the information about feeding through them. Moreover, there is no need to feed them. Help Timofey to learn what is the minimum number of trees under which he has to feed crows so that all the families that have remained will get the information about feeding. You are given several situations, described by integers $l$ and $r$ , you need to calculate the answer for all of them.
It's widely known that each tree is occupied by a single crow family. The trees in the alley form a row and are numbered from $1$ to $n$ . Some families are friends to each other. For some reasons, two families can be friends only if they live not too far from each other, more precisely, there is no more than $k-1$ trees between any pair of friend families. Formally, the family on the $u$ -th tree and the family on the $v$ -th tree can be friends only if $|u-v|<=k$ holds.
One of the friendship features is that if some family learns that Timofey is feeding crows somewhere, it notifies about this all friend families. Thus, after Timofey starts to feed crows under some tree, all the families that are friends to the family living on this tree, as well as their friends and so on, fly to the feeding place. Of course, the family living on the tree also comes to the feeding place.
Today Timofey came to the alley and noticed that all the families that live on trees with numbers strictly less than $l$ or strictly greater than $r$ have flown away. Thus, it is not possible to pass the information about feeding through them. Moreover, there is no need to feed them. Help Timofey to learn what is the minimum number of trees under which he has to feed crows so that all the families that have remained will get the information about feeding. You are given several situations, described by integers $l$ and $r$ , you need to calculate the answer for all of them.
输入格式
The first line contains integers $n$ and $k$ ( $1<=n<=10^{5}$ , $1<=k<=5$ ), where $n$ is the number of trees, and $k$ is the maximum possible distance between friend families.
The next line contains single integer $m$ ( $0<=m<=n·k$ ) — the number of pair of friend families.
Each of the next $m$ lines contains two integers $u$ and $v$ ( $1<=u,v<=10^{5}$ ), that means that the families on trees $u$ and $v$ are friends. It is guaranteed that $u≠v$ and $|u-v|<=k$ . All the given pairs are distinct.
The next line contains single integer $q$ ( $1<=q<=10^{5}$ ) — the number of situations you need to calculate the answer in.
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1<=l<=r<=10^{5}$ ), that means that in this situation families that have flown away lived on such trees $x$ , so that either $x<l$ or $x>r$ .
The next line contains single integer $m$ ( $0<=m<=n·k$ ) — the number of pair of friend families.
Each of the next $m$ lines contains two integers $u$ and $v$ ( $1<=u,v<=10^{5}$ ), that means that the families on trees $u$ and $v$ are friends. It is guaranteed that $u≠v$ and $|u-v|<=k$ . All the given pairs are distinct.
The next line contains single integer $q$ ( $1<=q<=10^{5}$ ) — the number of situations you need to calculate the answer in.
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1<=l<=r<=10^{5}$ ), that means that in this situation families that have flown away lived on such trees $x$ , so that either $x<l$ or $x>r$ .
输出格式
Print $q$ lines. Line $i$ should contain single integer — the answer in the $i$ -th situation.
输入输出样例
输入 #1
5 3 3 1 3 2 3 4 5 5 1 1 1 2 2 3 1 3 1 5
输出 #1
1 2 1 1 2
In the first example the following family pairs are friends: $(1,3)$ , $(2,3)$ and $(4,5)$ .
- In the first situation only the first family has remained, so the answer is $1$ .
- In the second situation the first two families have remained, and they aren't friends, so the answer is $2$ .
- In the third situation the families $2$ and $3$ are friends, so it is enough to feed any of them, the answer is $1$ .
- In the fourth situation we can feed the first family, then the third family will get the information from the first family, and the second family will get the information from the third. The answer is $1$ .
- In the fifth situation we can feed the first and the fifth families, so the answer is $2$ .
- In the first situation only the first family has remained, so the answer is $1$ .
- In the second situation the first two families have remained, and they aren't friends, so the answer is $2$ .
- In the third situation the families $2$ and $3$ are friends, so it is enough to feed any of them, the answer is $1$ .
- In the fourth situation we can feed the first family, then the third family will get the information from the first family, and the second family will get the information from the third. The answer is $1$ .
- In the fifth situation we can feed the first and the fifth families, so the answer is $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted