#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, MOD = 998244353;
typedef long double ld;
int n,m,tin[N],fup[N],timer;
bool vis[N];
vector<int> g[N];
void dfs(int v,int pr = -1){
vis[v]=1;
fup[v] = tin[v] = ++timer;
for(int to:g[v]){
if(to == pr) continue;
if(!vis[to]){
dfs(to,v);
fup[v] = min(fup[v],fup[to]);
}else{
fup[v] = min(fup[v],tin[to]);
}
}
}
int mxc=0,c[N];
ll sz[N];
void paint(int v,int cl){
c[v] = cl;
sz[cl]++;
for(int to:g[v]){
if(!c[to]){
if(fup[to] > tin[v]){
mxc++;
paint(to,mxc);
}else{
paint(to,cl);
}
}
}
}
vector<int> G[N];
int P[N];
int get(int v){
if(P[v] == v) return v;
return P[v]= get(P[v]);
}
void merge(int a,int b){
a = get(a);
b = get(b);
P[a] = b;
}
int sum[N];
void dfs2(int v,int pr = -1){
sum[v] = sz[v];
for(int to:G[v]){
if(to == pr) continue;
dfs2(to,v);
sum[v] += sum[to];
}
}
ll res = 0;
void dfs3(int v,int pr = -1,int S = 0){
ll ss =0 ,ff = S - sum[v];
for(int to:G[v]){
if(to != pr){
dfs3(to,v,S);
ss += ff * 1ll * sum[to];
ff += sum[to];
}
}
ss *= 2;
res += ss * 1ll * sz[v];
}
void test(){
cin >> n >> m;
for(int i = 1;i <= m;i++){
int a,b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1;i <= n;i++){
P[i] = i;
if(!vis[i]){
dfs(i);
}
}
for(int i = 1;i <= n;i++){
if(!c[i]){
mxc++;
paint(i,mxc);
}
}
for(int i = 1;i <= n;i++){
for(int to:g[i]){
int x = c[i],y = c[to];
if(get(x)!=get(y)){
G[x].push_back(y);
G[y].push_back(x);
merge(x,y);
}
}
}
n = mxc;
for(int i = 1;i <= n;i++) {
vis[i]=0;
res += sz[i] * (sz[i] - 1) * (sz[i] - 2);
}
ll A = 0;
// for(int i = 1;i <= n;i++){
// for(int j = 1;j <= n;j++){
// if(i == j) continue;
//
// A += sz[i] * sz[j] * (sz[j] - 1) - sz[j]+1;
// }
// }
res += A*2;
for(int i = 1;i <= n;i++){
if(!sum[i]){
dfs2(i);
dfs3(i,-1,sum[i]);
}
}
cout << res << '\n';
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int T = 1;
// cin >> T;
while (T--){
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
24156 KB |
Output is correct |
2 |
Correct |
46 ms |
24156 KB |
Output is correct |
3 |
Correct |
45 ms |
22392 KB |
Output is correct |
4 |
Correct |
40 ms |
23124 KB |
Output is correct |
5 |
Correct |
38 ms |
20180 KB |
Output is correct |
6 |
Correct |
44 ms |
21716 KB |
Output is correct |
7 |
Correct |
42 ms |
20560 KB |
Output is correct |
8 |
Correct |
45 ms |
20828 KB |
Output is correct |
9 |
Correct |
43 ms |
19732 KB |
Output is correct |
10 |
Correct |
42 ms |
19824 KB |
Output is correct |
11 |
Correct |
36 ms |
18524 KB |
Output is correct |
12 |
Correct |
34 ms |
18512 KB |
Output is correct |
13 |
Correct |
34 ms |
18684 KB |
Output is correct |
14 |
Correct |
31 ms |
18344 KB |
Output is correct |
15 |
Correct |
27 ms |
18480 KB |
Output is correct |
16 |
Correct |
34 ms |
18064 KB |
Output is correct |
17 |
Correct |
7 ms |
14172 KB |
Output is correct |
18 |
Correct |
6 ms |
14184 KB |
Output is correct |
19 |
Correct |
5 ms |
14168 KB |
Output is correct |
20 |
Correct |
5 ms |
14104 KB |
Output is correct |
21 |
Correct |
5 ms |
14172 KB |
Output is correct |
22 |
Correct |
5 ms |
14172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12720 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12708 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12888 KB |
Output is correct |
13 |
Correct |
4 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
3 ms |
12740 KB |
Output is correct |
19 |
Correct |
3 ms |
12732 KB |
Output is correct |
20 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
20420 KB |
Output is correct |
2 |
Correct |
53 ms |
20564 KB |
Output is correct |
3 |
Correct |
67 ms |
20632 KB |
Output is correct |
4 |
Correct |
52 ms |
20508 KB |
Output is correct |
5 |
Correct |
56 ms |
20496 KB |
Output is correct |
6 |
Correct |
56 ms |
25428 KB |
Output is correct |
7 |
Correct |
60 ms |
23772 KB |
Output is correct |
8 |
Correct |
54 ms |
22860 KB |
Output is correct |
9 |
Correct |
55 ms |
22100 KB |
Output is correct |
10 |
Correct |
50 ms |
20564 KB |
Output is correct |
11 |
Correct |
56 ms |
20560 KB |
Output is correct |
12 |
Correct |
55 ms |
20560 KB |
Output is correct |
13 |
Correct |
50 ms |
20660 KB |
Output is correct |
14 |
Correct |
44 ms |
20308 KB |
Output is correct |
15 |
Correct |
40 ms |
20048 KB |
Output is correct |
16 |
Correct |
26 ms |
18772 KB |
Output is correct |
17 |
Correct |
34 ms |
21276 KB |
Output is correct |
18 |
Correct |
33 ms |
21332 KB |
Output is correct |
19 |
Correct |
35 ms |
21456 KB |
Output is correct |
20 |
Correct |
39 ms |
21332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
20568 KB |
Output is correct |
2 |
Correct |
55 ms |
20512 KB |
Output is correct |
3 |
Incorrect |
51 ms |
19024 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |