题库练习 Remove Directed Edges
← 上一题 下一题 →

A14980 | Remove Directed Edges

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

You are given a directed acyclic graph, consisting of $n$ vertices and $m$ edges. The vertices are numbered from $1$ to $n$ . There are no multiple edges and self-loops.

Let $\mathit{in}_v$ be the number of incoming edges (indegree) and $\mathit{out}_v$ be the number of outgoing edges (outdegree) of vertex $v$ .

You are asked to remove some edges from the graph. Let the new degrees be $\mathit{in'}_v$ and $\mathit{out'}_v$ .

You are only allowed to remove the edges if the following conditions hold for every vertex $v$ :

- $\mathit{in'}_v < \mathit{in}_v$ or $\mathit{in'}_v = \mathit{in}_v = 0$ ;
- $\mathit{out'}_v < \mathit{out}_v$ or $\mathit{out'}_v = \mathit{out}_v = 0$ .

Let's call a set of vertices $S$ cute if for each pair of vertices $v$ and $u$ ( $v \neq u$ ) such that $v \in S$ and $u \in S$ , there exists a path either from $v$ to $u$ or from $u$ to $v$ over the non-removed edges.

What is the maximum possible size of a cute set $S$ after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to $0$ ?

输入格式

The first line contains two integers $n$ and $m$ ( $1 \le n \le 2 \cdot 10^5$ ; $0 \le m \le 2 \cdot 10^5$ ) — the number of vertices and the number of edges of the graph.

Each of the next $m$ lines contains two integers $v$ and $u$ ( $1 \le v, u \le n$ ; $v \neq u$ ) — the description of an edge.

The given edges form a valid directed acyclic graph. There are no multiple edges.

输出格式

Print a single integer — the maximum possible size of a cute set $S$ after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to $0$ .

输入输出样例

输入 #1
3 3
1 2
2 3
1 3
输出 #1
2
输入 #2
5 0
输出 #2
1
输入 #3
7 8
7 1
1 3
6 2
2 3
7 2
2 4
7 3
6 3
输出 #3
3
C++ 编辑器
输入
输出