CF1062F.Upgrading Cities
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n cities in the kingdom X, numbered from 1 through n. People travel between cities by some one-way roads. As a passenger, JATC finds it weird that from any city u, he can't start a trip in it and then return back to it using the roads of the kingdom. That is, the kingdom can be viewed as an acyclic graph.
Being annoyed by the traveling system, JATC decides to meet the king and ask him to do something. In response, the king says that he will upgrade some cities to make it easier to travel. Because of the budget, the king will only upgrade those cities that are important or semi-important. A city u is called important if for every city v=u, there is either a path from u to v or a path from v to u. A city u is called semi-important if it is not important and we can destroy exactly one city v=u so that u becomes important.
The king will start to act as soon as he finds out all those cities. Please help him to speed up the process.
王国 X 中有 n 座城市,编号从 1 到 n。人们通过若干单向道路在城市之间通行。作为一名乘客,JATC 发现一件奇怪的事:对于任意城市 u,他都无法从 u 出发,沿着王国的道路旅行后最终返回 u。也就是说,该王国的路网可被视作一个有向无环图(DAG)。
由于对当前交通系统感到不满,JATC 决定面见国王并请求他采取一些措施。作为回应,国王表示他将升级部分城市以改善出行体验。但由于预算限制,国王只会升级那些“重要”或“半重要”的城市。
一座城市 u 被称为重要的,当且仅当对任意城市 v=u,都存在一条从 u 到 v 的路径,或存在一条从 v 到 u 的路径。
一座城市 u 被称为半重要的,当且仅当它不是重要的,但存在恰好一个城市 v=u,使得若将 v 彻底移除(即删除该城市及其所有关联的道路),则 u 变为重要的。
国王将在确定所有此类城市后立即开始行动。请帮助他加快这一进程。
输入格式
The first line of the input contains two integers n and m (2≤n≤300000, 1≤m≤300000) — the number of cities and the number of one-way roads.
Next m lines describe the road system of the kingdom. Each of them contains two integers ui and vi (1≤ui,vi≤n, ui=vi), denoting one-way road from ui to vi.
It is guaranteed, that the kingdoms' roads make an acyclic graph, which doesn't contain multiple edges and self-loops.
输入的第一行包含两个整数 n 和 m(2≤n≤300000,1≤m≤300000)—— 分别表示城市的数量和单向道路的数量。
接下来的 m 行描述王国的道路系统。每行包含两个整数 ui 和 vi(1≤ui,vi≤n,ui=vi),表示一条从城市 ui 到城市 vi 的单向道路。
保证王国的道路构成一个有向无环图(DAG),且不含重边和自环。
输出格式
Print a single integer — the number of cities that the king has to upgrade.
输出一个整数——国王需要升级的城市数量。
输入输出样例
输入#1
7 7 1 2 2 3 3 4 4 7 2 5 5 4 6 4
输出#1
4
输入#2
6 7 1 2 2 3 3 4 1 5 5 3 2 6 6 4
输出#2
4
说明/提示
In the first example:

- Starting at the city 1 we can reach all the other cities, except for the city 6. Also, from the city 6 we cannot reach the city 1. Therefore, if we destroy the city 6 then the city 1 will become important. So 1 is a semi-important city.
- For city 2, the set of cities that cannot reach 2 and cannot be reached by 2 is 6. Therefore, destroying city 6 will make the city 2 important. So city 2 is also semi-important.
- For city 3, the set is 5,6. As you can see, destroying either city 5 or 6 will not make the city 3 important. Therefore, it is neither important nor semi-important.
- For city 4, the set is empty. So 4 is an important city.
- The set for city 5 is 3,6 and the set for city 6 is 3,5. Similarly to city 3, both of them are not important nor semi-important.
- The city 7 is important since we can reach it from all other cities.
So we have two important cities (4 and 7) and two semi-important cities (1 and 2).
In the second example, the important cities are 1 and 4. The semi-important cities are 2 and 3.
在第一个例子中:

- 从城市 1 出发,我们可以到达所有其他城市,除了城市 6;同时,从城市 6 也无法到达城市 1。因此,若摧毁城市 6,则城市 1 将变为重要城市。故 1 是一个半重要城市。
- 对于城市 2,无法到达 2 且不能被 2 到达的城市集合为 {6}。因此,摧毁城市 6 可使城市 2 变为重要城市。故城市 2 也是一个半重要城市。
- 对于城市 3,该集合为 {5,6}。可以看出,仅摧毁城市 5 或仅摧毁城市 6 均无法使城市 3 变为重要城市。因此,它既不是重要城市,也不是半重要城市。
- 对于城市 4,该集合为空集。故 4 是一个重要城市。
- 城市 5 对应的集合为 {3,6},城市 6 对应的集合为 {3,5}。与城市 3 类似,它们均既非重要城市,也非半重要城市。
- 城市 7 是一个重要城市,因为所有其他城市均可到达它。
因此,我们共有两个重要城市(4 和 7)以及两个半重要城市(1 和 2)。
在第二个例子中,重要城市是 1 和 4;半重要城市是 2 和 3。
输入解题思路,AI测评打分。不知道怎么写?