#include <iostream>
using namespace std;
#include <vector>
#define vi vector<int>
#define ll long long
#include <algorithm>
#include <set>
#include <string>
#include <bitset>
#include <cmath>
#include <math.h>
#define pll pair<ll,ll>
#define vll vector<ll>
#define pi pair<int,int>
#include <map>
#include <queue>
#define pd pair<double,double>
#include <stack>
const int siz = 1e5 + 1;
int c[siz];
vi fr[siz];
bool vis[siz];
int pad[siz];
vi frt[siz];
int padt[siz];
ll w[siz];
int sor[siz];
int cum[siz];
void dfs(int i, int&s, int so) {
s++;
sor[i] = so;
if (vis[i])return;
vis[i] = 1;
for (int f : fr[i]) {
if (!vis[f]) {
pad[f] = i;
dfs(f,s,so);
}
else if (f != pad[i] && c[i]==i) {
int k = pad[i];
while (k != f) {
w[i]++;
c[k] = i;
k = pad[k];
}
w[i]++;
c[k] = i;
}
}
}
ll ans = 0;
int n, m;
bool vist[siz];
int dfst(int i, int t) {
vist[i] = 1;
vll v;
int sum = w[i];
for (int f : frt[i]) {
if (!vist[f]) {
v.push_back(dfst(f,t));
sum += v.back();
}
}
v.push_back(t - sum);
ans += (w[i] * (w[i] - 1)) * (t - 2);
for (ll k : v) {
ans += w[i] * k * (t - 1 - k);
ans -= 2 * k * (w[i] - 1);
}
return sum;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
fr[a].push_back(b);
fr[b].push_back(a);
}
for (int i = 0; i < n; i++) {
w[i] = 1;
c[i] = i;
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
pad[i] = i;
int s = 0;
dfs(i,s,i);
cum[i] = s;
}
}
for (int i = 0; i < n; i++) {
for (int f : fr[i]) {
if (c[i] != c[f]) {
frt[c[i]].push_back(c[f]);
}
}
}
for(int i = 0; i < n; i++)if(!vist[c[i]])dfst(c[i],cum[sor[i]]);
cout << ans;
}
/*
4 3
1 2
2 3
3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
7260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
7260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
18772 KB |
Output is correct |
2 |
Correct |
66 ms |
18772 KB |
Output is correct |
3 |
Correct |
71 ms |
17748 KB |
Output is correct |
4 |
Correct |
66 ms |
18772 KB |
Output is correct |
5 |
Correct |
66 ms |
16720 KB |
Output is correct |
6 |
Correct |
96 ms |
18096 KB |
Output is correct |
7 |
Correct |
70 ms |
16468 KB |
Output is correct |
8 |
Correct |
70 ms |
16976 KB |
Output is correct |
9 |
Correct |
73 ms |
15444 KB |
Output is correct |
10 |
Correct |
69 ms |
15456 KB |
Output is correct |
11 |
Correct |
60 ms |
13716 KB |
Output is correct |
12 |
Correct |
56 ms |
13500 KB |
Output is correct |
13 |
Correct |
53 ms |
13648 KB |
Output is correct |
14 |
Correct |
52 ms |
13404 KB |
Output is correct |
15 |
Correct |
53 ms |
13576 KB |
Output is correct |
16 |
Correct |
42 ms |
12884 KB |
Output is correct |
17 |
Correct |
5 ms |
8028 KB |
Output is correct |
18 |
Correct |
5 ms |
8096 KB |
Output is correct |
19 |
Correct |
5 ms |
8028 KB |
Output is correct |
20 |
Correct |
6 ms |
8028 KB |
Output is correct |
21 |
Correct |
4 ms |
8028 KB |
Output is correct |
22 |
Correct |
5 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7536 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7524 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7512 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7512 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7520 KB |
Output is correct |
19 |
Correct |
2 ms |
7768 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
14276 KB |
Output is correct |
2 |
Correct |
85 ms |
14324 KB |
Output is correct |
3 |
Correct |
75 ms |
14420 KB |
Output is correct |
4 |
Correct |
75 ms |
14444 KB |
Output is correct |
5 |
Correct |
73 ms |
14416 KB |
Output is correct |
6 |
Correct |
103 ms |
20816 KB |
Output is correct |
7 |
Correct |
90 ms |
18768 KB |
Output is correct |
8 |
Correct |
78 ms |
17756 KB |
Output is correct |
9 |
Correct |
80 ms |
16468 KB |
Output is correct |
10 |
Correct |
77 ms |
14264 KB |
Output is correct |
11 |
Correct |
72 ms |
15556 KB |
Output is correct |
12 |
Correct |
72 ms |
15508 KB |
Output is correct |
13 |
Correct |
72 ms |
15696 KB |
Output is correct |
14 |
Correct |
66 ms |
15284 KB |
Output is correct |
15 |
Correct |
86 ms |
15056 KB |
Output is correct |
16 |
Correct |
42 ms |
13292 KB |
Output is correct |
17 |
Correct |
63 ms |
17360 KB |
Output is correct |
18 |
Correct |
62 ms |
17228 KB |
Output is correct |
19 |
Correct |
62 ms |
17104 KB |
Output is correct |
20 |
Correct |
64 ms |
16932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Incorrect |
2 ms |
7516 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
14420 KB |
Output is correct |
2 |
Correct |
73 ms |
14160 KB |
Output is correct |
3 |
Incorrect |
111 ms |
13360 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
7260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
7260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |