# 【Codeforces 1454 E】Number of Simple Paths，基环树，拓扑排序找环，dfs统计节点数

#### problem

E. Number of Simple Paths
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an undirected graph consisting of n vertices and n 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 1 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] and [3,2,1] are considered the same.

You have to answer t independent test cases.

Recall that a path in the graph is a sequence of vertices v1,v2,…,vk 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.

Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (3≤n≤2⋅105) — the number of vertices (and the number of edges) in the graph.

The next n lines of the test case describe edges: edge i is given as a pair of vertices ui, vi (1≤ui,vi≤n, ui≠vi), where ui and vi are vertices the i-th edge connects. For each pair of vertices (u,v), there is at most one edge between u and v. 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 n does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print one integer: the number of simple paths of length at least 1 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).

Example
inputCopy
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
outputCopy
6
11
18
Note
Consider the second test case of the example. It looks like that:

There are 11 different simple paths:

[1,2];
[2,3];
[3,4];
[2,4];
[1,2,4];
[1,2,3];
[2,3,4];
[2,4,3];
[3,2,4];
[1,2,3,4];
[1,2,4,3].

#### solution

/*

+ 给出一个n个点n条边的图，求简单路径数。

+ 可以发现是基环树，所以先拓扑排序找环。
+ 然后我们把环上的每个点当作一棵树的根，dfs求每棵树的节点sz[i]。
+ 当两点位于不同子树，则路径为2，位于同一子树则路径为1。另pre[i]=sz[1]+sz[2]+...sz[i]，即当前子树i的路径数=子树内sz[i]*(sz[i]-1)/2+子树外(pre[rt]-pre[i])*sz[i]*2.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+5;
const int mod = 1e9+7;

vector<int>G[maxn], vec;
int in[maxn], vis[maxn];
int sz[maxn], pre[maxn];
void dfs(int rt, int u, int fa){
sz[rt]++;
for(int v:G[u]){
if(v==fa || vis[v]==1)continue;
dfs(rt,v,u);
}
}

int main(){
ios::sync_with_stdio(false);
int T;  cin>>T;
while(T--){
//input
int n;  cin>>n;
for(int i = 1; i <= n; i++)G[i].clear();
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
vec.clear();
for(int i = 1; i <= n; i++){
int u, v;  cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
in[u]++; in[v]++;
}

//topu
queue<int>q;
for(int i = 1; i<= n; i++)
if(in[i]==1)q.push(i);
while(q.size()){
int x = q.front();  q.pop();
for(int u: G[x]){
in[u]--;
if(in[u]==1)q.push(u);
}
}
for(int i = 1; i <= n; i++)
if(in[i]>=2)
{ vis[i] = 1;  vec.push_back(i); }

//sz
memset(sz,0,sizeof(sz));
memset(pre,0,sizeof(pre));
int rt = 0;
for(int u: vec) dfs(++rt, u, u);
for(int i = 1; i <= rt; i++)
pre[i] = pre[i-1]+sz[i];
LL ans = 0;
for(int i = 1; i <= rt; i++){
ans += (LL)sz[i]*(sz[i]-1)/2+(LL)2*sz[i]*(pre[rt]-pre[i]);
}
cout<<ans<<"\n";
}
return 0;
}


10-04 253

08-02 856
03-27 38
04-09 67
07-15 1344
10-26 1682
08-20 1373
08-28 2296
03-04 1362
11-01 7593
07-15 1756
08-12 1550
08-14 1522
10-07 1275