#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
const int MX_N = 1e5+5;
int N, M;
vector<int> al[MX_N];
namespace BCT {
int pre[MX_N], low[MX_N], cnt, root, rootChildren;
vector<int> stk;
int comp[MX_N], sz[MX_N];
int nap, nv;
vector<int> tree[MX_N];
void dfs(int u, int p) {
pre[u] = low[u] = ++cnt;
for (int v : al[u]) if (v != p) {
if (pre[v] == -1) {
dfs(v,u);
if (u == root) ++rootChildren;
else if (comp[u] == -1 && low[v] >= pre[u]) comp[u] = ++nv, sz[nv] = 1;
low[u] = min(low[u],low[v]);
} else {
low[u] = min(low[u],pre[v]);
}
}
}
void addEdge(int u, int v) {
tree[u].push_back(v);
tree[v].push_back(u);
}
void decomp(int u, int p) {
pre[u] = ++cnt;
stk.push_back(u);
for (int v : al[u]) if (v != p) {
if (pre[v] == -1) {
int st = stk.back();
decomp(v,u);
if (comp[u] != -1 && low[v] >= pre[u]) {
sz[++nv] = 1;
addEdge(comp[u],nv);
while (stk.back() != st) {
int& w = stk.back(); stk.pop_back();
++sz[nv];
if (comp[w] == -1) comp[w] = nv;
else addEdge(comp[w],nv);
}
}
}
}
}
void run() {
fill_n(comp,N+1,-1); nv = 0;
fill_n(pre,N+1,-1); cnt = 0;
FOR(u,1,N) if (pre[u] == -1) {
root = u, rootChildren = 0;
dfs(u,0);
if (rootChildren > 1) comp[u] = ++nv, sz[nv] = 1;
}
nap = nv;
fill_n(pre,N+1,-1); cnt = 0;
FOR(u,1,N) if (pre[u] == -1) {
stk.clear();
decomp(u,0);
if (comp[u] == -1) {
sz[++nv] = 0;
while (!stk.empty()) {
int& w = stk.back(); stk.pop_back();
++sz[nv];
if (comp[w] == -1) comp[w] = nv;
else addEdge(comp[w],nv);
}
}
}
}
void dbg() {
FOR(i,1,N){ TRACE(i _ pre[i] _ low[i] _ comp[i]); }
TRACE(nv);
cout << "APs: "; FOR(i,1,N){ if (comp[i] <= nap) cout << i << ' '; } cout << endl;
FOR(i,1,nv){ cout << i << " (" << sz[i] << "): "; for(int v : tree[i]) { cout << v << ' '; } cout << endl; }
}
long long ans;
int sub[MX_N];
void precomp(int u, int p) {
sub[u] = sz[u];
for (int v : tree[u]) if (v != p) {
precomp(v,u);
sub[u] += sub[v];
--sub[u];
}
//TRACE(u _ sz[u] _ u _ sub[u]);
}
void trav(int u, int p, int all) {
for (int v : tree[u]) if (v != p) {
trav(v,u,all);
}
if (u <= nap) {
//if (p != 0) TRACE(u _ 2LL * (all-sub[u]) * (sub[u]-1));
if (p != 0) ans += 2LL * (all-sub[u]) * (sub[u]-1);
for (int v : tree[u]) if (v != p) {
ans += (sub[v]-1) * (sub[u]-sub[v]);
}
} else {
if (sz[u] >= 2) {
//TRACE(u _ (all-sub[u]) _ (sz[u]-2) _ (sz[u]-SZ(tree[u])));
if (p != 0) ans += 2LL * (all-sub[u]) * (sz[u]-2) * (sz[u]-SZ(tree[u]));
for (int v : tree[u]) if (v != p) {
ans += (sub[v]-1) * (sz[u]-2) * (sz[u]-SZ(tree[u]));
}
if (sz[u] >= 3) ans += sz[u] * (sz[u]-1) * (sz[u]-2);
}
}
}
long long solve() {
ans = 0;
fill_n(sub,N+1,-1);
FOR(i,1,nv) if (sub[i] == -1) {
precomp(i,0);
trav(i,0,sub[i]);
}
return ans;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
FOR(i,1,M){
int U, V; cin >> U >> V;
al[U].push_back(V);
al[V].push_back(U);
}
BCT::run();
//BCT::dbg();
cout << BCT::solve() << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
7 ms |
4992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
7 ms |
4992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
19696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5376 KB |
Output is correct |
5 |
Correct |
8 ms |
5248 KB |
Output is correct |
6 |
Correct |
9 ms |
5248 KB |
Output is correct |
7 |
Correct |
9 ms |
5376 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
9 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
9 ms |
5220 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
112 ms |
24824 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Incorrect |
10 ms |
5120 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
101 ms |
24568 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
7 ms |
4992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
7 ms |
4992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |