-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe.cpp
68 lines (63 loc) · 1.51 KB
/
e.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
int T, N;
vector<int> graph[200000], cur;
bool vis[200000], cycle[200000], found_cycle = false;
int dfs(int id, int par){
int ans = 1;
for(int i : graph[id]){
if(i == par || cycle[i]) continue;
ans += dfs(i, id);
}
return ans;
}
void cyclef(int id, int par){
if(vis[id]){
for(int i = (int)cur.size() - 1; i >= 0; i--){
cycle[cur[i]] = true;
if(cur[i] == id) break;
}
found_cycle = true;
return;
}
cur.push_back(id);
vis[id] = true;
for(int i : graph[id]){
if(i == par) continue;
cyclef(i, id);
if(found_cycle) return;
}
vis[id] = false;
cur.pop_back();
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while(T--){
cin >> N;
for(int i = 0; i < N; i++){
graph[i].clear();
}
found_cycle = false;
fill(vis, vis+N, false);
fill(cycle, cycle+N, false);
cur.clear();
for(int i = 0; i < N; i++){
int u, v; cin >> u >> v;
graph[u-1].push_back(v-1);
graph[v-1].push_back(u-1);
}
cyclef(0, -1);
int ans = 0;
for(int i = 0; i < N; i++){
if(cycle[i]){
int nodes = dfs(i, -1);
ans += (nodes * (nodes - 1)) + (2 * (N - nodes) * nodes);
}
}
cout << ans / 2 << endl;
}
}