#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)/2 + (sz[j]-1) * (sz[j]-2) /2*sz[i];
}
}
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 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
24132 KB |
Output is correct |
2 |
Correct |
51 ms |
24164 KB |
Output is correct |
3 |
Execution timed out |
1046 ms |
22100 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12636 KB |
Output is correct |
2 |
Correct |
5 ms |
12636 KB |
Output is correct |
3 |
Correct |
5 ms |
12632 KB |
Output is correct |
4 |
Correct |
5 ms |
12888 KB |
Output is correct |
5 |
Correct |
5 ms |
12892 KB |
Output is correct |
6 |
Correct |
5 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
12892 KB |
Output is correct |
8 |
Correct |
5 ms |
12880 KB |
Output is correct |
9 |
Correct |
5 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
12740 KB |
Output is correct |
11 |
Correct |
5 ms |
12636 KB |
Output is correct |
12 |
Correct |
6 ms |
12636 KB |
Output is correct |
13 |
Correct |
5 ms |
12848 KB |
Output is correct |
14 |
Correct |
5 ms |
12632 KB |
Output is correct |
15 |
Correct |
5 ms |
12636 KB |
Output is correct |
16 |
Correct |
6 ms |
12636 KB |
Output is correct |
17 |
Correct |
4 ms |
12888 KB |
Output is correct |
18 |
Correct |
5 ms |
12772 KB |
Output is correct |
19 |
Correct |
6 ms |
12636 KB |
Output is correct |
20 |
Correct |
5 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1070 ms |
20268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12636 KB |
Output is correct |
2 |
Correct |
5 ms |
12636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
12636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1063 ms |
20368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |