#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define PB push_back
#define pii pair<int,int>
#define ft first
#define sd second
#define MP make_pair
using namespace std;
typedef long long ll;
const int N = 100100;
const int M = 200100;
const int oo = 2e9;
vector<int> g[N];
vector<pii> gr[N];
int n, m, tin[N], fup[N], tt = 0, U[M], V[M], pr[N], siz[N];
int nw_siz[N];
bool mrk[N], bridge[M], cutpoint[N], cp[N], used[N];
ll ans = 0, SZ;
void dfs(int v, int p){
mrk[v] = 1;
tin[v] = fup[v] = ++tt;
for (int nm : g[v]){
int u = (U[nm] == v ? V[nm] : U[nm]);
if (u == p) continue;
if (mrk[u])
fup[v] = min(fup[v], tin[u]);
else {
dfs(u, v);
fup[v] = min(fup[v], fup[u]);
if (fup[u] > tin[v])
bridge[nm] = 1;
}
}
}
int get(int x) { return (x == pr[x] ? x : pr[x] = get(pr[x])); }
void dfs1(int v, int p){
nw_siz[v] = siz[v];
mrk[v] = 1;
for (pii u : gr[v]){
if (u.sd == p) continue;
dfs1(u.sd, v);
nw_siz[v] += nw_siz[u.sd];
}
}
void dfs2(int v, int p){
ans += ll(siz[v]) * (ll(siz[v]) - 1ll) * (ll(siz[v]) - 2ll) / 2ll; // first
ans += (SZ - ll(siz[v])) * (ll(siz[v]) - 1ll) * (ll(siz[v]) - 1ll);
ll sum = 0ll;
for (int it = 0; it < sz(gr[v]); ){
int j = it;
ll sim = 0;
while (j < sz(gr[v]) && gr[v][it].ft == gr[v][j].ft){
int u = gr[v][j].sd;
if (u != p) {
dfs2(u, v);
ans += sim * ll(nw_siz[u]);
sim += ll(nw_siz[u]);
} else {
ans += sim * (SZ - ll(nw_siz[v]));
sim += (SZ - ll(nw_siz[v]));
}
j++;
}
ans += sum * ll(siz[v]) * sim;
sum += sim;
it = j;
}
}
void search_cutpoint(int v, int p){
mrk[v] = 1;
tin[v] = fup[v] = ++tt;
int children = 0;
for (int nm : g[v]){
int u = (U[nm] == v ? V[nm] : U[nm]);
if (u == p) continue;
if (mrk[u])
fup[v] = min(fup[v], tin[u]);
else {
children++;
search_cutpoint(u, v);
fup[v] = min(fup[v], fup[u]);
if (fup[u] >= tin[v] && p >= 0){
cutpoint[v] = 1;
}
}
}
if (p < 0 && children > 1)
cutpoint[v] = 1;
}
void last_dfs(int v){
if (cp[v] || used[v]) return;
used[v] = 1;
ans++;
for (int nm : g[v]){
int u = (U[nm] == v ? V[nm] : U[nm]);
last_dfs(u);
}
}
void dfser(int v, int p, int st){
if (mrk[v]) return;
mrk[v] = 1;
if (cutpoint[v])
cp[v] = 1;
if (v != st){
fill(used, used + n, 0);
used[v] = 1;
for (int nm : g[v]){
int u = (U[nm] == v ? V[nm] : U[nm]);
if (u == p) continue;
last_dfs(u);
}
}
for (int nm : g[v]){
int u = (U[nm] == v ? V[nm] : U[nm]);
dfser(u, v, st);
}
if (cutpoint[v])
cp[v] = 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i = 0; i < m; i++){
cin >> U[i] >> V[i];
U[i]--; V[i]--;
g[U[i]].PB(i);
g[V[i]].PB(i);
}
if (n <= 50 && m <= 100){
for (int i = 0; i < n; i++){
if (mrk[i]) continue;
search_cutpoint(i, -1);
}
for (int st = 0; st < n; st++){
fill(mrk, mrk + n, 0);
dfser(st, -1, st);
}
cout << ans;
return 0;
}
for (int i = 0; i < n; i++) {
if (!mrk[i])
dfs(i, -1);
pr[i] = i;
siz[i] = 1;
}
for (int i = 0; i < m; i++) {
if (bridge[i]) continue;
int x = get(U[i]), y = get(V[i]);
if (x == y) continue;
pr[x] = y;
siz[y] += siz[x];
}
for (int i = 0; i < m; i++)
if (bridge[i]){
int x = get(U[i]), y = get(V[i]);
gr[x].PB(MP(U[i], y));
gr[y].PB(MP(V[i], x));
}
fill(mrk, mrk + n, 0);
for (int i = 0; i < n; i++)
if (sz(gr[i])){
sort(all(gr[i]));
}
for (int i = 0; i < n; i++){
if (get(i) != i) continue;
if (mrk[i]) continue;
dfs1(i, -1);
SZ = nw_siz[i];
dfs2(i, -1);
}
cout << ans * 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
19704 KB |
Output is correct |
2 |
Correct |
93 ms |
19704 KB |
Output is correct |
3 |
Correct |
154 ms |
17776 KB |
Output is correct |
4 |
Correct |
104 ms |
18936 KB |
Output is correct |
5 |
Correct |
110 ms |
15992 KB |
Output is correct |
6 |
Correct |
130 ms |
17144 KB |
Output is correct |
7 |
Correct |
137 ms |
15992 KB |
Output is correct |
8 |
Correct |
145 ms |
16476 KB |
Output is correct |
9 |
Correct |
119 ms |
15132 KB |
Output is correct |
10 |
Correct |
122 ms |
15352 KB |
Output is correct |
11 |
Correct |
78 ms |
13668 KB |
Output is correct |
12 |
Correct |
110 ms |
13452 KB |
Output is correct |
13 |
Correct |
102 ms |
13432 KB |
Output is correct |
14 |
Correct |
107 ms |
13304 KB |
Output is correct |
15 |
Correct |
88 ms |
12792 KB |
Output is correct |
16 |
Correct |
83 ms |
12492 KB |
Output is correct |
17 |
Correct |
11 ms |
7168 KB |
Output is correct |
18 |
Correct |
13 ms |
7168 KB |
Output is correct |
19 |
Correct |
12 ms |
7168 KB |
Output is correct |
20 |
Correct |
11 ms |
7168 KB |
Output is correct |
21 |
Correct |
12 ms |
7168 KB |
Output is correct |
22 |
Correct |
10 ms |
7168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5248 KB |
Output is correct |
2 |
Correct |
10 ms |
5248 KB |
Output is correct |
3 |
Correct |
9 ms |
5248 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5200 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
9 ms |
5248 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
9 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5172 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
9 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
9 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
16312 KB |
Output is correct |
2 |
Correct |
186 ms |
16224 KB |
Output is correct |
3 |
Correct |
167 ms |
16248 KB |
Output is correct |
4 |
Correct |
140 ms |
16216 KB |
Output is correct |
5 |
Correct |
183 ms |
16208 KB |
Output is correct |
6 |
Correct |
194 ms |
20600 KB |
Output is correct |
7 |
Correct |
201 ms |
19448 KB |
Output is correct |
8 |
Correct |
197 ms |
18552 KB |
Output is correct |
9 |
Correct |
189 ms |
17656 KB |
Output is correct |
10 |
Correct |
167 ms |
16248 KB |
Output is correct |
11 |
Correct |
168 ms |
16248 KB |
Output is correct |
12 |
Correct |
181 ms |
16248 KB |
Output is correct |
13 |
Correct |
174 ms |
16248 KB |
Output is correct |
14 |
Correct |
162 ms |
15736 KB |
Output is correct |
15 |
Correct |
130 ms |
15096 KB |
Output is correct |
16 |
Correct |
84 ms |
12920 KB |
Output is correct |
17 |
Correct |
97 ms |
16728 KB |
Output is correct |
18 |
Correct |
112 ms |
16768 KB |
Output is correct |
19 |
Correct |
100 ms |
16736 KB |
Output is correct |
20 |
Correct |
104 ms |
16744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
8 ms |
5248 KB |
Output is correct |
3 |
Correct |
9 ms |
5248 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
9 ms |
5248 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
9 ms |
5248 KB |
Output is correct |
10 |
Correct |
8 ms |
5248 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
9 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5248 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
9 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5248 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
9 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
8 ms |
5120 KB |
Output is correct |
25 |
Correct |
7 ms |
5120 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
16188 KB |
Output is correct |
2 |
Correct |
123 ms |
16120 KB |
Output is correct |
3 |
Correct |
134 ms |
15224 KB |
Output is correct |
4 |
Correct |
106 ms |
14296 KB |
Output is correct |
5 |
Correct |
102 ms |
13432 KB |
Output is correct |
6 |
Correct |
84 ms |
13048 KB |
Output is correct |
7 |
Correct |
97 ms |
12920 KB |
Output is correct |
8 |
Correct |
97 ms |
12920 KB |
Output is correct |
9 |
Correct |
84 ms |
12624 KB |
Output is correct |
10 |
Correct |
92 ms |
12664 KB |
Output is correct |
11 |
Correct |
87 ms |
12536 KB |
Output is correct |
12 |
Correct |
79 ms |
12664 KB |
Output is correct |
13 |
Correct |
92 ms |
12664 KB |
Output is correct |
14 |
Correct |
84 ms |
14072 KB |
Output is correct |
15 |
Correct |
123 ms |
18144 KB |
Output is correct |
16 |
Correct |
121 ms |
17656 KB |
Output is correct |
17 |
Correct |
139 ms |
18296 KB |
Output is correct |
18 |
Correct |
124 ms |
16660 KB |
Output is correct |
19 |
Correct |
98 ms |
14328 KB |
Output is correct |
20 |
Correct |
108 ms |
14328 KB |
Output is correct |
21 |
Correct |
124 ms |
14272 KB |
Output is correct |
22 |
Correct |
89 ms |
13816 KB |
Output is correct |
23 |
Correct |
80 ms |
13304 KB |
Output is correct |
24 |
Correct |
89 ms |
15380 KB |
Output is correct |
25 |
Correct |
108 ms |
15732 KB |
Output is correct |
26 |
Correct |
102 ms |
14832 KB |
Output is correct |
27 |
Correct |
96 ms |
14840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |