CF1454E.Number of Simple Paths

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an undirected graph consisting of nn vertices and nn edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph.

Your task is to calculate the number of simple paths of length at least 11 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). For example, paths [1,2,3][1, 2, 3] and [3,2,1][3, 2, 1] are considered the same.

You have to answer tt independent test cases.

Recall that a path in the graph is a sequence of vertices v1,v2,…,vkv_1, v_2, \ldots, v_k such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct.

给你一个包含 nn 个顶点和 nn 条边的无向图。保证该图是连通的(即任意顶点均可到达其余任意顶点),且图中不存在自环和重边。

你的任务是计算该图中长度至少为 11 的简单路径的数量。注意:仅方向不同的路径被视为同一条路径(即需计算无向路径的数量)。例如,路径 [1,2,3][1, 2, 3] 和 [3,2,1][3, 2, 1] 被视为同一条路径。

你需要回答 tt 个相互独立的测试用例。

回顾:图中的一条路径是指一个顶点序列 v1,v2,…,vkv_1, v_2, \ldots, v_k,使得该序列中每一对相邻(连续)顶点之间均存在一条边。路径的长度定义为其中所含边的数量。简单路径是指该路径中所有顶点互不相同的路径。

输入格式

The first line of the input contains one integer tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the number of vertices (and the number of edges) in the graph.

The next nn lines of the test case describe edges: edge ii is given as a pair of vertices uiu_i, viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), where uiu_i and viv_i are vertices the ii-th edge connects. For each pair of vertices (u,v)(u, v), there is at most one edge between uu and vv. There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i. e. all its edges are bidirectional. The graph is connected, i. e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph.

It is guaranteed that the sum of nn does not exceed 2⋅1052 \cdot 10^5 (∑n≤2⋅105\sum n \le 2 \cdot 10^5).

输入的第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5),表示图中顶点(以及边)的数量。

接下来的 nn 行描述该测试用例中的边:第 ii 条边由一对顶点 uiu_i、viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,且 ui≠viu_i \ne v_i)给出,表示该边连接顶点 uiu_i 和 viv_i。对于任意一对顶点 (u,v)(u, v),uu 与 vv 之间至多存在一条边;图中不存在从顶点指向自身的边。因此,图中既无自环,也无重边。该图是无向图,即所有边均为双向边。该图是连通的,即图中任意两个顶点之间均存在一条沿边构成的路径。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5(即 ∑n≤2⋅105\sum n \le 2 \cdot 10^5)。

输出格式

For each test case, print one integer: the number of simple paths of length at least 11 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths).

对于每个测试用例,输出一个整数:给定图中长度至少为 11 的简单路径的数量。注意,仅方向不同的路径被视为同一条路径(即,你需要计算无向路径的数量)。

输入输出样例

  • 输入#1

    3
    3
    1 2
    2 3
    1 3
    4
    1 2
    2 3
    3 4
    4 2
    5
    1 2
    2 3
    1 3
    2 5
    4 3

    输出#1

    6
    11
    18

说明/提示

Consider the second test case of the example. It looks like that:

There are 1111 different simple paths:

  1. [1,2][1, 2];
  2. [2,3][2, 3];
  3. [3,4][3, 4];
  4. [2,4][2, 4];
  5. [1,2,4][1, 2, 4];
  6. [1,2,3][1, 2, 3];
  7. [2,3,4][2, 3, 4];
  8. [2,4,3][2, 4, 3];
  9. [3,2,4][3, 2, 4];
  10. [1,2,3,4][1, 2, 3, 4];
  11. [1,2,4,3][1, 2, 4, 3].

考虑示例中的第二个测试用例,其结构如下:

共有 1111 条不同的简单路径:

  1. [1,2][1, 2];
  2. [2,3][2, 3];
  3. [3,4][3, 4];
  4. [2,4][2, 4];
  5. [1,2,4][1, 2, 4];
  6. [1,2,3][1, 2, 3];
  7. [2,3,4][2, 3, 4];
  8. [2,4,3][2, 4, 3];
  9. [3,2,4][3, 2, 4];
  10. [1,2,3,4][1, 2, 3, 4];
  11. [1,2,4,3][1, 2, 4, 3]。

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

首页