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>
using namespace std;
typedef long long ll;
const int N = 4e5 + 21, MOD = (int)1e9+7;
int n,m;
bool isb[N];
vector<vector<int>> bcc(vector<vector<int>> &g,vector<int> &sz){
vector<bool> ap(n+1,0);
vector<int> tin(n+1,0),up(n+1,0),id(n+1,0);
vector<int> stk;
vector<vector<int>> comps,G;
function <void(int,int,int&)> dfs = [&](int v,int pr,int &t){
tin[v] = up[v] = ++t;
stk.push_back(v);
int cc =0;
for(int to:g[v]){
if(to == pr)continue;
if(!tin[to]){
dfs(to,v,t);
cc++;
up[v] = min(up[v],up[to]);
if(up[to] >= tin[v]){
if(pr != -1){
ap[v] = 1;
}
comps.push_back({v});
while(comps.back().back() != to){
comps.back().push_back(stk.back());
stk.pop_back();
}
}
}else{
up[v] = min(up[v],tin[to]);
}
}
if(pr == -1){
ap[v] = (cc > 1);
}
};
int timer = 0;
for(int i = 1;i <= n;i++){
if(!tin[i]){
dfs(i,-1,timer);
}
}
function<vector<vector<int>>()> build_tree = [&]() {
vector<vector<int>> t(1);
int it = 0;
sz = vector<int> (1,0);
for(int i = 1;i <= n;i++){
if(ap[i]){
id[i] = ++it;
sz.push_back(1);
sz[id[i]] = 1;
t.push_back({});
}
}
for(auto c:comps){
it++;
isb[it] = 1;
sz.push_back(0);
t.push_back({});
for(auto j:c){
if(ap[j]){
t[id[j]].push_back(it);
t[it].push_back(id[j]);
}else{
id[j] = it;
sz[it]++;
}
}
// cout << '\n';
}
return t;
};
return build_tree();
}
vector<vector<int>> g(N),G;
ll calc(vector<int> &S){
ll s = 0,d = 0,t = 0;
for(auto i:S){
t += i *1ll * d;
d += s * 1ll * i;
s += i;
}
return t;
}
int bf_n,nn = 0;
vector<int> sz;
bool vis[N];
ll ss[N];
void prec(int v,int pr = -1){
vis[v] = 1;
ss[v] += sz[v];
for(int to:G[v]){
if(to == pr)continue;
prec(to,v);
ss[v] += ss[to];
}
}
ll res = 0;
void clac(int v,int pr = -1){
ll c = sz[v] * 1ll * (sz[v] - 1);
res += c / 2 *1ll * (sz[v] - 2 + (int)G[v].size());
res += c * 1ll * (nn - sz[v]);
vector<ll> vals;
for(int to:G[v]){
if(to == pr){
vals.push_back(nn-ss[v]);
continue;
}
clac(to,v);
vals.push_back(ss[to]);
}
ll D = 0,S = 0;
for(ll i:vals){
D += i * S;
S += i;
}
res += D * 1ll * sz[v] * (isb[v] ? 2ll : 1ll);
}
void test(){
cin >> n >> m;
for(int i = 1;i <= m;i++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
G = bcc(g,sz);
n = (int)G.size() - 1;
for(int i = 1;i <= n;i++){
if(!vis[i]){
prec(i);
nn = ss[i];
clac(i);
}
}
cout << res * 2 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# | 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... |