CF2204D.Alternating Path

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given an undirected graph with nn vertices and mm edges. The vertices are numbered from 11 to nn. The graph contains no self-loops or multiple edges.

Your task is to make a graph directed by choosing a direction for each edge. After directing the edges, call a sequence of vertices v1,v2,…,vkv_1, v_2, \dots, v_k, where kk can be arbitrarily large and any vertex can be repeated any number of times, an alternating path if:

  • the edge (v1,v2)(v_1, v_2) is directed from v1v_1 to v2v_2;
  • the edge (v2,v3)(v_2, v_3) is directed from v3v_3 to v2v_2;
  • the edge (v3,v4)(v_3, v_4) is directed from v3v_3 to v4v_4;
  • the edge (v4,v5)(v_4, v_5) is directed from v5v_5 to v4v_4;
  • and so on.

Call a vertex vv beautiful if all paths (not necessarily simple) in the original graph that start at vertex vv are alternating in the resulting directed graph.

What is the maximum number of vertices that can be made beautiful after directing the edges?

给你一个包含 nn 个顶点和 mm 条边的无向图。顶点编号为 11 到 nn。该图不含自环或重边。

你的任务是通过为每条边指定一个方向,将该图转化为有向图。在完成边的定向后,称一个顶点序列 v1,v2,…,vkv_1, v_2, \dots, v_k(其中 kk 可任意大,且任意顶点可重复出现任意多次)为交替路径,如果满足:

  • 边 (v1,v2)(v_1, v_2) 的方向为从 v1v_1 指向 v2v_2;
  • 边 (v2,v3)(v_2, v_3) 的方向为从 v3v_3 指向 v2v_2;
  • 边 (v3,v4)(v_3, v_4) 的方向为从 v3v_3 指向 v4v_4;
  • 边 (v4,v5)(v_4, v_5) 的方向为从 v5v_5 指向 v4v_4;
  • 依此类推。

称一个顶点 vv 是优美的,如果在原图中所有从顶点 vv 出发的路径(不一定是简单路径)在所得的有向图中均为交替路径。

在对边进行定向后,最多能有多少个顶点被设为优美的?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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 edges in the graph, respectively.

Each of the following mm lines contains two integers vv and uu (1≤v,u≤n1 \le v, u \le n) — the description of the edges of the graph.

Additional constraints on the input:

  • The given graph contains no self-loops or multiple edges;
  • The sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5;
  • The sum of mm over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

第一行包含两个整数 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),表示图中的一条边。

输入的额外约束条件:

  • 给定图中不含自环或重边;
  • 所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5;
  • 所有测试用例的 mm 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print a single integer — the maximum number of vertices that can be made beautiful after directing the edges.

对于每个测试用例,输出一个整数——在对边进行定向后,能够变为“美丽”的顶点的最大数量。

输入输出样例

  • 输入#1

    4
    8 9
    1 3
    1 4
    2 3
    2 4
    5 6
    6 7
    7 8
    8 5
    6 8
    4 0
    6 2
    1 5
    2 3
    1 0

    输出#1

    2
    4
    4
    1

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

首页