A9680 | Mr. Kitayuta's Technology
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Shuseki Kingdom is the world's leading nation for innovation and technology. There are $n$ cities in the kingdom, numbered from $1$ to $n$ .
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city $x$ to city $y$ cannot be used to travel from city $y$ to city $x$ . The transportation within each city is extremely developed, therefore if a pipe from city $x$ to city $y$ and a pipe from city $y$ to city $z$ are both constructed, people will be able to travel from city $x$ to city $z$ instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the $m$ pairs of city $(a_{i},b_{i})$ ( $1<=i<=m$ ) is important. He is planning to construct teleportation pipes so that for each important pair $(a_{i},b_{i})$ , it will be possible to travel from city $a_{i}$ to city $b_{i}$ by using one or more teleportation pipes (but not necessarily from city $b_{i}$ to city $a_{i}$ ). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city $x$ to city $y$ cannot be used to travel from city $y$ to city $x$ . The transportation within each city is extremely developed, therefore if a pipe from city $x$ to city $y$ and a pipe from city $y$ to city $z$ are both constructed, people will be able to travel from city $x$ to city $z$ instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the $m$ pairs of city $(a_{i},b_{i})$ ( $1<=i<=m$ ) is important. He is planning to construct teleportation pipes so that for each important pair $(a_{i},b_{i})$ , it will be possible to travel from city $a_{i}$ to city $b_{i}$ by using one or more teleportation pipes (but not necessarily from city $b_{i}$ to city $a_{i}$ ). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.
输入格式
The first line contains two space-separated integers $n$ and $m$ ( $2<=n<=10^{5},1<=m<=10^{5}$ ), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.
The following $m$ lines describe the important pairs. The $i$ -th of them ( $1<=i<=m$ ) contains two space-separated integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n,a_{i}≠b_{i}$ ), denoting that it must be possible to travel from city $a_{i}$ to city $b_{i}$ by using one or more teleportation pipes (but not necessarily from city $b_{i}$ to city $a_{i}$ ). It is guaranteed that all pairs $(a_{i},b_{i})$ are distinct.
The following $m$ lines describe the important pairs. The $i$ -th of them ( $1<=i<=m$ ) contains two space-separated integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n,a_{i}≠b_{i}$ ), denoting that it must be possible to travel from city $a_{i}$ to city $b_{i}$ by using one or more teleportation pipes (but not necessarily from city $b_{i}$ to city $a_{i}$ ). It is guaranteed that all pairs $(a_{i},b_{i})$ are distinct.
输出格式
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.
输入输出样例
输入 #1
4 5 1 2 1 3 1 4 2 3 2 4
输出 #1
3
输入 #2
4 6 1 2 1 4 2 3 2 4 3 2 3 4
输出 #2
4
For the first sample, one of the optimal ways to construct pipes is shown in the image below:
For the second sample, one of the optimal ways is shown below:

For the second sample, one of the optimal ways is shown below:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted