CF1656I.Neighbour Ordering

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given an undirected graph GG, we say that a neighbour ordering is an ordered list of all the neighbours of a vertex for each of the vertices of GG. Consider a given neighbour ordering of GG and three vertices uu, vv and ww, such that vv is a neighbor of uu and ww. We write u<vwu \lt _{v} w if uu comes after ww in vv's neighbor list.

A neighbour ordering is said to be good if, for each simple cycle v1,v2,…,vcv_1, v_2, \ldots, v_c of the graph, one of the following is satisfied:

  • v1<v2v3,v2<v3v4,…,vc−2<vc−1vc,vc−1<vcv1,vc<v1v2v_1 \lt _{v_2} v_3, v_2 \lt _{v_3} v_4, \ldots, v_{c-2} \lt _{v_{c-1}} v_c, v_{c-1} \lt _{v_c} v_1, v_c \lt _{v_1} v_2.
  • v1>v2v3,v2>v3v4,…,vc−2>vc−1vc,vc−1>vcv1,vc>v1v2v_1 \gt _{v_2} v_3, v_2 \gt _{v_3} v_4, \ldots, v_{c-2} \gt _{v_{c-1}} v_c, v_{c-1} \gt _{v_c} v_1, v_c \gt _{v_1} v_2.

Given a graph GG, determine whether there exists a good neighbour ordering for it and construct one if it does.

给定一个无向图 GG,我们称邻居排序(neighbour ordering)为:对图 GG 中的每个顶点,将其所有邻居按某种顺序排成的一个有序列表。考虑图 GG 的一个给定邻居排序,以及三个顶点 uu、vv 和 ww,其中 vv 同时是 uu 和 ww 的邻居。我们记 u<vwu \lt _{v} w,当且仅当在 vv 的邻居列表中,uu 出现在 ww 之后。

若对图 GG 的任意简单环 v1,v2,…,vcv_1, v_2, \ldots, v_c,以下两个条件之一成立,则称该邻居排序是好的(good):

  • v1<v2v3,  v2<v3v4,  …,  vc−2<vc−1vc,  vc−1<vcv1,  vc<v1v2v_1 \lt _{v_2} v_3,\; v_2 \lt _{v_3} v_4,\; \ldots,\; v_{c-2} \lt _{v_{c-1}} v_c,\; v_{c-1} \lt _{v_c} v_1,\; v_c \lt _{v_1} v_2;
  • v1>v2v3,  v2>v3v4,  …,  vc−2>vc−1vc,  vc−1>vcv1,  vc>v1v2v_1 \gt _{v_2} v_3,\; v_2 \gt _{v_3} v_4,\; \ldots,\; v_{c-2} \gt _{v_{c-1}} v_c,\; v_{c-1} \gt _{v_c} v_1,\; v_c \gt _{v_1} v_2。

给定一个图 GG,请判断是否存在一个好的邻居排序;若存在,请构造出一个。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤3⋅1052 \leq n \leq 3 \cdot 10^5, 1≤m≤3⋅1051 \leq m \leq 3 \cdot 10^5), the number of vertices and the number of edges of the graph.

The next mm lines each contain two integers u,vu, v (0≤u,v<n0 \leq u, v \lt n), denoting that there is an edge connecting vertices uu and vv. It is guaranteed that the graph is connected and there are no loops or multiple edges between the same vertices.

The sum of nn and the sum of mm for all test cases are at most 3⋅1053 \cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤3⋅1052 \leq n \leq 3 \cdot 10^5,1≤m≤3⋅1051 \leq m \leq 3 \cdot 10^5),分别表示图的顶点数和边数。

接下来的 mm 行,每行包含两个整数 uu 和 vv(0≤u,v<n0 \leq u, v \lt n),表示顶点 uu 与顶点 vv 之间存在一条边。保证该图是连通的,且不存在自环或两点间有多条重边。

所有测试用例的 nn 之和与 mm 之和均不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, output one line with YES if there is a good neighbour ordering, otherwise output one line with NO. You can print each letter in any case (upper or lower).

If the answer is YES, additionally output nn lines describing a good neighbour ordering. In the ii-th line, output the neighbours of vertex ii in order.

If there are multiple good neigbour orderings, print any.

对于每个测试用例,如果存在一个“好邻居排序”,则输出一行 YES;否则输出一行 NO。你可以以任意大小写(大写或小写)输出每个字母。

如果答案为 YES,则还需额外输出 nn 行,描述一个“好邻居排序”。在第 ii 行中,按顺序输出顶点 ii 的所有邻居。

若存在多个“好邻居排序”,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    5 6
    0 1
    0 2
    1 2
    2 3
    3 4
    4 1
    2 1
    0 1
    6 10
    0 1
    2 0
    0 3
    0 4
    1 2
    1 4
    2 3
    2 5
    3 5
    4 5

    输出#1

    YES
    1 2 
    4 2 0 
    0 1 3 
    2 4 
    3 1 
    YES
    1 
    0 
    NO

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

首页