#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e5 + 69;
int n, m;
vector<int> graph[N];
int num[N], low[N];
bool isJoint[N];
vector<int> st;
int dfs_cnt;
vector<vector<int>> bbc;
void dfs(int u, int p){
num[u] = low[u] = ++dfs_cnt;
st.push_back(u);
vector<int> child;
for(int v: graph[u]) {
if (v == p) {p = -1; continue;}
if (num[v]){
minimize(low[u], num[v]);
}
else{
child.push_back(v);
dfs(v, u);
minimize(low[u], low[v]);
if (low[v] >= num[u]){
isJoint[u] = true;
}
}
}
if (u == p) isJoint[u] = isJoint[u] && (child.size() >= 2);
if (!isJoint[u]) return;
while(child.size()){
int v = child.back();
child.pop_back();
bbc.push_back({});
while(bbc.back().empty() || bbc.back().back() != v){
bbc.back().push_back(st.back());
st.pop_back();
}
bbc.back().push_back(u);
}
}
vector<int> bc_tree[2*N];
int joint_cnt = 0;
int pos[2*N];
int sz[2 * N];
int generate_block_cut_tree(){
dfs_cnt = 0;
for(int i= 1; i<=n; ++i) if (num[i] == 0){
dfs(i, i);
if (st.size()) {
bbc.push_back(st);
st.clear();
}
}
int idx = 0;
for(int i= 1; i<=n; ++i){
if (!isJoint[i]) continue;
joint_cnt++;
pos[i] = ++idx;
sz[idx] = -1;
}
for(vector<int> i: bbc){
++idx;
sz[idx] = i.size();
for(int j: i){
if (isJoint[j]){
int u = idx, v = pos[j];
bc_tree[u].push_back(v);
bc_tree[v].push_back(u);
}
}
}
return idx;
}
bool visited[N * 2];
int og[N * 2];
ll ans = 0;
void get_sz(int u, int p){
visited[u] = 1;
og[u] = sz[u];
for(int v: bc_tree[u]) if (v != p){
get_sz(v, u);
sz[u] += sz[v];
}
}
void dfs(int u, int p, int M){
if (u <= joint_cnt){
vector<int> child;
for(int v: bc_tree[u]){
if (v != p) child.push_back(sz[v] - 1);
}
child.push_back(og[p] -1 );
int sum = 0;
for(int i: child) {
ans -= 4LL * i * sum;
sum += i;
}
sum -= child.back();
child.pop_back();
child.push_back(M - sz[u] - 1);
sum += child.back();
for(int i: child){
ans -= 1LL * i * (sum - i) * (sum - i - 1);
}
}
for(int v: bc_tree[u]) if (v != p){
dfs(v, u, M);
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
// if (m != n - 1) return -1;
for(int i= 0; i<m; ++i){
int u, v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
int tree_sz = generate_block_cut_tree();
for(int i= joint_cnt + 1; i<=tree_sz; ++i) if (!visited[i]){
get_sz(i, i);
int M = sz[i];
ans += 1LL * M * (M - 1) * (M - 2);
dfs(i, i, M);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
32724 KB |
Output is correct |
2 |
Correct |
45 ms |
32648 KB |
Output is correct |
3 |
Incorrect |
64 ms |
34076 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
25548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
25524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |