This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct BIT{
int n;
vector<ll> sm;
BIT(int _n){
n = _n;
sm.resize(n);
}
void add(int in, ll x){
in++;
while(in <= n) sm[in-1]+=x, in+=in&-in;
}
ll sum(int in){
in++;
ll s = 0;
while(in >= 1) s+=sm[in-1], in-=in&-in;
return s;
}
ll sum(int l, int r){
return sum(r)-(l == 0? 0:sum(l-1));
}
};
int n, m;
vector<vector<int>> v, tree, backedge;
vector<bool> bl;
vector<bool> marked;
vector<int> sz;
vector<int> pos;
vector<int> sth;
ll ans = 0;
void dfs_tree(int nd, int ss = -1){
bl[nd] = 1;
for(int x: v[nd]){
if(!bl[x]){
bl[x] = 1;
tree[nd].pb(x);
dfs_tree(x, nd);
}
else{
if(x != ss) backedge[x].pb(nd);
}
}
}
void mark(int nd){
marked[nd] = !marked[nd];
for(int x: tree[nd]) mark(x);
}
int cnt = 0;
void pre_dfs(int nd){
sz[nd] = 1;
pos[nd] = cnt++;
for(int x: tree[nd]) pre_dfs(x);
for(int x: tree[nd]) sz[nd]+=sz[x];
}
int root;
BIT bit(0);
void dfs(int nd){
for(int x: tree[nd]) dfs(x);
for(int x: tree[nd]) sth[nd]+=sth[x];
bool bkg = 0;
for(int x: backedge[nd]) if(!marked[x]) bkg = 1;
if(bkg){
ll cur = sz[root]-sz[nd];
cur-=bit.sum(pos[root], pos[root]+sz[root]-1)-bit.sum(pos[nd], pos[nd]+sz[nd]-1);
cur*=2;
cur*=sth[nd];
sth[nd] = 0;
ans+=cur;
bit.add(pos[nd], 1);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
v.assign(n, {});
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
marked.assign(n, 0);
sz.resize(n);
pos.resize(n);
for(int i = 0; i < n; i++){
tree.assign(n, {});
backedge.assign(n, {});
bl.assign(n, 0);
sth.assign(n, 1);
bit = BIT(n);
dfs_tree(i);
cnt = 0;
pre_dfs(i);
for(int x: tree[i]){
ans+=ll(sz[i]-1-sz[x])*ll(sz[x]);
root = x;
mark(x);
dfs(x);
mark(x);
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |