CF1674G.Remove Directed Edges
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 inv be the number of incoming edges (indegree) and outv 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 in′v and out′v .
You are only allowed to remove the edges if the following conditions hold for every vertex v :
- in′v<inv or in′v=inv=0 ;
- out′v<outv or out′v=outv=0 .
Let's call a set of vertices S cute if for each pair of vertices v and u ( v=u ) such that v∈S and u∈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≤n≤2⋅105 ; 0≤m≤2⋅105 ) — 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≤v,u≤n ; v=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
说明/提示
In the first example, you can remove edges (1,2) and (2,3) . in=[0,1,2] , out=[2,1,0] . in′=[0,0,1] , out′=[1,0,0] . You can see that for all v the conditions hold. The maximum cute set S is formed by vertices 1 and 3 . 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 and outv are equal to 0 , leaving a graph with zero edges is allowed. There are 5 cute sets, each contains a single vertex. Thus, the maximum size is 1 .
In the third example, you can remove edges (7,1) , (2,4) , (1,3) and (6,2) . The maximum cute set will be S={7,3,2} . You can remove edge (7,3) as well, and the answer won't change.
Here is the picture of the graph from the third example: