#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100'000 + 10;
int n, m;
vector<int> G[N], T[N];
bool seen[N];
ll cnt[N][3];
int h[N], minh[N], par[N], sz[N];
bool bridge[N];
vector<pair<int,int> > edges;
int root(int x)
{
if(par[x] == x) return x;
return par[x] = root(par[x]);
}
void merge(int a, int b)
{
a = root(a), b = root(b);
if(a == b) return;
if(sz[a] > sz[b]) swap(a, b);
par[a] = b;
sz[b] += sz[a];
}
void dfs(int v, int p = 0)
{
minh[v] = h[v] = h[p] + 1;
seen[v] = true;
for(int u : G[v])
{
if(u > v)
edges.push_back({u, v});
if(u == p) continue;
if(seen[u])
{
minh[v] = min(h[u], minh[v]);
}
else
{
dfs(u, v);
if(minh[u] > h[v])
bridge[u] = bridge[v] = true;
minh[v] = min(minh[u], minh[v]);
}
}
}
void compute(int v, int p = 0)
{
cnt[v][0] = sz[v];
cnt[v][1] = (sz[v] * (sz[v] - 1)) / 2;
cnt[v][2] = (sz[v] * (sz[v] - 1) * (sz[v] - 2)) / 6;
for(int u : T[v])
{
if(u == p) continue;
compute(u, v);
cnt[v][2] += cnt[v][1] * cnt[u][0] + cnt[v][0] * cnt[u][1] + cnt[u][2];
cnt[v][1] += cnt[u][1] + cnt[v][0] * cnt[u][0];
cnt[v][0] += cnt[u][0];
}
// cerr << v << ' ' << cnt[v][0] << ' ' << cnt[v][1] << ' ' << cnt[v][2] << ' ' << sz[v] << endl;
}
ll solve(int v)
{
dfs(v);
vector<pair<int,int> > tree;
for(auto [x, y] : edges)
{
if(!bridge[x] && !bridge[y])
merge(x, y);
else
tree.push_back({x, y});
}
edges.clear();
for(auto& [x, y] : tree)
{
x = root(x);
y = root(y);
if(x > y) swap(x, y);
edges.push_back({x, y});
}
sort(edges.begin(), edges.end());
edges.resize(unique(edges.begin(), edges.end()) - edges.begin());
for(auto [x, y] : edges)
{
T[x].push_back(y);
T[y].push_back(x);
cerr << x << ' ' << y << endl;
}
edges.clear();
compute(root(v));
return cnt[root(v)][2];
}
int main()
{
cin >> n >> m;
for(int i = 0; i < m ; i++)
{
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = 1; i <= n; i ++)
par[i] = i, sz[i] = 1;
ll ways = 0;
for(int i = 1; i < n; i ++)
{
if(seen[i]) continue;
ways += solve(i);
}
cout << 2 * ways << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
23760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
390 ms |
19840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
417 ms |
19704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |