Problem - E - Codeforces
Problem - E - Codeforces
4train | Logout
HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP ICPC CHALLENGE
You are given an undirected graph consisting of n vertices and n edges. It is guaranteed that → Virtual participation
the given graph is connected (i. e. it is possible to reach any vertex from any other vertex)
Virtual contest is a way to take part in past
and there are no self-loops and multiple edges in the graph. contest, as close as possible to participation
on time. It is supported only ICPC mode for
virtual contests. If you've seen these
Your task is to calculate the number of simple paths of length at least 1 in the given graph. problems, a virtual contest is not for you -
Note that paths that differ only by their direction are considered the same (i. e. you have to solve these problems in the archive. If you
just want to solve some problem from a
calculate the number of undirected paths). For example, paths [1, 2, 3] and [3, 2, 1] are contest, a virtual contest is not for you -
considered the same. solve this problem in the archive. Never use
someone else's code, read the tutorials or
communicate with other person during a
You have to answer t independent test cases. virtual contest.
Recall that a path in the graph is a sequence of vertices v1 , v2 , … , vk such that each pair of Start virtual contest
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.
→ Clone Contest to Mashup
Input
The first line of the input contains one integer t (1
4
≤ t ≤ 2 ⋅ 10 ) — the number of test You can clone this contest to a mashup.
cases. Then t test cases follow.
Clone Contest
5 5
It is guaranteed that the sum of n does not exceed 2 ⋅ 10 (∑ n ≤ 2 ⋅ 10 ).
→ Problem tags
Output
For each test case, print one integer: the number of simple paths of length at least 1 in the combinatorics dfs and similar graphs
given graph. Note that paths that differ only by their direction are considered the same (i. e.
trees *2000
you have to calculate the number of undirected paths).
No tag edit access
Example
input Copy → Contest materials
3
3 Announcement (en)
1 2
2 3 Tutorial
1 3 watch (en)
4
1 2
2 3
3 4
4 2
5
1 2
2 3
1 3
2 5
4 3
output Copy
6
11
18
Note
Consider the second test case of the example. It looks like that:
1. [1, 2];
2. [2, 3];
3. [3, 4];
4. [2, 4];
5. [1, 2, 4];
6. [1, 2, 3];
7. [2, 3, 4];
8. [2, 4, 3];
9. [3, 2, 4];
10. [1, 2, 3, 4] ;
11. [1, 2, 4, 3] .
Supported by