CF1674G.Remove Directed Edges

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 in′v\mathit{in'}_v and out′v\mathit{out'}_v.

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

  • in′v<inv\mathit{in'}_v \lt \mathit{in}_v or in′v=inv=0\mathit{in'}_v = \mathit{in}_v = 0;
  • out′v<outv\mathit{out'}_v \lt \mathit{out}_v or out′v=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 (v≠uv \neq u) such that v∈Sv \in S and u∈Su \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?

给你一个有向无环图(DAG),包含 nn 个顶点和 mm 条边。顶点编号为 11 到 nn。图中不存在重边和自环。

令 inv\mathit{in}_v 表示顶点 vv 的入度(即指向 vv 的边数),outv\mathit{out}_v 表示顶点 vv 的出度(即从 vv 出发的边数)。

你需要从图中删除若干条边。设删除后各顶点的新入度和新出度分别为 in′v\mathit{in'}_v 和 out′v\mathit{out'}_v。

你仅可在满足以下条件的前提下删除边:对每个顶点 vv,均需满足:

  • in′v<inv\mathit{in'}_v \lt \mathit{in}_v 或 in′v=inv=0\mathit{in'}_v = \mathit{in}_v = 0;
  • out′v<outv\mathit{out'}_v \lt \mathit{out}_v 或 out′v=outv=0\mathit{out'}_v = \mathit{out}_v = 0。

我们称顶点集合 SS 是“可爱的”(cute),当且仅当对任意两个不同顶点 v,uv, u(即 v≠uv \neq u),若 v∈Sv \in S 且 u∈Su \in S,则在未被删除的边构成的子图中,存在一条从 vv 到 uu 的路径,或存在一条从 uu 到 vv 的路径。

在满足上述删边约束(即所有顶点的入度和出度要么严格减小,要么保持为 00)的前提下,你能得到的“可爱”集合 SS 的最大可能大小是多少?

输入格式

The first line contains two integers nn and mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 0≤m≤2⋅1050 \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 (1≤v,u≤n1 \le v, u \le n; v≠uv \neq u) — the description of an edge.

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

第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;0≤m≤2⋅1050 \le m \le 2 \cdot 10^5)—— 分别表示图的顶点数和边数。

接下来的 mm 行,每行包含两个整数 vv 和 uu(1≤v,u≤n1 \le v, u \le n;v≠uv \neq u)—— 描述一条有向边。

给定的边构成一个合法的有向无环图(DAG),且不存在重边。

输出格式

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.

输出一个整数——在删除图中的一些边之后,所得到的“可爱”集合 SS 的最大可能大小,且所有顶点的入度和出度均不增加(即只能减少或保持为 00)。

输入输出样例

  • 输入#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)(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,2S = {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:

在第一个例子中,你可以删除边 (1,2)(1, 2) 和 (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]。可以看出,对所有顶点 vv,条件均成立。最大的“可爱”集合 SS 由顶点 11 和 33 构成。它们之间仍存在一条直接的边,因此二者间存在路径。

在第二个例子中,图中没有边。由于所有 inv\mathit{in}_v 和 outv\mathit{out}_v 均为 00,保留一个零条边的图是允许的。共有 55 个“可爱”集合,每个集合仅包含一个顶点。因此,最大大小为 11。

在第三个例子中,你可以删除边 (7,1)(7, 1)、(2,4)(2, 4)、(1,3)(1, 3) 和 (6,2)(6, 2)。此时最大的“可爱”集合为 S={7,3,2}S = \{7, 3, 2\}。你也可以额外删除边 (7,3)(7, 3),答案不会改变。

以下是第三个例子中图的示意图:

输入解题思路,AI测评打分。不知道怎么写?

首页