CF BUDDY
← Problems·

1674G · Remove Directed Edges

2000 · dfs and similar, dp, graphs

Problem: You are given a directed acyclic graph, consisting of nn vertices and mm edges. The vertices are numbered from 11 to nn. There are no multiple edges and self-loops.

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

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

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

  • inv<inv\mathit{in'}_v < \mathit{in}_v or inv=inv=0\mathit{in'}_v = \mathit{in}_v = 0;
  • outv<outv\mathit{out'}_v < \mathit{out}_v or outv=outv=0\mathit{out'}_v = \mathit{out}_v = 0.

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

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

Input Format: The first line contains two integers nn and mm (1n21051 \le n \le 2 \cdot 10^5; 0m21050 \le m \le 2 \cdot 10^5) — the number of vertices and the number of edges of the graph.

Each of the next mm lines contains two integers vv and uu (1v,un1 \le v, u \le n; vuv \neq u) — the description of an edge.

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

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

Note: In the first example, you can remove edges (1,2)(1, 2) and (2,3)(2, 3). in=[0,1,2]\mathit{in} = [0, 1, 2], out=[2,1,0]\mathit{out} = [2, 1, 0]. in=[0,0,1]\mathit{in'} = [0, 0, 1], out=[1,0,0]\mathit{out'} = [1, 0, 0]. You can see that for all vv the conditions hold. The maximum cute set SS is formed by vertices 11 and 33. They are still connected directly by an edge, so there is a path between them.

In the second example, there are no edges. Since all inv\mathit{in}_v and outv\mathit{out}_v are equal to 00, leaving a graph with zero edges is allowed. There are 55 cute sets, each contains a single vertex. Thus, the maximum size is 11.

In the third example, you can remove edges (7,1)(7, 1), (2,4)(2, 4), (1,3)(1, 3) and (6,2)(6, 2). The maximum cute set will be S={7,3,2}S = \{7, 3, 2\}. You can remove edge (7,3)(7, 3) as well, and the answer won't change.

Here is the picture of the graph from the third example:

Sample Cases

Case 1

Input

3 3
1 2
2 3
1 3

Output

2

Case 2

Input

5 0

Output

1

Case 3

Input

7 8
7 1
1 3
6 2
2 3
7 2
2 4
7 3
6 3

Output

3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback